4

我在我的 Qt 5.7(在 Windows 10 上)应用程序中遇到了一个奇怪的错误,并且找不到这种行为的常见罪魁祸首:

  • 被移动的对象有一个父对象——当然不是这样
  • 试图将对象拉到线程而不是推送它 - 这是错误的原因但是我不知道它来自哪里

完整的错误信息是

QObject::moveToThread: 当前线程 (0x2afcca68) 不是对象的线程 (0x34f4acc8)。无法移动到目标线程 (0x34f4adc8)

QObject::setParent:无法设置父级,新父级在不同的线程中

这也是我的代码:

主文件

#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();
}

mainmain.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, &(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)); // Not important so not included as code here
        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实例化的同一个线程之前。在返回相同的结果之后打印线程 ID,moveToThread(...)但这是由于未能将对象正确移动到另一个线程而导致的。


更新:

错误消息仅在以发布模式构建时出现,但无论构建类型如何,我的错误仍然存​​在。

4

1 回答 1

1

我已经设法通过查明问题的发生时间来解决我的问题。

上周末,我正在编写的应用程序突然开始工作,所以尽管这让我很困扰,为什么在此之前发生的所有事情我都顺其自然。我既没有更改库的代码(除了我的代码中的一些注释显然不会影响代码本身)也没有更改C++我的QML应用程序的代码。我所做的只是我的更改,QML但实际上与C++下面的代码无关。我唯一改变的是构建类型。但是我上周并没有真正注意到这一点。

昨天我开始做一个新项目。在第一次运行之后,我遇到了同样的问题。它让我发疯。所以我开始分析我的代码(@Kuba Ober,对不起,伙计,但是不可能发布完整的代码甚至是库的一小部分,否则我会这样做(即使它是几百行实际代码(不包括诸如注释和空行之类的东西))。我检查并仔细检查了父子关系,但找不到任何可以给我一点提示的东西,甚至是什么时候以及为什么会发生这种情况。我还分析了堆栈以尽我所能,但一切都是徒劳的。

然后它打动了我......我在上面提到过我之前的项目在更改其构建类型后突然开始工作。的确,这是我处境中所有邪恶的根源。我将我的库添加到我的项目的方式(不包括与库一起属于同一subdir项目的初始库)是在我的新项目的根目录中创建一个文件夹,libs并将相关内容复制到其中。现在,在我完成了我的库并进行了一些测试之后,我显然决定切换到release build但是我将库构建模式复制到项目构建模式releasedebug. 因此,经过几次重建并在这里和那里复制库后,我发现混合使用库的应用程序的构建类型和库本身会导致这个问题。

我知道混合构建类型是一个坏主意,我通常不会这样做,但这次它只是让我忘记了,完全是个意外。我不知道当 X 构建类型的应用程序和 Y 构建类型的库混合时内部发生了什么,但在我的情况下结果是我在这个线程中发布的错误。

感谢所有的帮助。我从你的评论中学到了很多!尽管在我的情况下不需要调试,但我很感激你。:)

于 2016-08-16T07:42:30.483 回答