我正在写由另一个应用程序作为一个插件,并想利用Qt的能力,一个DLL。结果 我都设置了类,编译和运行,但是没有信号被发射。 因此,它好像没有QEventLoop。

尝试1:搜索 我修改了主类的子类的QThread代替的QObject,并且在run()创建一个QEventLoop,连接所有信号/插槽,以及执行的是线程。结果 但它没有说你不能有一个QEventLoop没有QApplication的。

尝试2:结果 我修改主类(仍然继承了QThraed),以代替实例化一个QCoreApplication,连接所有信号/插槽,然后执行的是应用程序。结果, 警告说的QApplication未在主()线程创建,并且仍然不会发出信号。

我真的不知道该怎么在这里做。我显然不能创建应用程序中的一个QCoreApplication将使用我的插件,和在没有一个我不能发射信号。

我已经包括一个简单的(和可怕写的)测试应用程序,应说明我的问题:

任何帮助,将不胜感激!

main.cpp中:

#include <iostream>
#include "ThreadThing.h"
using namespace std;
int main(int argc, char *argv[])
{
    cout << "Main: " << 1 << endl;
    ThreadThing thing1;
    cout << "Main: " << 2 << endl;
    thing1.testStart();
    cout << "Main: " << 3 << endl;
    thing1.testEnd();
    cout << "Main: " << 4 << endl;
    thing1.wait(-1);
    cout << "Main: " << 5 << endl;
    return 0;
}

ThreadThing.h:

#ifndef THREADTHING_H
#define THREADTHING_H
#include <QThread>
class ThreadThing : public QThread
{
    Q_OBJECT
public:
    ThreadThing();
    virtual void run();
    void testStart();
    void testEnd();
public slots:
    void testSlot();
signals:
    void testSignal();
};
#endif//THREADTHING_H

ThreadThing.cpp:

#include "ThreadThing.h"
#include <iostream>
#include <QCoreApplication>

using namespace std;

ThreadThing::ThreadThing()
{
    cout << "Constructor: " << 1 << endl;
    this->start();
    cout << "Constructor: " << 2 << endl;
}

void ThreadThing::run()
{
    cout << "Run: " << 1 << endl;
    int i = 0;
    cout << "Run: " << 2 << endl;
    QCoreApplication* t = new QCoreApplication(i, 0);
    cout << "Run: " << 3 << endl;
    connect(this, SIGNAL(testSignal()), this, SLOT(testSlot()), Qt::QueuedConnection);
    cout << "Run: " << 4 << endl;
    t->exec();
    cout << "Run: " << 5 << endl;
}

void ThreadThing::testStart()
{
    cout << "TestStart: " << 1 << endl;
    emit testSignal();
    cout << "TestStart: " << 2 << endl;
}

void ThreadThing::testEnd()
{
    cout << "TestEnd: " << 1 << endl;
    this->quit();
    cout << "TestEnd: " << 1 << endl;
}

void ThreadThing::testSlot()
{
    cout << "TEST WORKED" << endl;
}

输出:

Main: 1
Constructor: 1
Constructor: 2
Main: 2
TestStart: 1
TestStart: 2
Main: 3
TestEnd: 1
TestEnd: 1
Main: 4
Run: 1
Run: 2
WARNING: QApplication was not created in the main() thread.
Run: 3
Run: 4
有帮助吗?

解决方案

您的的创建QCoreApplication或QApplication的和您的的做在主线程。

这并不意味着你不能把为代码在你的插件...除非应用程序始终运行在自己的线程每个插件。

如果该应用程序的的这样做,那么你可以尝试挂钩到任何本地事件循环的应用程序使用,并安排其在主线程中调用一些功能插件。

其他提示

我已经成功创建QCoreApplication并在后台线程上运行它。这是的的标准实现,但可以为简单的信号/槽的功能工作。我这样做是为了有大笔遗产的Qt代码库的本地iOS应用。

//I really don't do anything but run on a background thread
class MyQtAppForBackgroundThread : public QCoreApplication 
{
    Q_OBJECT
    ...
}

 //iOS specific code here...
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
        // This spawns a QCoreApplication on a background thread in an attempt to create something that can
        // queue signals across threads
        qtApp = new MyQtAppForBackgroundThread(smArgc, smArgv);
        qtApp->exec();

    });

信号烧制在那里他们被连接会被抓住在同一线程上。要在不同的线程捕获的信号,必须创建并在那里创建了信号的线程上轮询QEventLoop。

//Fire me periodically on the thread the signals and slots were connected
QEventLoop loop;
loop.processEvents( QEventLoop::ExcludeUserInputEvents, 500 );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top