简体   繁体   中英

Show message on statusbar from a callback in a different thread

I'm trying to show some connected/disconnected messages on the status bar in my application. It is a console application written in c++, with a Qt GUI. When something changes in the connections status, the connection handler calls a callback (in some gui related object), which updates the GUI. I can draw on my QGraphicsScene, but when i try to use showMessage method of QStatusBar, sometimes it crash immediately, sometimes it works until a few calls (but the message isn't disappear after the timeout elapsed). I get this error message:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QStatusBar(0xae55feb0), parent's thread is QThread(0xb3e006f0), current thread is QThread(0xb0c00478)
QObject::startTimer: QTimer can only be used with threads started with QThread
QPixmap: It is not safe to use pixmaps outside the GUI thread

I think that means the showMessage is not called from the GUI thread. Then how could i display connection status changes on the status bar?

You are not allowed to make direct GUI calls from non-GUI threads. What you need to do instead is set up a signal/slot connection. Create a signal in the class where you implement your connection handler, and connect it to the showMessage() slot of your status bar. When you want to show something on the status bar, emit the signal and pass the appropriate message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM