Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:44:42+00:00 2026-06-02T19:44:42+00:00

I’m new to Qt application development, I would appreciate your help. This is a

  • 0

I’m new to Qt application development, I would appreciate your help. This is a re-post of
redirect std::cout to QTextEdit

I’m trying to redirect std::cout to a QTextEdit and I’ve seen and tried to test the example provided in the following link.

Reference Link 1: http://lists.trolltech.com/qt-interest/2005-06/thread00166-0.html

Using Qt Creator 2.4.1 to test the example from Reference Link 1.

untiled1.pro

SOURCES += \
main.cpp
HEADERS += \
qdebugstream.h

qdebugstream.h

    #ifndef QDEBUGSTREAM_H
#define QDEBUGSTREAM_H

#include <iostream>
#include <streambuf>
#include <string>

#include "qtextedit.h"

class QDebugStream : public std::basic_streambuf<char>
{
public:
 QDebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
 {
  log_window = text_edit;
  m_old_buf = stream.rdbuf();
  stream.rdbuf(this);
 }
 ~QDebugStream()
 {
  // output anything that is left
  if (!m_string.empty())
   log_window->append(m_string.c_str());

  m_stream.rdbuf(m_old_buf);
 }

protected:
 virtual int_type overflow(int_type v)
 {
  if (v == '\n')
  {
   log_window->append(m_string.c_str());
   m_string.erase(m_string.begin(), m_string.end());
  }
  else
   m_string += v;

  return v;
 }

 virtual std::streamsize xsputn(const char *p, std::streamsize n)
 {
  m_string.append(p, p + n);

  int pos = 0;
  while (pos != std::string::npos)
  {
   pos = m_string.find('\n');
   if (pos != std::string::npos)
   {
    std::string tmp(m_string.begin(), m_string.begin() + pos);
    log_window->append(tmp.c_str());
    m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
   }
  }

  return n;
 }

private:
 std::ostream &m_stream;
 std::streambuf *m_old_buf;
 std::string m_string;


  QTextEdit* log_window;
    };

#endif

main.cpp

#include "qdebugstream.h"
#include "qtextedit.h"
#include <QtGui>

int main(int argc, char **argv)
{
    QApplication application(argc, argv);
       application.connect(&application, SIGNAL(lastWindowClosed()),
    &application, SLOT(quit()));

       QMainWindow* mainWindow = new QMainWindow();

       QTextEdit* myTextEdit = new QTextEdit(mainWindow, "myTextEdit");
       myTextEdit->setTextFormat(Qt::LogText);
       QDebugStream qout(std::cout, myTextEdit);

       mainWindow->show();
       std::cout << "Send this to the Text Edit!" << std::endl;

       return application.exec();
}

I get the following error messages:

C:\Documents and
Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:13: error: C2664: ‘QTextEdit::QTextEdit(const QString &,QWidget *)’ :
cannot convert parameter 1 from ‘QMainWindow *’ to ‘const QString &’
Reason: cannot convert from ‘QMainWindow *’ to ‘const QString’ No
constructor could take the source type, or constructor overload
resolution was ambiguous

C:\Documents and
Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:14: error: C2039: ‘setTextFormat’ : is not a member of ‘QTextEdit’

c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtgui\qtextedit.h:70: see
declaration of ‘QTextEdit’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:44:43+00:00Added an answer on June 2, 2026 at 7:44 pm

    Looking at the documentation, QTextEdit has two constructors,

    QTextEdit::QTextEdit ( QWidget * parent = 0 )
    

    and

    QTextEdit::QTextEdit ( const QString & text, QWidget * parent = 0 )
    

    Try changing

    QTextEdit* myTextEdit = new QTextEdit(mainWindow, "myTextEdit");
    

    to

    QTextEdit* myTextEdit = new QTextEdit(mainWindow);
    

    This won’t fix all the problems, since there are still places that use the old Qt3 API. You’ll need to make changes as necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance for your help. I have a need within an application to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.