将std::cout重定向到QTextEdit

3

我是Qt应用程序开发的新手,希望能得到你的帮助。这是一个重新发布的问题:将std::cout重定向到QTextEdit

我试图将std::cout重定向到QTextEdit,并尝试测试以下链接中提供的示例。

参考链接1:http://lists.trolltech.com/qt-interest/2005-06/thread00166-0.html

使用Qt Creator 2.4.1来测试参考链接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();
}

我收到了以下错误信息:
C:\Documents and Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:13: 错误:C2664: 'QTextEdit::QTextEdit(const QString &,QWidget *)':不能将参数 1 从 'QMainWindow *' 转换为 'const QString &'。原因:无法将 'QMainWindow *' 转换为 'const QString',没有构造函数能够接受源类型,或者构造函数重载决策存在歧义。
C:\Documents and Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:14: 错误:C2039: 'setTextFormat':不是 'QTextEdit' 的成员。
c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtgui\qtextedit.h:70: 参见“QTextEdit”的声明。
1个回答

1

查看文档QTextEdit有两个构造函数,

QTextEdit::QTextEdit ( QWidget * parent = 0 )

并且

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

尝试更改

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

QTextEdit* myTextEdit = new QTextEdit(mainWindow);

这并不能解决所有的问题,因为仍然有一些地方使用旧的Qt3 API。您需要根据需要进行更改。


谢谢。我能够测试它,它可以正常工作。 - Ninja Joey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接