c – Qt无法将目标移动到线程
我在Qt 5.7(在
Windows 10上)应用程序中遇到了一个奇怪的错误,并且无法找到这种行为的常见罪魁祸首:
>被移动的对象具有父对象 – 大多数情况并非如此 完整的错误消息是
这里也是我的代码: 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(); } 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构建类型的库混合时,我不知道内部发生了什么,但我的情况中的结果是我在此线程中发布的错误. 谢谢你的帮助.我通过你的评论学到了很多东西!即使在我的情况下调试不是必要的,你也要感谢. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |