Qt-Signal未在虚拟机中连接

时间:2018-04-04 09:54:58

标签: c++ qt signals slots

我使用GCC 4.8.5和Qt 4.8.5在RedHat 7.4上开发和编译。代码必须静态链接。然后在运行Scientific Linux版本6.7的虚拟机上执行。 memcpy-Wrap用于防止对较新的GLIBC> = 2.4

的依赖

我有以下MWE:

#include <iostream>
#include <unistd.h>
#include <QtCore>
#include <QThread>

__asm__(".symver memcpy, memcpy@GLIBC_2.2.5");
extern "C" {
void *__wrap_memcpy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); }
}

class Worker : public QThread {
    void run() {
        std::cout << "WORKER: Started." << std::endl;
        QObject::connect(this, SIGNAL(finished()), QCoreApplication::instance(), SLOT(quit()));
        int i=0;
        while(i++<3) {
            std::cout << "WORKER: I am running." << std::endl;
            usleep(1e6);
        }
        std::cout << "WORKER: Finished." << std::endl;
    }
};

int main(int argc, char** argv) {

    std::cout << "MAIN: Init QCoreApplication" << std::endl;
    QCoreApplication qtApplication(argc, argv);

    std::cout << "MAIN: Init Worker" << std::endl;
    Worker myWorker;
    myWorker.start();

    std::cout << "MAIN: Start Event-Loop." << std::endl;
    qtApplication.exec();

    std::cout << "MAIN: Event-Loop finished." << std::endl;
    return 0;
}

此代码使用

在RedHat-System上编译
g++ -I$QTD/mkspecs/linux-g++ -I$QTD/include -I$QTD/include/QtCore -o mwe mwe.cpp  -Wl,--wrap=memcpy -L$QTD/lib/ -lQtCore -lQtNetwork -lglib-2.0 -lrt -lpthread -ldl -lz

其中$ QTD持有我的Qt-4.8.5安装。

在Red-Hat系统上预期和观察到以下行为:

MAIN: Init QCoreApplication
MAIN: Init Worker
MAIN: Start Event-Loop.
WORKER: Started.
WORKER: I am running.
WORKER: I am running.
WORKER: I am running.
WORKER: Finished.
MAIN: Event-Loop finished.

在Scientific-Linux-System上观察到以下行为:

MAIN: Init QCoreApplication
MAIN: Init Worker
MAIN: Start Event-Loop.
WORKER: Started.
WORKER: I am running.
WORKER: I am running.
WORKER: I am running.
WORKER: Finished.

然后应用程序永远不会完成。

似乎在Red-Hat系统中,来自worker-thread的完成信号连接到核心应用程序中的quit-slot。这似乎不会发生在Scientific-Linux-System中。有没有人有任何建议为什么会发生这种情况以及如何调试它?

2 个答案:

答案 0 :(得分:0)

你必须以正确的方式使用QThread。 重载run()函数是不安全的,诺基亚之前接受它并展示如何使用QThread。

Look at this documentation

答案 1 :(得分:0)

如上所述 - 不建议在Qt中使用线程。但这对你的情况很好。无论如何,我建议你尝试下一个方法:

position: absolute