I want to use qInstallMessageHandler(handler)
to redirect the qDebug
to QTextEdit
.
I define a handler function in a class:
void Spider::redirect(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
console->append(msg);
}
and call qInstallMessageHandler(redirect)
in the constructor of the class (Spider).
But, when i compile this program, i got a error:
cannot convert 'Spider::redirect' from type 'void (Spider::)(QtMsgType, const QMessageLogContext&, const QString&)' to type 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}'
If i define the handler function in global, it's ok.
I can't figure out the difference between these two behaviors.
Non-static class method and a global function have different signatures. You cannot use a non-static method as a function.
I really like having this debugging ability around. I've done it a few times on the last couple projects I've worked on. Here are the relevant code snippets.
in mainwindow.h, inside your
MainWindow
class, underpublic
in mainwindow.cpp, outside of any function
in MainWindow constructor
in main.cpp, above the
main()
inside main() in main.cpp, before initializing
QApplication
instance.Note: This works wonderfully for any single threaded application. Once you start using
qDebug()
outside your GUI thread, you will crash. You then need to create aQueuedConnection
from any threaded function (anything not running on your GUI thread), to connect to your instance ofMainWindow::s_textEdit
, like so:If you end up using
QDockWidget
s and making use of theQMenu
, there are some additional cool things that you can do. The end result it a very user-friendly, easy-to-manage console window.Hope that helps.