I encountered a strange error in my Qt 5.7 application (on Windows 10), and there are nowhere to find the usual culprits for this behavior:
- An object that is moved has a parent - this is certainly not the case.
- Trying to pull an object into a stream instead of clicking it is the cause of the error, but I have no idea where it comes from
Full error message
QObject :: moveToThread: current thread (0x2afcca68) is not a thread object (0x34f4acc8). Unable to jump to target topic (0x34f4adc8)
QObject :: setParent: cannot set the parent element, the new parent is in another thread
as well as my code:
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 (alias main.qml ):
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) {
this->airWheelRecognizer = new QAirWheelGestureRecognizer();
this->airWheelType = QGestureRecognizer::registerRecognizer(airWheelRecognizer);
grabGesture(airWheelType);
grabGesture(Qt::SwipeGesture);
grabGesture(Qt::TapGesture);
this->deviceThread = new QThread(this);
this->deviceWorker = new DeviceMapper(this, Q_NULLPTR);
this->deviceWorker->init();
this->timer = new QTimer();
this->timer->setTimerType(Qt::PreciseTimer);
this->timer->setInterval(5);
this->timer->moveToThread(this->deviceThread);
this->deviceWorker->moveToThread(this->deviceThread);
createConnections();
this->deviceThread->start();
QTimer::singleShot(0, this->deviceWorker, &(this->deviceWorker->slotToggleConnection));
QTimer::singleShot(0, this->deviceWorker, &(this->deviceWorker->slotToggleRun));
this->show();
}
CustomQuickWidget::~CustomQuickWidget()
{
if (this->deviceThread) {
this->deviceThread->quit();
this->deviceThread->wait();
}
}
void CustomQuickWidget::createConnections()
{
connect(this->timer, SIGNAL(timeout()),
this->deviceWorker, SLOT(slotRetrieveData()));
connect(this->deviceThread, SIGNAL(started()),
this->timer, SLOT(start()));
connect(this->deviceThread, SIGNAL(finished()),
this->deviceWorker, SLOT(deleteLater()));
connect(this->deviceThread, SIGNAL(finished()),
this->deviceThread, SLOT(deleteLater()));
}
bool CustomQuickWidget::event(QEvent* event) {
if (event->type() == QEvent::Gesture) {
bool res = gestureEvent(static_cast<QGestureEvent*>(event));
return res;
}
return QWidget::event(event);
}
, , . , ( DeviceMapper) . ( QThread), .
, , , :
- ,
this->timer->moveToThread(this->deviceThread); - - , subdirs - ( ), - , .
- QQuickWidget ( QWidget) QML. QML, QQuickWidget, , "".
cout << this->deviceWorker->thread()->currentThreadId() << endl;
cout << this->thread()->currentThreadId() << endl;
this->deviceWorker->moveToThread(this->deviceThread);,
0x18b0
0x18b0
, moveToThread(...) , QThread. moveToThread(...) , - .
UPDATE:
, , .