简体   繁体   English

在另一个线程中的回调中显示状态栏上的消息

[英]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. 它是一个用c ++编写的控制台应用程序,带有Qt GUI。 When something changes in the connections status, the connection handler calls a callback (in some gui related object), which updates the GUI. 当连接状态发生变化时,连接处理程序会调用回调(在某些gui相关对象中),这会更新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). 我可以在我的QGraphicsScene上绘制,但是当我尝试使用QStatusBar的showMessage方法时,有时会立即崩溃,有时它会工作直到几次调用(但是超时过后消息不会消失)。 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. 我认为这意味着不会从GUI线程调用showMessage。 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. 您不能从非GUI线程进行直接GUI调用。 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. 在实现连接处理程序的类中创建一个信号,并将其连接到状态栏的showMessage()槽。 When you want to show something on the status bar, emit the signal and pass the appropriate message. 如果要在状态栏上显示某些内容,请发出信号并传递相应的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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