C++ Qt将dll对象加载到单独的线程中,为什么?
我在使用事件循环从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
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
因此,我的问题是:
#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();
}