gpt4 book ai didi

c++ - 是否可以从其他线程隐藏 qt 小部件窗口?

转载 作者:行者123 更新时间:2023-11-28 01:51:29 25 4
gpt4 key购买 nike

是否可以从其他线程隐藏qt widget窗口?
例如,如果使用 ptr->window->hide();
从其他线程它因错误而崩溃:

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

在这种情况下应该使用信号和插槽还是更容易。替代方案?

最佳答案

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

当然,您只需将工作线程上的信号连接到 UI 线程上的插槽即可。幸运的是QWidget::hide已经是一个插槽(甚至不需要将其包装在自己的插槽中)。

// 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();
}

关于c++ - 是否可以从其他线程隐藏 qt 小部件窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com