UI isn't shown when worker thread runs.



  • I have one worker thread which parses XML and passing QString parsed data via signal to QLabel slot setText();

    I create thread and xmlparse class instances that runs in my MainWindow constructor like this:

    MainWindow:MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    startParsing();
    }

    What can cause situation that my UI with label does not appear when application is launched?


  • Lifetime Qt Champion

    Hi,

    Are you sure that startParsing doesn't block the main thread ?



  • @SGaist

    Here is my mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <xmlparser.h>
    #include <QThread>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        startParsing();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::startParsing()
    {
        QThread *xml = new QThread;
        xmlparser *first = new xmlparser();
    
        first->moveToThread(xml);
        xml->start();
    
        connect(first, SIGNAL(xmlParsed(QString)), ui->label, SLOT(setText(QString)));
    }
    

  • Lifetime Qt Champion

    Ok then, are you sure that your label is not just empty rather than not visible ?

    Note that you have two memory leaks here.


  • Moderators

    @Zola said:

    What can cause situation that my UI with label does not appear when application is launched?

    Did you call MainWindow::show()?



  • @JKSH @SGaist

    It is shown in main.cpp with:

    MainWindow w;
    w.show();


  • Lifetime Qt Champion

    Did you check that xmlparser correctly emits xmlParsed and that the parameter is not empty ?



  • I solved my problem with not calling parseFunction in its own class constructor but with signals and slots in mainwindow.cpp file:

    connect(xml, SIGNAL(started()), first, SLOT(parseFunction()));