我想以这种方式处理我的按钮:
当我这样做:
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();
});
}