大佬教程收集整理的这篇文章主要介绍了c – Qt无法将目标移动到线程,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
>被移动的对象具有父对象 – 大多数情况并非如此
>尝试将对象拉到线程而不是推送它 – 这是错误的原因但是我不知道它来自哪里
完整的错误消息是
这里也是我的代码:
@H_561_5@main.cpp中
#include <QApplication> #include <QQuickItem> #include "CustomQuickWidget.h" int main(int argc,char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc,argv); const QUrl source = QUrl(Qlatin1String("qrc:/main")); CustomQuickWidget widget(sourcE); return app.exec(); }@H_561_5@main(main.qml的别名):
// You can put any random QML content in this case really as long as it doesn't create a window since the CustomQuickWidget does that. Rectangle { id: window visible: true width: 600 height: 480 }
CustomQuickWidget.cpp
#include "CustomQuickWidget.h" #include <QQuickItem> CustomQuickWidget::CustomQuickWidget(const QUrl &source,QWidget *parent) : QQuickWidget(source,parent) { // Setup the recognizer this->airWheelRecognizer = new QAirWheelGestureRecognizer(); this->airWheelType = QGestureRecognizer::registerRecognizer(airWheelRecognizer); // and turn on grabbing for all the supported gestures grabGesture(airWheelTypE); grabGesture(Qt::SwipeGesturE); grabGesture(Qt::TapGesturE); // Create thread and device worker this->deviCEThread = new QThread(this); this->deviceWorker = new DeviceMapper(this,Q_NULLPTR); // NOTE: this here is NOT for parent. The constructor's signature for this class is: DeviceMapper(QObject* receiver,QList<Qt::GestureType>* gesturEIDs,QObject* parent = Q_NULLPTR) this->deviceWorker->init(); // Create timer that will trigger the data retrieval slot upon timeout this->timer = new QTimer(); this->timer->setTimerType(Qt::PreciseTimer); this->timer->seTinterval(5); // Move timer and device mapper to other thread this->timer->moveToThread(this->deviCEThread); this->deviceWorker->moveToThread(this->deviCEThread); // FIXME For unkNown reason: QObject::moveToThread: Current thread (...) is not the object's thread. CAnnot move to target thread // Connect widget,timer and device mapper createConnections(); // Run thread this->deviCEThread->start(); // Connect device and start data retrieval QTimer::singleShot(0,this->deviceWorker,&(this->deviceWorker->slotToggleConnection)); QTimer::singleShot(0,&(this->deviceWorker->slotToggleRun)); this->show(); } CustomQuickWidget::~CustomQuickWidget() { if (this->deviCEThread) { this->deviCEThread->quit(); this->deviCEThread->wait(); } } void CustomQuickWidget::createConnections() { connect(this->timer,SIGNAL(timeout()),SLOT(slotRetrieveData())); connect(this->deviCEThread,SIGNAL(started()),this->timer,SLOT(start())); connect(this->deviCEThread,SIGNAL(finished()),SLOT(deleteLater())); connect(this->deviCEThread,this->deviCEThread,SLOT(deleteLater())); } bool CustomQuickWidget::event(QEvent* event) { if (event->type() == QEvent::GesturE) { bool res = gestureEvent(static_cast<QGestureEvent*>(event)); // Not important so not included as code here return res; } return QWidget::event(event); }
正如你所看到的,我在这里有一个典型的工人线程.我确保我的工作人员(这里是DeviceMapper)没有父母.它也在我的小部件中实例化(QThread也在其中创建),但随着计时器一起移动到线程.
>当这个> timer-> moveToThread(this-> deviCEThread)时没有这样的错误;叫做
>这个相同的代码在另一个项目中没有任何问题,这是一个子项目 – 一个子项目创建共享库(我也在这个项目中使用),另一个 – 使用该库的应用程序.
我的其他应用程序和这个应用程序之间的唯一区别是使用QQuickWidget(而不是QWidget)和QMl.我是QML的新手,这也是我的第一个QQuickWidget,所以我可能会错过一些需要“激活”的明显设置.
我还补充道
cout << this->deviceWorker->thread()->currentThreadId() << endl; cout << this->thread()->currentThreadId() << endl;
就在此之前 – > deviceWorker-> moveToThread(this-> deviCEThread);我得到了
0x18b0 0x18b0
这意味着在moveToThread(…)之前,我的对象属于QThread实例化的同一个线程.在moveToThread(…)之后打印线程ID会返回相同的结果,但由于无法将对象正确地移动到另一个线程,因此这是预期的.
更新:
在上周末,我写的应用程序突然开始工作,所以即使它困扰我为什么在此之前发生的所有事情我都让它成为现实.我既没有改变库的代码(除了我的代码中的一些注释,这显然不会影响代码本身),也没有改变我的QML应用程序的C代码.我所改变的只是我的QML,但其实际上并没有与下面的C代码相关.我唯一改变的是构建类型.但是上周我没注意到.
昨天我开始研究一个新项目.在第一次运行后,我遇到了同样的问题.它让我疯了.所以我开始分析我的代码(@Kuba Ober,对不起伙伴,但发布完整的代码甚至是一小部分库是不可能的,否则我会做到这一点(即使它是几百行的实际代码(排除评论和空行之类的东西)).我检查并仔细检查了亲子关系,但找不到任何可以给我提供一个小提示的时间和原因.我还分析了堆栈到我最好的能力,但都是徒劳的.
然后它让我感到震惊……我上面已经提到过,我之前的项目在更改其构建类型后突然开始工作.事实上,在我的情况下,这是所有邪恶的根源.我将库添加到项目中的方式(不包括最初的库和库是同一个子项目的一部分)的方法是在我的新项目的根目录中创建一个名为libs的文件夹,并将相关内容复制到其中.现在,在我完成了我的库并进行了一些测试之后,我显然决定切换到发布版本.但是,我将发布模式下的库构建复制到了调试模式下的项目构建.因此,经过几次重建并在此处复制库后,我发现混合使用库和库本身的应用程序的构建类型会导致此问题.
我知道混合构建类型是一个坏主意,我通常不这样做,但这次它只是让我不知所措,完全是一次意外.当我使用X构建类型的应用程序和具有Y构建类型的库混合时,我不知道内部发生了什么,但我的情况中的结果是我在此线程中发布的错误.
以上是大佬教程为你收集整理的c – Qt无法将目标移动到线程全部内容,希望文章能够帮你解决c – Qt无法将目标移动到线程所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。