简体   繁体   English

从Qt中的QMainWindow的构造函数启动一个新线程

[英]Start a new thread from the constructor of a QMainWindow in Qt

I have the MainWindow class. 我有MainWindow类。 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. 我在main中实例化MainWindow But i need to access the method from MainWindow in the run method of the NewThread . 但是我需要在NewThread的run方法中从MainWindow访问该方法。 That's why it is instantiated in the NewThread . 这就是为什么在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();
}

Your issue with your code is that you create the mainwindow and the qt application in different threads. 代码的问题是,您在不同的线程中创建了mainwindow和qt应用程序。 The main window seems to be created in your "new thread", whereas the qt application is not. 主窗口似乎是在“新线程”中创建的,而qt应用程序则不是。

You also seem to have a circular dependency between the mainwindow constructor and the run method of the thread. 您还似乎在mainwindow构造函数和线程的run方法之间具有循环依赖关系。

You would need to move the mainwindow creation into your main.cpp which is also a logical place for it. 您需要将mainwindow创建移动到main.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部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM