QDebug output in a QTextEdit
-
I'm new in Qt and I need to display the Qdebug output for the hole application into a QTextEdit , I found @customMessageHandler and @qInstallMessageHandler but the problem is that @customMessageHandler is static so I can't redirect my output to QTextEdit from this function, please help me if you have any idea.
-
Hi and welcome to DevNet,
You might implement a singleton class that contains a pointer to a QTextEdit and use it to write your output:
@
class LoggerSingleton {
// Add all missing constructor etc... to have singletonprivate:
QPointer<QTextEdit> _textEdit;
};LoggerSingleton::setTextEdit(QTextEdit *textEdit)
{
_textEdit = textEdit;
}
LoggerSingleton::writeMessage(const QString& message)
{
if (_textEdit) {
_textEdit->append(message);
}
}
@This is just a brief "mockup" of the idea. I'll let you implement the singleton class properly
Don't forget to put proper locking when access the QTextEdit.
-
Have a look at the threading documentation. Basically you have to use a mutex each time you want to access the _textEdit since it's shared.
If you only want to show the debug message, i'd suggest to also make it read only.
-
this is the code for now:
@#include<Presentation/LoggerSingleton.h>bool LoggerSingleton::instanceFlag = false;
LoggerSingleton* LoggerSingleton::single = NULL;LoggerSingleton::LoggerSingleton()
{
}LoggerSingleton *LoggerSingleton::getInstance()
{
if(! instanceFlag)
{
single = new LoggerSingleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}LoggerSingleton::~LoggerSingleton()
{
instanceFlag = false;
}void LoggerSingleton::setTextEdit(QTextEdit *textEdit)
{
_textEdit = textEdit;
}void LoggerSingleton::writeMessage(const QString &message)
{
if (_textEdit) {
_textEdit->append(message);
}
}
@and
@
void HomePage::customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString txt;
switch (type) {
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;case QtWarningMsg: txt = QString("Warning: %1").arg(msg);; break; case QtCriticalMsg: txt = QString("Critical: %1").arg(msg); break; case QtFatalMsg: txt = QString("Fatal: %1").arg(msg); abort(); } logger=LoggerSingleton::getInstance();
//Set my QTextEdit
logger->setTextEdit(log);
//Write the message to QTextEdit
logger->writeMessage(txt);
}
@
in the constructor of the HomePage
@ qInstallMessageHandler(customMessageHandler); @ -
Your implementation could be simpler:
@
LoggerSingleton *LoggerSingleton::getInstance()
{
if(!_single) {
_single = new LoggerSingleton();
}
return _single
}@There's no need for a flag.
Don't forget to add locking when accessing the QTextEdit
Without knowing your program, it's difficult to say why they don't appear "real time". Messages comes from various sources (thinking threads), your main thread (AKA gui thread) might be busy doing something else etc...
-
I recommend you change the global variables for static one.
[code]
LoggerSingleton *LoggerSingleton::getInstance()
{
static LoggerSingleton *_single = LoggerSingleton();
if(!_single) {
_single = new LoggerSingleton();
}
return _single
}[/code]
Or my favorite, return a reference rather than a pointer.
[code]
LoggerSingleton & LoggerSingleton::getInstance()
{
static LoggerSingleton _single;
return _single
}
[/code]