Hello,
Premise: about 6 months ago I changed my job, so I inherited some pretty big C++ project using qt libraries and I started learning qt that moment.

My issue: the project I'm working on consists of several sub-projects and most of them make use of multi-threading to handle asynchronous events from the GUI and from connected devices.
The guy who developed the project in the first place has implemented a message handler for qInfo, qDebug and qCritical in the following way:
main.cpp
Qt Code:
  1. Console * console;
  2.  
  3. void messageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg) {
  4. switch (type) {
  5. case QtDebugMsg:
  6. // formatting for debug messages
  7. break;
  8.  
  9. case QtInfoMsg:
  10. // formatting for info messages
  11. break;
  12.  
  13. case QtCriticalMsg:
  14. // formatting for critical messages
  15. break;
  16.  
  17. default:
  18. break;
  19. }
  20.  
  21. if (console) {
  22. console->addLine(msg);
  23. }
  24. }
  25.  
  26. int main(int argc, char * argv[]) {
  27. QApplication a(argc, argv);
  28. qInstallMessageHandler(QtMessageHandler(messageHandler));
  29.  
  30. console = new Console();
  31. MainWindow mainwindow;
  32.  
  33. mainwindow.showMaximized();
  34. mainwindow.setConsole(console);
  35. a.exec();
  36. }
To copy to clipboard, switch view to plain text mode 
console.ui declares a QListWidget * consoleWidget for the class Console.
console.h:
Qt Code:
  1. namespace Ui {
  2. class Console;
  3. }
  4.  
  5. class Console : public QWidget {
  6. Q_OBJECT
  7.  
  8. public:
  9. explicit Console(QWidget * parent = 0);
  10.  
  11. void addLine(QString line);
  12.  
  13. private:
  14. Ui::Console * ui;
  15. };
To copy to clipboard, switch view to plain text mode 
console.cpp:
Qt Code:
  1. Console::Console(QWidget * parent) : QWidget(parent), ui(new Ui::Console) {
  2. ui->setupUi(this);
  3. }
  4.  
  5. void Console::addLine(QString line) {
  6. ui->consoleWidget->addItem(line);
  7. ui->consoleWidget->scrollToBottom();
  8. }
To copy to clipboard, switch view to plain text mode 

So the aim is to redirect all of the messages, properly formatted, to a console.
At some point I noticed that disabling the addLine on the console prevented a lot of extemporary crashes of the application, preceded by some warnings complaining about QTimers being started or stopped from different threads.
So I decided to disable the console with an ifdef, hoping to find a solution later on, and the warnings and crashes actually ceased.

Today I realized the problem may be due to the fact that qInfo, qDebug and qCritical are called all over the code, so they actually allow pieces of code running in different threads to directly call a method (addLine) on a GUI object (Console).
So I wanted to try and replace console->addLine(msg); within messageHandler with a signal emit to keep safe the GUI thread, but I don't know how to do it, since messageHandler is a non-member function so, afaik, it cannot emit signals. Moreover, I think that if I created a class and made messageHandler one of its members I could experience problems due to having the method of a single object called in different threads.

So lastly, my questions:
1) Is my analysis of the problem correct or the crashes may be due to something else?
2) Is the requirement of redirecting all messages to a console something intrinsically wrong and should I give up to a different solution for my logs?
3) If no, what would be the proper implementation to obtain such behaviour?

Thanks for your help and best regards!