Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating Qlabel image

Updating Qlabel image

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valerio.j
    wrote on last edited by
    #1

    Hello,

    I have a small switch function running that I want to update the image of a label depending on the result. The issue I have is that all images are only updated at the end of the run. How can I update each image as each case is evaluated?

    int MainWindow::readADC(int range)
    {
        QPixmap stat_GO (":/res/images/stat_GO.png");
        QByteArray data;
        bool ok = false;
        float r = 0.0;
    
        switch (range)
        {
        case 24:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "24VDC Conversion Failed";
            qDebug() << r;
    
            if ((r > 23.2) && (r < 24.8))   //Check tolerance (3.3%)
                ui->_tstr24Vok->setPixmap(stat_GO);
            return 1;
            break;
    
        case 3:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "3.3VDC Conversion Failed";
            qDebug() << r;
    
            if ((r > 3.2) && (r < 3.4))   //Check tolerance (3%)
                 ui->_but3v3Vok->setPixmap(stat_GO);
            return 1;
            break;
    
        case 12:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "12VDC Conversion Failed";
            qDebug() << r;
            
            if ((r > 11.6) && (r < 12.4))   //Check tolerance (3.3%)
            ui->_but12Vok->setPixmap(stat_GO);
            return 1;
            break;
        }
        return 0;
    }
    
    1 Reply Last reply
    0
    • miclandM Offline
      miclandM Offline
      micland
      wrote on last edited by
      #2

      I don't know what you're doing after returning from that method but it seems that the event loop is still busy so the label is not updated immediately.
      Try calling ui->myLabel->repaint(); to enforce an update.

      V 1 Reply Last reply
      1
      • miclandM micland

        I don't know what you're doing after returning from that method but it seems that the event loop is still busy so the label is not updated immediately.
        Try calling ui->myLabel->repaint(); to enforce an update.

        V Offline
        V Offline
        valerio.j
        wrote on last edited by
        #3

        @micland
        After the return is validated it just continues on basically repeating similar tasks.
        adding ui->myLabel->repaint(); after each label change does what I'mm looking for. Thanks.

        1 Reply Last reply
        0
        • miclandM Offline
          miclandM Offline
          micland
          wrote on last edited by
          #4

          If you're running your tasks in an endless loop and can't move it to a background thread your UI will freeze so instead of calling repaint() it might be better to call QCoreApplication::processEvents() to process all queued events (unless you want ensure that any user input is blocked until you leave your task loop).

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved