Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt将dll对象加载到单独的线程中,为什么?_C++_Qt - Fatal编程技术网

C++ Qt将dll对象加载到单独的线程中,为什么?

C++ Qt将dll对象加载到单独的线程中,为什么?,c++,qt,C++,Qt,我在使用事件循环从dll启动基于计时器的对象时遇到一些问题。因此,我做了一个简单的示例,这是我的输出: QObject: Cannot create children for a parent that is in a different thread. (Parent is MainWindow(0x28fe08), parent's thread is QThread(0x1222aab0), current thread is QThread(0x121421c8) QObject::st

我在使用事件循环从dll启动基于计时器的对象时遇到一些问题。因此,我做了一个简单的示例,这是我的输出:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is MainWindow(0x28fe08), parent's thread is QThread(0x1222aab0), current thread is QThread(0x121421c8)
QObject::startTimer: Timers can only be used with threads started with QThread
MainWindow thread: 0x1222aab0
DllThreadTest thread: 0x121421c8
我正在使用Windows 8和Qt5.3.2

因此,我的问题是:

  • 为什么我从dll创建的对象与主窗口在不同的线程中?正如您在代码中看到的,我没有使用线程

  • 从我的库中创建对象时,我无法将此作为参数传递,正如您在第一条错误消息中看到的那样。请参见下面的示例

  • 我猜当我解决线程问题时,我也解决了计时器问题

    例如:

    dllthreadtest.h

    #ifndef DLLTHREADTEST_H
    #define DLLTHREADTEST_H
    
    #include <QObject>
    #include <QTimer>
    
    #if defined TEST_BUILD_DLLTHREADTEST
        #define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
    #else
        #define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
    #endif
    
    class TEST_COMMON_DLLSPEC DllThreadTest : public QObject
    {
        Q_OBJECT
    public:
        explicit DllThreadTest(QObject *parent = 0);
    
    signals:
    
    public slots:
        void onTimeOut();
    private:
        QTimer timer;
    };
    
    #endif // DLLTHREADTEST_H
    
    我正在构建它,然后在一个新创建的默认项目中包含该库,该项目只创建了一个QMainWindow。在这里,我实例化了一个DllThreadTest对象

    DllImportTest.pro

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = DllImportTest
    TEMPLATE = app
    
    INCLUDEPATH += \
    $$PWD/libs/
    
    win32{
    LIBS += -L$$PWD/libs/ -ldllthreadtest
    }
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dllthreadtest.h"
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->pushButton,SIGNAL(clicked()),
                this,SLOT(onButtonClick()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::onButtonClick()
    {
        dllThreadTest_=new DllThreadTest;
        qDebug()<<"MainWindow thread:"<<this->thread();
        qDebug()<<"DllThreadTest thread:"<<dllThreadTest_->thread();
    }
    
    maindwindow.cpp(仅DllImportTest.pro类)

    #包括“mainwindow.h”
    #包括“ui_main window.h”
    #包括“dllthreadtest.h”
    #包括
    主窗口::主窗口(QWidget*父窗口):
    QMainWindow(父级),
    用户界面(新用户界面::主窗口)
    {
    用户界面->设置用户界面(此);
    连接(用户界面->按钮,信号(点击()),
    这个,插槽(onButtonClick());
    }
    MainWindow::~MainWindow()
    {
    删除用户界面;
    }
    void主窗口::onButtonClick()
    {
    dllThreadTest=新的dllThreadTest;
    
    qDebug()你似乎有两个
    main.cpp
    文件。这很可疑。如果我从我的dll项目中删除main.cpp,这没有什么区别。我在那里实例化了一个DllThreadTest对象-它怀疑显示此代码可能是个好主意。我在我的文章末尾添加了代码。嗯,这很奇怪。你能在y中再添加两个调试输出吗我们的onButtonClick()方法检查a)
    QApplication::instance()->thread()
    和b)
    QThread::currentThread()
    的结果。
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = DllImportTest
    TEMPLATE = app
    
    INCLUDEPATH += \
    $$PWD/libs/
    
    win32{
    LIBS += -L$$PWD/libs/ -ldllthreadtest
    }
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dllthreadtest.h"
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->pushButton,SIGNAL(clicked()),
                this,SLOT(onButtonClick()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::onButtonClick()
    {
        dllThreadTest_=new DllThreadTest;
        qDebug()<<"MainWindow thread:"<<this->thread();
        qDebug()<<"DllThreadTest thread:"<<dllThreadTest_->thread();
    }
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }