头文件相关
Q_INVOKABLE int test();
public slots:
void slot_update(QString text);
//源文件
int MainPage::test()
{
QMessageBox::information(this,"","test");
return 10086;
}
void MainPage::slot_update(QString text)
{
ui->label->setText(text);
QMessageBox::information(this,"","update");
}
void MainPage::on_pushButton_clicked()
{
QtConcurrent::run([](MainPage* page){
//todo: concurrent
//update ui
QMetaObject::invokeMethod(page,"slot_update",Q_ARG(QString ,QString("456")));
int retVal=9;
QMetaObject::invokeMethod(page,"test",Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, retVal));
qDebug()<< __LINE__ << __FUNCTION__ << __FILE__<<retVal;
},this);
}
说明
默认是Qt::AutoConnection连接
如要返回值,需要设为Qt::BlockingQueuedConnection
如不是槽函数,需要加上Q_INVOKABLE