如何从另一个线程关闭对话框?QT
How I can close dialog, from another thread? Qt
我想以这种方式处理我的按钮:
- 更改标签上的文本(如"请等待...",例如"请等待...")
- 从数据库下载一些数据
- 下载完成后,关闭对话框此按钮在哪里。
当我这样做时:
void LoadingDialog::on_pushButton_clicked()
{
m_ui->labelStatus->setText("Pobieranie wysyłek...");
if(m_methodToDo == MethodToDo::LoadShipment)
{
if(DataManager::getManager()->loadShipments())
{
this->close();
}
}
}
标签没有更改文本,是滞后的几秒钟(下载k记录很少),并且对话框正在关闭。
当我尝试一下时:
void LoadingDialog::changeStatus(QString status)
{
m_ui->labelStatus->setText(status);
}
bool LoadingDialog::load()
{
if(m_methodToDo == MethodToDo::LoadShipment)
{
if(DataManager::getManager()->loadShipments())
{
this->close();
}
}
}
void LoadingDialog::on_pushButton_clicked()
{
QFuture<void> future3 = QtConcurrent::run([=]() {
changeStatus("Pobieranie wysyłek..."); // "Downloading.."
});
QFuture<void> future = QtConcurrent::run([=]() {
load();
});
}
标签具有更改文字 - 还可以是滞后的几秒 - 还可以但是对话框没有关闭,我的应用程序会引发例外:
Cannot send events to objects owned by a different thread. Current thread 229b1178. Receiver 'Dialog' (of type 'LoadingDialog') was created in thread 18b00590
任何建议?
首先,Changestatus没有阻止,因此请勿在另一个线程上运行。另一方面,如果您想从另一个线程调用插槽,则可以使用QMetaObject::invokeMethod()
:
bool LoadingDialog::load()
{
if(m_methodToDo == MethodToDo::LoadShipment)
if(DataManager::getManager()->loadShipments())
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
}
void LoadingDialog::on_pushButton_clicked()
{
changeStatus("Pobieranie wysyłek..."); // "Downloading.."
QFuture<void> future = QtConcurrent::run([=]() {
load();
});
}
相关文章:
- Qt为所有对话框设置通用背景
- 如何抓取指向Qt中弹出对话框的指针,该对话框阻止了QTest中的UI线程
- Qt 忙处理对话框
- Qt从主窗口打开对话框
- 模型对话框移动到主窗口后面的Qt错误
- 用静态版本的QT编译时,文件对话框崩溃
- QT如何显示,请等待对话框,而插座正在等待响应
- Qt - 创建主窗口前的非模态对话框
- 异步显示 Qt 对话框
- 使两个QT对话框相互关闭
- QT-如何处理对话框的内存管理
- 在我关闭刚开始的孩子对话框并进入父母对话框之后,我的QT桌面应用程序不断崩溃
- 在Qt中创建类,其中QDialog内置特定对话框
- 在QT对话框上布置图像
- 如何从另一个线程关闭对话框?QT
- 注销侏儒会话时如何从Qt应用程序执行"confirm" gnome会话管理器的对话框(不使用QSessionManager)
- 无法更新 qt 对话框中的按钮
- 无法从对话框中关闭应用程序(Qt 应用程序)
- QT对话框窗口禁用ewerningOntonop会导致窗口关闭
- Link2019:调用对话框qt的未解析的外部符号