简体   繁体   中英

Is it possible to hide qt widget window from other thread?

Is it possible to hide qt widget window from other thread?
For example if using ptr->window->hide();
from other thread it crashes with error:

Cannot send events to objects owned by a different thread...

Should signals and slots be used in this case or there are easier. alternatives?

Is it possible to hide Qt widget window from other thread?

Absolutely, all you need is to connect the signal on your worker thread with the slot on UI thread. And luckily QWidget::hide is a slot already (not even needed to wrap that in own slot).

// in WorkerQObject.h file:
class WorkerQObject : public QObject
{
   Q_OBJECT
public:
     ///
signals:
       void hideUI();
private:
     ///
};

// in WorkerQObject.cpp file:
WorkerQObject::WorkerQObject()
{
    // thread initialization; move to thread etc.
    connect(this, SIGNAL(hideUI()), pWidget, SLOT(hide()));
}

void WorkerQObject::methodOnWorkerThread()
{
    emit hideUI();
}

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