我要以这种方式处理按钮:
当我这样做时:
void LoadingDialog::on_pushButton_clicked()
{
m_ui->labelStatus->setText("Pobieranie wysyłek...");
if(m_methodToDo == MethodToDo::LoadShipment)
{
if(DataManager::getManager()->loadShipments())
{
this->close();
}
}
}
标签没有更改文本,滞后几秒钟(正在下载几千条记录)并且对话框正在关闭。
当我尝试这样做时:
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
有什么建议吗?
答案 0 :(得分:0)
首先,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();
});
}