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

时间:2014-06-03 20:28:20

标签: c++ multithreading qt qtgui qtcore

我有MainWindow课程。在这个类的构造函数中,我想开始一个新的线程来完成一些工作。但是我得到了这个错误:

  

在QWidget中断言失败:“必须在GUI线程中创建小部件。”

在这个新线程中,我没有创建任何小部件。这是我到目前为止所尝试的。有人可以帮我解决这个问题吗?在没有信号和插槽的经验,我真的很感激一些建议。

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

#include "newthread.h"
NewThread::NewThread(QObject *parent) :
    QThread(parent) { }

void NewThread::run(){
    MainWindow m;
    m.updateInBackground();
}

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));
       }    
    }
}

修改

的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();
}

我在main中实例化MainWindow。但我需要在MainWindow的run方法中从NewThread访问该方法。这就是它在NewThread中实例化的原因。

修改

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 个答案:

答案 0 :(得分:0)

您的代码问题是您在不同的线程中创建主窗口和qt应用程序。主窗口似乎是在“新线程”中创建的,而qt应用程序则不是。

您似乎在主窗口构造函数和线程的run方法之间存在循环依赖关系。

您需要将主窗口创建移动到main.cpp中,这也是它的合理位置。

话虽如此,请查看下面的网址和帖子中的所有参考资料,以便进一步了解。

How to Use QThread in the Right Way (Part 1)

How to Use QThread in the Right Way (Part 2)