将std :: cout重定向到QTextEdit [英] Redirect std::cout to a QTextEdit

查看:538
本文介绍了将std :: cout重定向到QTextEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt应用程序开发的新手,我非常感谢您的帮助。这是
的重新发布 redirect std :: cout到QTextEdit

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

我试图将 std :: cout 重定向到 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.

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

使用Qt Creator 2.4 .1从参考链接1测试示例。

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

untiled1.pro

untiled1.pro

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

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和
Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop _- MSVC2010 _Qt_SDK__Debug.. \
不能将参数1从'QMainWindow *'转换为'const QString&'$',而不能将参数1从'QMainWindow *'转换为'QTextEdit :: QTextEdit(const QString&,QWidget * b $ b原因:无法从'QMainWindow *'转换为'const QString'无
构造函数可以采用源类型或构造函数重载
解析是不明确的

C:\Documents and Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-MSVC2010_Qt_SDK__Debug..\untitled1\main.cpp:13: error: C2664: 'QText: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和
Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop _- MSVC2010 _Qt_SDK__Debug..\untitled1\main.cpp:14:错误:C2039:'setTextFormat':不是'QTextEdit'的成员

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
'QTextEdit'的声明

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


推荐答案

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

Looking at the documentation, QTextEdit has two constructors,

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。您需要根据需要进行更改。

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.

这篇关于将std :: cout重定向到QTextEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆