A
A
Apexis2019-11-21 14:24:37
Qt
Apexis, 2019-11-21 14:24:37

How to update label text from static method in QT?

You need to update the UI in a separate thread (pthreads). To do this, you need to pass a callback function (method of the MainWindow class) to pthread_create. I can't pass a non-static class method as a callback. You can make the method static, but then I don’t know how to access the non-static ui from it. The task seems to be simple, to update the text in the label, but I can’t figure out how to put a callback and update the interface from one function.

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

callback
void *MainWindow::status(void *a) {
   ui->start->setEnabled(false);
    while(true) {
        ui->progressBar->setValue(progress); // из статического метода до ui не достучаться
        ui->done->setText(QString::number(done));
        ui->fail->setText(QString::number(fail));

        Sleep(500);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory, 2019-11-21
@Griglapidus

In Qt, this must be done through signals and slots. Then it can be done from any thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question