重定向std :: cout到QTextEdit [英] redirect std::cout to 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
槽。因此,您的子问题/练习是:
- 重定向
cout
- 重定向
cout
到您自己的ostream
实现或您可以扩展
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 ownostream
implementation which wouldemit
signals which you hook onto theappend
slot. You sub-problems/exercises are therefore:
- redirect
cout
- redirect
cout
to your ownostream
implementation or one that you can extendemit
signals toQTextBox
These sub-topics are available on SO, as far as I know
这篇关于重定向std :: cout到QTextEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!