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 8721101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:10:02+00:00 2026-06-13T07:10:02+00:00

The first output in the textedit is a number 3, I don’t know why

  • 0

The first output in the textedit is a number 3, I don’t know why that number is coming form Qt::LogText. This question is based from a previous question I had asked, I’m using the same qdebugstream header file from the link below.

Redirect std::cout to a QTextEdit

The new project below is a QT Gui Application that will redirect the cout to a textedit. Also, since settextformat() is no longer a member of QTextEdit, I converted Qt::LogText into a string.

This was based on another post but I did not understand the solution.
QTextEdit::setTextFormat(Qt::LogText) does not exist anymore, what else can I use to log?.
Can someone provide more information on this?

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    ui->textEdit->setReadOnly(true);
    ui->textEdit->setText(QString("%1").arg(Qt::LogText));

    QDebugStream qout(std::cout, ui->textEdit);

    cout << "Send this to the Text Edit!" << endl;

}

MainWindow::~MainWindow()
{
    delete ui;
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qdebugstream.h"
#include "stdio.h"
#include "iostream"

using namespace std;

namespace Ui {
class MainWindow;
}

class QtextEdit;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
  • 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-13T07:10:03+00:00Added an answer on June 13, 2026 at 7:10 am

    You should use QPlainTextEdit in Qt 4.x which seems to be equivalent using Q3TextEdit(QTextEdit in Qt 3.x) with text format of Qt::LogText.

    For QPlainTextEdit version of QDebugStream might be like followings.(you may be noticed that append() mem func is renamed into addPlainText() and that’s it viola). Hope this helps.

    #ifndef QDEBUGSTREAM_H
    #define QDEBUGSTREAM_H
    
    #include <iostream>
    #include <streambuf>
    #include <string>
    #include <QPlainTextEdit>
    
    class QDebugStream : public std::basic_streambuf<char>
    {
    public:
        QDebugStream(std::ostream &stream, QPlainTextEdit* 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->appendPlainText(m_string.c_str());
    
            m_stream.rdbuf(m_old_buf);
        }
    
    protected:
        virtual int_type overflow(int_type v)
        {
            if (v == '\n')
            {
                log_window->appendPlainText(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->appendPlainText(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;
    
    
        QPlainTextEdit* log_window;
    };
    
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching a Python dictionary based on the value first, to get a key output
The first output the string not translated: {{ chart.name~'.short'|trans({}, charts) }} This one works
I have 2 output from a php function as below First output gives me
this is my first question here and if its format is not what is
I am trying to compare two text files and output the first string in
the output should process the first date as day and second as night. I've
I need to output the lines of a file where the first column's values
I'm writing my first gtk2hs+glade application, and I'm using a textview to display output
I am reading some json output... just some integers. The first NSLog outputs things
I currently am using two stylesheets to get my intended output. The first stylesheet

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.