Updating QLabel from non-gui thread



  • @void MainWindow::update()
    {
    while(work)
    {
    QSettings setting("Log.ini",QSettings::IniFormat);
    setting.beginGroup("History");
    setting.setValue("CurrentMailpass",currentDataLine);
    setting.endGroup();

        ui->label_21->setNum(socksLenght);
        ui->label_22->setNum(dataLenght);
        ui->label_23->setNum(currentSocksLine);
        ui->label_24->setNum(currentDataLine);
        ui->label_5->setNum(liveNum);
        QCoreApplication::processEvents();
    
        ::Sleep(2000);
    }
    

    }@

    @ QString key = ui->lineEdit_7->text();
    work = true;
    if(key.length() > 0) QtConcurrent::run(this,&MainWindow::socksUpdate,key);
    QtConcurrent::run(this,&MainWindow::update);
    thread();
    work = false;@

    But those labels are not updated



  • Lol i have 5 threads in the program, but 2 was runned only

    Fix:
    @QThreadPool::globalInstance()->setMaxThreadCount(100);@


  • Moderators

    FYI, QWidgets are not thread safe, and should be accessed from the GUI thread only. Updating them directly from other threads can cause problems for you in the future.

    For cross-thread communications, I recommend signals + slots.

    Also, QtConcurrent::run() and QThreadPool are meant for short tasks (the whole idea is, when your task finishes, the thread can be reused for other tasks). For long tasks like infinite loops, use QThread.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.