【问题标题】:Unknown ouput with qdebugstream and qtextedit带有 qdebug 流和 qtextedit 的未知输出
【发布时间】:2012-10-19 16:59:28
【问题描述】:

textedit 中的第一个输出是数字 3,我不知道为什么这个数字来自 Qt::LogText。这个问题基于我之前提出的问题,我使用的是来自下面链接的相同 qdebugstream 头文件。

Redirect std::cout to a QTextEdit

下面的新项目是将 cout 重定向到 textedit 的 QT Gui 应用程序。此外,由于 settextformat() 不再是 QTextEdit 的成员,我将 Qt::LogText 转换为字符串。

这是基于另一个帖子,但我不明白解决方案。 QTextEdit::setTextFormat(Qt::LogText) does not exist anymore, what else can I use to log?。 有人可以提供更多信息吗?

ma​​inwindow.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;
}

主窗口.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

【问题讨论】:

    标签: c++ qt iostream


    【解决方案1】:

    您应该在 Qt 4.x 中使用 QPlainTextEdit,这似乎等同于使用 Q3TextEdit(Qt 3.x 中的 QTextEdit)和 Qt::LogText 的文本格式。

    对于 QPlainTextEdit 版本的 QDebugStream 可能如下所示。(您可能会注意到 append() mem func 已重命名为 addPlainText() ,仅此而已)。希望这会有所帮助。

    #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多