I have the MainWindow
class. In the constructor of this class I want to start a new thread that will do some work. But I get this error:
我有主窗口类。在这个类的构造函数中,我想要启动一个新线程,它将完成一些工作。但是我得到了这个错误:
Assert failure in QWidget: "Widgets must be created in the GUI thread."
在QWidget中断言失败:“必须在GUI线程中创建小部件。”
In this new thread I am not creating any widgets. This is what I have tried so far. Could someone help me on solving this problem? In don't have experience with signals and slots and I will really appreciate some advises.
在这个新线程中,我没有创建任何小部件。这是我迄今为止尝试过的。有人能帮我解决这个问题吗?在没有信号和插槽的经验中,我会非常感谢一些建议。
newThread.h
newThread.h
#ifndef NEWTHREAD_H
#define NEWTHREAD_H
#include <QThread>
#include "mainwindow.h"
class NewThread : public QThread
{
Q_OBJECT
public:
explicit NewThread(QObject *parent = 0);
signals:
public slots:
protected:
void run();
};
#endif // NEWTHREAD_H
newThread.cpp
newThread.cpp
#include "newthread.h"
NewThread::NewThread(QObject *parent) :
QThread(parent) { }
void NewThread::run(){
MainWindow m;
m.updateInBackground();
}
MainWindow.cpp
MainWindow.cpp
MainWindow::MainWindow(QStringList applications, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ReadFromRegistry read;
this->setFixedSize(435,280);
ui->setupUi(this);
appsNames = applications;
this->apps = read.getApplicationsFromRegistry(appsNames);
ui->updateInBackgroundCkb->setChecked(false);
//read from settings.xml the time interval
QString time = RWXml::readSettingsFile();
if(time.compare("-1") != 0){
NewThread th;
while(true){
th.start();
th.sleep(time.toLong(0,10));
}
}
}
EDIT:
编辑:
main.cpp
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList apps;
QString app = "AppTest1";
apps.append(app);
app = "AppTest2";
apps.append(app);
app = "AppTest3";
apps.append(app);
MainWindow w(apps);
w.create();
w.show();
return a.exec();
}
I instantiate the MainWindow
in main. But i need to access the method from MainWindow
in the run method of the NewThread
. That's why it is instantiated in the NewThread
.
我在主窗口中实例化了主窗口。但是我需要在NewThread的run方法中从MainWindow访问该方法。这就是它在NewThread中实例化的原因。
EDIT:
编辑:
void MainWindow::updateInBackground(){
ClientSocket client;
for(Application ap : getApps()){
QString currentVersion = ap.getAppVersion();
QString appCode = ap.getAppCode();
QString appSerial = ap.getAppSerialNo();
client.connect();
QString message = "2//" + currentVersion + "//" + appCode + "//"+ appSerial;
//send message to the server
client.sendMessage(message);
//receiver message from the server
QString received = client.receiveMessage();
//check if the current version is the last one
if(received.compare("0") != 0){
//if is not the last one, set the new version
ap.setAppVersion(received);
//set the update date
ap.setCurrentDate();
//write in windows registry
WriteInRegistry::writeRegistry(ap);
//update the xml file containg the updates of this application
updateXMLFile(ap);
}
}
//read from registry
ReadFromRegistry read;
//populate the grid from the MainWindow with the new data
populateTable(read.getApplicationsFromRegistry(getAppsNames()));
client.closeConnection();
}
1 个解决方案
#1
0
Your issue with your code is that you create the mainwindow and the qt application in different threads. The main window seems to be created in your "new thread", whereas the qt application is not.
您的代码问题是在不同的线程中创建主窗口和qt应用程序。主窗口似乎是在您的“新线程”中创建的,而qt应用程序则不是。
You also seem to have a circular dependency between the mainwindow constructor and the run method of the thread.
您似乎还在主窗口构造函数和线程的运行方法之间有一个循环依赖关系。
You would need to move the mainwindow creation into your main.cpp which is also a logical place for it.
您需要将主窗口创建移动到您的主窗口中。cpp也是一个合乎逻辑的地方。
That being said, please do take a look at the url below and all the references in the post for getting some further thoughts.
说到这里,请查看下面的url和文章中所有的参考资料,以获得更多的想法。
How to Use QThread in the Right Way (Part 1)
如何正确使用QThread(第1部分)
How to Use QThread in the Right Way (Part 2)
如何正确使用QThread(第2部分)
#1
0
Your issue with your code is that you create the mainwindow and the qt application in different threads. The main window seems to be created in your "new thread", whereas the qt application is not.
您的代码问题是在不同的线程中创建主窗口和qt应用程序。主窗口似乎是在您的“新线程”中创建的,而qt应用程序则不是。
You also seem to have a circular dependency between the mainwindow constructor and the run method of the thread.
您似乎还在主窗口构造函数和线程的运行方法之间有一个循环依赖关系。
You would need to move the mainwindow creation into your main.cpp which is also a logical place for it.
您需要将主窗口创建移动到您的主窗口中。cpp也是一个合乎逻辑的地方。
That being said, please do take a look at the url below and all the references in the post for getting some further thoughts.
说到这里,请查看下面的url和文章中所有的参考资料,以获得更多的想法。
How to Use QThread in the Right Way (Part 1)
如何正确使用QThread(第1部分)
How to Use QThread in the Right Way (Part 2)
如何正确使用QThread(第2部分)