qt子线程无法直接操作ui,需要发信号到主线程
需求是在数据加载的时候显示等待动画,加载结束关闭动画。
QWidget *qw = new QWidget(w); qw->setFixedSize(125,80); QProgressIndicator *pgi= new QProgressIndicator(qw); pgi->setColor(QColor(9, 126, 186)); //text QLabel *label = new QLabel(qw); label->setText(u8"数据正在加载中"); label->setAlignment(Qt::AlignLeft); //设置布局 QVBoxLayout *hLayout = new QVBoxLayout(this); hLayout->setMargin(0); hLayout->addWidget(pgi); hLayout->addWidget(label); hLayout->setAlignment(pgi, Qt::AlignCenter); qw->setLayout(hLayout); //设置位置 int x = (w->width() - qw->width()) / 2; int y = (w->height() - qw->height()) / 2; qw->move(x, y); qw->show(); QFutureWatcher<void> *pwatcher = new QFutureWatcher<void>; pgi->startAnimation(); //执行耗时任务 QFuture<void> future = QtConcurrent::run([=]() { w->readRawFolder(str); }); connect(pwatcher, &QFutureWatcher<void>::finished, this, [=]() { pgi->stopAnimation(); qw->hide(); delete qw; delete pwatcher; }); pwatcher->setFuture(future);
这样导致我主界面有些操作无法正常运行了。
QObject::connect: Cannot queue arguments of type 'QVector<int>'
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)QBasicTimer::start: Timers cannot be started from another thread
QObject::setParent: Cannot set parent, new parent is in a different thread
控制台会打印这些东西。
问了问别人是,qt子线程无法直接操作ui,需要发信号到主线程,也就是ui线程。
#qt##多线程#