简体   繁体   English

QThread 控制台应用程序 - 没有这样的信号,没有这样的插槽

[英]QThread console app - no such signal, no such slot

I recently successfully implemented multithreading to my GUI application using method by reimplementing run() virtual void, but I read there is a lot of confusion around threading in QT as even documentation doesn't present it correctly.我最近使用通过重新实现 run() virtual void 的方法成功地为我的 GUI 应用程序实现了多线程,但我读到 QT 中的线程存在很多混淆,因为即使文档没有正确显示它。 What I'm trying now is just to understand how it works and use it without any issues even not visible ones at first look.我现在正在尝试的只是了解它的工作原理并使用它没有任何问题,甚至乍一看也不明显。 I'm wondering what can be wrong in my code below, as I'm getting info from Visual Studio when starting that some errors occured and I see "QtRunWork" task returned false but did not log an error." When I ignore this and start the program I'm getting in console this: "qt.core.qobject.connect: QObject::connect: No such slot QObject::process() in (..)main.cpp:38 qt.core.qobject.connect: QObject::connect: No such signal QObject::finished() in (..)main.cpp:39 qt.core.qobject.connect: QObject::connect: No such signal QObject::finished() in (..)main.cpp:40" "我想知道下面的代码有什么问题,因为我在启动时从 Visual Studio 获取了一些错误的信息,并且我看到“QtRunWork”任务返回错误但没有记录错误。”当我忽略这个并且启动我在控制台中得到的程序:“qt.core.qobject.connect: QObject::connect: No such slot QObject::process() in (..)main.cpp:38 qt.core.qobject。连接:QObject::connect:没有这样的信号 QObject::finished() in (..)main.cpp:39 qt.core.qobject.connect: QObject::connect: 没有这样的信号 QObject::finished() in ( ..)main.cpp:40""

#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QThread>
#include <QString>
#include <QDebug>

class Worker : public QObject {
 Q_OBJECT

public slots:
    
    void process() {

        qDebug() << "Thread1";
        emit finished();
    }

signals:
    void finished();
};

class MyThread : public QThread {

public:
    MyThread();

signals:
    void finished();
};

MyThread::MyThread() {

    Worker* w = new Worker();
    w->moveToThread(this);
    connect(this, SIGNAL(started()), w, SLOT(process()));
    connect(w, SIGNAL(finished()), this, SLOT(quit()));
    connect(w, SIGNAL(finished()), w, SLOT(deleteLater()));
    connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    this->start();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyThread m;

    return a.exec();
}

Do not declare signal finished in your thread class.不要在你的线程 class 中声明信号finished QThread already has this signal https://doc.qt.io/qt-5/qthread.html#finished QThread已经有这个信号https://doc.qt.io/qt-5/qthread.html#finished

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM