C++ 重新实现QThread的start()

C++ 重新实现QThread的start(),c++,qt,C++,Qt,我正在用Qt练习线程。我重新实现了run()(尽管不建议这样做),一切都很好 现在我想给run()添加更多功能,让它传递一个变量:run(inti)。 此外,我希望调用run的start(),将变量传递给run(inti):start(intj) 我认为用以下方式重新实现start应该是可行的:(Zaehler是QThread) void Zaehler::start(int Zaehler索引) { 运行(ZaehlerIndex), 终止(); } 但事实并非如此。我的GUI在启动线程时冻

我正在用Qt练习线程。我重新实现了
run()
(尽管不建议这样做),一切都很好

现在我想给
run()
添加更多功能,让它传递一个变量:
run(inti)
。 此外,我希望调用run的
start()
,将变量传递给
run(inti)
start(intj)


我认为用以下方式重新实现start应该是可行的:(Zaehler是QThread)

void Zaehler::start(int Zaehler索引)
{
运行(ZaehlerIndex),
终止();
}

但事实并非如此。我的GUI在启动线程时冻结


问题: 我知道,应该避免弄乱启动和运行,但有没有办法做到这一点?我做错什么了吗

备注: 我查阅了qthread.cpp以了解
start()
是如何实现的,但我只找到了


\sa run(),terminate()
已注释掉!所以它实际上不应该起作用

如果要在线程内启动函数并传递一些参数,请使用此函数,我建议使用以下方法:

class MyWorker: public QThread
{
  Q_OBJECT
public:
    MyWorker()
    {
        start();
    }

    virtual ~MyWorker()
    {
        quit();
        wait();
    }

    // your function to call inside thread
    Q_INVOKABLE void doSomeWork(int data)
    {
        // check if we called from another thread
        if (QThread::currentThread() != this)
        {
            // re-call self from very own thread
            QMetaObject::invokeMethod(this, "doSomeWork",
                                      Qt::BlockingQueuedConnection,
            // or Qt::QueuedConnection if you want to start it asynchronously
            //  and don't wait when work finished
                                      Q_ARG(int, data));
            return;
        }

        // this will be started within your thread
        // some useful work here
    }
};
如果您的线程没有run()方法,则将使用QEventLoop,您可以在线程上下文中调用您的方法


您还可以将方法声明为slot,并使用排队连接调用它,而不是Q_INVOKABLE。

类似的方法如何

class Worker
{
    //note, only pass parameters by copy here.
    public: 
        Worker(int param) : myParam(param) {}

    public slots:
        void process()
        {
            //do something with myParam here, it will run in the thread.
        }

    signals:
        void finished();
        void error(QString err);

    private:
        int myParam;
};
然后可以使用“moveToThread”将其连接到线程对象,如下所示:

有关更多信息和Qt中线程使用的简短教程,请参见此处:

你的问题解决了吗?
QThread* thread = new QThread;
Worker* worker = new Worker(5);
worker->moveToThread(thread);
connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();