Results 1 to 3 of 3

Thread: updating qlabel image in switch statement

  1. #1
    Join Date
    Dec 2014
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default updating qlabel image in switch statement

    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?

    Qt Code:
    1. int MainWindow::readADC(int range)
    2. {
    3. QPixmap stat_GO (":/res/images/stat_GO.png");
    4. QByteArray data;
    5. bool ok = false;
    6. float r = 0.0;
    7.  
    8. switch (range)
    9. {
    10. case 24:
    11. serial->waitForReadyRead(1000);
    12. data = serial->readAll();
    13. qDebug() << data;
    14. r = data.toFloat(&ok);
    15. if (!ok) qDebug() << "24VDC Conversion Failed";
    16. qDebug() << r;
    17.  
    18. if ((r > 23.2) && (r < 24.8)) //Check tolerance (3.3%)
    19. ui->_tstr24Vok->setPixmap(stat_GO);
    20. return 1;
    21. break;
    22.  
    23. case 3:
    24. serial->waitForReadyRead(1000);
    25. data = serial->readAll();
    26. qDebug() << data;
    27. r = data.toFloat(&ok);
    28. if (!ok) qDebug() << "3.3VDC Conversion Failed";
    29. qDebug() << r;
    30.  
    31. if ((r > 3.2) && (r < 3.4)) //Check tolerance (3%)
    32. ui->_but3v3Vok->setPixmap(stat_GO);
    33. return 1;
    34. break;
    35.  
    36. case 12:
    37. serial->waitForReadyRead(1000);
    38. data = serial->readAll();
    39. qDebug() << data;
    40. r = data.toFloat(&ok);
    41. if (!ok) qDebug() << "12VDC Conversion Failed";
    42. qDebug() << r;
    43.  
    44. if ((r > 11.6) && (r < 12.4)) //Check tolerance (3.3%)
    45. ui->_but12Vok->setPixmap(stat_GO);
    46. return 1;
    47. break;
    48. }
    49. return 0;
    50. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: updating qlabel image in switch statement

    This method is OK. The problem is how this method is invoke. Refreshing the screen needs working event loop. So if You do this calling readADC 3 times You have such a symptom.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: updating qlabel image in switch statement

    Also blocking the main thread and thus the GUI for up to a second (through waitForReadyRead) is usually also a very bad idea.

    There really doesn't seem to be a need for this, all branches do that, so the switch could easily be done in a slot connected to readyRead().

    The code suprisingly also doesn't even care how much data is reads.

    Cheers,
    _

Similar Threads

  1. Updating QLabel Image
    By jvalerio in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2014, 15:06
  2. geting QLabel text ontop of other QLabel displaying image
    By krystosan in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2013, 17:35
  3. Replies: 7
    Last Post: 3rd March 2011, 14:32
  4. custom image dial and switch
    By Bill_Lumberg in forum Newbie
    Replies: 2
    Last Post: 8th October 2010, 19:43
  5. Pixmap updating QLabel
    By Matt in forum Newbie
    Replies: 11
    Last Post: 17th August 2010, 21:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.