PDA

View Full Version : QTextEdit from cout stream not displayed at once



nina1983
12th May 2011, 11:19
Dear all,
I search in the web and I found out that a way to redirect std::cout to a QTextEditor is to implement a class

class QDebugStream : public std::basic_streambuf<char>
The implementation of the class is in the attached file.
I made the call of the class in this way (Ui_outputView I designed myself through qtDesigner, it is just a Widget containing a QTextEditor "consoleTextEdit")


class OutputView : public QWidget, private Ui_outputView
{
Q_OBJECT

public:
OutputView(QWidget* parent = NULL){
setupUi(this);
consoleTextEdit->toPlainText();
qout = new QDebugStream(std::cout, consoleTextEdit);
}
~OutputView(){}

private:
QDebugStream *qout;

};


The code works fine, I can display the output text but I have two main questions:
1- I would like that the text is displayed not all at once: so far the text is displayed when the function that contains all the prints "cout<<" returns. I would like to see the output as in happen in the console when the cout is called.
2- is it worth perhaps to use QTextStream to catch the standard output and display it in a QTextEdit? Any hints on how to do that?

Thanks in advance for your help, if something is not clear please let me know.
Regards,
Annalisa

nina1983
8th June 2011, 17:40
Dear, the problem is getting serious now that the code takes several minutes to run the calculation.... I have the GUI in stand-by for several minutes, I would like to display the std::cout in the QTextEditor as a stream during the calculation and not all at once at the end as it is now.... is it a problem of repaint?

PLEASE HELP!!!

Annalisa

Santosh Reddy
8th June 2011, 18:06
Did you try implementing
virtual std::basic_streambuf::sync(); in QDebugStream class?

nina1983
8th June 2011, 23:18
Hi!

Thanks for your answer.
I looked a bit in the web regarding the hint you gave me and I learnt a lot about streams this evening!!! :)

Btw I think I found a simple way of solving my problem, I just add the line

QCoreApplication::processEvents();

every time log_window->append(...) is called inside QDebugStream. I found the solution on this useful link
http://www.qtcentre.org/wiki/index.php?title=Keeping_the_GUI_Responsive
it as drwbacks for multithread application but so far I think is an easy solution for my problem and I don't have to deal with multithread! and most important: IT WORKS

Thanks a lot again for your time and your help!
If you have any further comment on the problem please let me know

Regards
Annalisa

Q
CoreApplication::ProcessEvents();

I wrote it in the wrong format ...