重定向std :: cout到QTextEdit [英] redirect std::cout to QTextEdit

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

问题描述

是否可能(更重要的是--how-)将输出流重定向到QTextBox。所以如果我写 std :: cout<< test在应用程序中的任何地方重定向到一个文本框我定义?



我试过明显(其中ui.textEdit是一个指针到文本编辑框):

  std :: cout.rdbuf(ui.textEdit); 
std :: cout<< 测试;

但这不起作用。 (明显)。 - 也不会将cout重定向到qDebug工作(甚至将qDebug指向文本字段)。



我使用qt4.8 btw ...



编辑:
所以我试着在邮件列表中发布的解决方案..但现在出现访问冲突。

  class MainInterface:public QMainWindow 
{
Q_OBJECT
....
private:
QDebugStream qout



使用构造函数:

  MainInterface :: MainInterface(QWidget * parent,Qt :: WFlags flags)
:QMainWindow(parent,flags),
qout(std :: cout,ui.textEdit)
{

在成员函数中,发布了以下行: std :: cout ;& 项目加载< std :: endl;



该行现在会导致来自qscoped_pointer的访问冲突。 (我应该更详细的发布这个单独的问题吗?)



编辑:这个解决方案只是在ui.textEdit完全创建后声明qout。 / p>

解决方案

您可以将 cout 重置为您自己的 ostream 实现,它会发出信号,你挂在 append 槽。因此,您的子问题/练习是:


  1. 重定向 cout

  2. 重定向 cout 到您自己的 ostream 实现或您可以扩展

  3. emit 传送到 QTextBox






这些子主题可以在SO上使用,就我所知


Is it possible (and more importantly -how-) to redirect an output stream to a QTextBox. So that if I write std::cout << "test" anywhere in the application it gets redirected to a textbox I defined?

I tried the obvious (where ui.textEdit is a pointer to the text edit box):

std::cout.rdbuf(ui.textEdit);
std::cout << "test";

However this doesn't work. (obviously). - Nor does redirecting cout to qDebug work (or even direction a qDebug to a textfield).

I'm using qt4.8 btw...

EDIT: So I tried the solution posted in the mailing list.. However now an access violation shows up.

class MainInterface : public QMainWindow
{
    Q_OBJECT
....
private: 
    QDebugStream qout

With the constructor:

MainInterface::MainInterface(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags),
    qout(std::cout, ui.textEdit)
{

And in a member function the following line is posted: std::cout << "Project Loaded" << std::endl;

That line now causes an access violation from "qscoped_pointer". (Should I post this with more detail as seperate question?)

EDIT: well the "solution" was to only declare qout after the ui.textEdit was fully created.

解决方案

You could reset cout to your own ostream implementation which would emit signals which you hook onto the append slot. You sub-problems/exercises are therefore:

  1. redirect cout
  2. redirect cout to your own ostream implementation or one that you can extend
  3. emit signals to QTextBox


These sub-topics are available on SO, as far as I know

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

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