Results 1 to 6 of 6

Thread: Updating QLabel Image

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

    Default Updating QLabel Image

    Hello,

    I have a program that communicates with an Arduino board and have made a simple protocol. Now I'm trying to automate things and want some advice on how to accomplish my current task.
    So Basically I have the below function which when called sends data over to the Aurdino telling it which pin to turn on. So the idea is to update the QLabel with a stat_sBy button. This is a yellow .png image and there are other states I will work with later. But I'm getting a error as I'm not sure what to use to Automate the process for replacing the QLabel Objecname in the loop.



    Qt Code:
    1. void MainWindow::TestIOPCB()
    2. {
    3. QPixmap stat_OFF (":/res/images/stat_OFF.png");
    4. QPixmap stat_noGO (":/res/images/stat_noGO.png");
    5. QPixmap stat_GO (":/res/images/stat_GO.png");
    6. QPixmap stat_sBy (":/res/images/stat_sBy.png");
    7.  
    8. QByteArray testOuts;
    9. testOuts.resize(4);
    10. testOuts[0] = 24v_ena;
    11. testOuts[1] = spare_cmd;
    12. testOuts[2] = shut_cmd;
    13. testOuts[3] = mux_cmd;
    14.  
    15. QStringList testLabels;
    16. testLabels << "Label25" << "Laberl45";
    17.  
    18. char db0 = 0x53;
    19. char db1 = 0x50;
    20. //Initiate I/O Tests
    21.  
    22. for (int x = 0; x < 4; x++)
    23. {
    24. setOutput(db0, 0x48, db1, testOuts[x]);
    25. ui->testLabels[x]->setPixmap(stat_sBy);
    26.  
    27. //The below works fine, but I have too many ports and want to simplify a little.//
    28. /*
    29.   setOutput(db0, 0x48, db1, shutter_cmd);
    30.   ui->label_27->setPixmap(stat_sBy);
    31.  
    32.   setOutput(db0, 0x48, db1, mux_1064_cmd);
    33.   ui->label_28->setPixmap(stat_sBy);
    34.  
    35.   setOutput(db0, 0x48, db1, mux_532_cmd);
    36.   ui->label_29->setPixmap(stat_sBy);
    37.  
    38.   setOutput(db0, 0x48, db1, amp_atten_cmd);
    39.   ui->label_30->setPixmap(stat_sBy);
    40.   */
    41. }
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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

    Is there is fixed mapping of value to pixmap?

    E.g. "shutter_cmd" always means "stat_sBy"?

    If so use a switch() or put the pixmaps into a map/hash with the value as the key.

    Cheers,
    _

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

    Default Re: Updating QLabel Image

    No, they can have either of the other values: stat_OFF; stat_noGO; stat_GO; stat_sBy;

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Updating QLabel Image

    Your error is because you are looping over 4 things, but your "testLabels" list only contains 2 things. But even if it had 4 things in it, those are QString instances, not pointers to QLabels. The solution is to stuff your QLabel pointers into a list as you make them:

    Qt Code:
    1. QList< QLabel * > testLabels;
    2. testLabels << ui->label_27 << ui->label_28 << ui->label_29 << ui->label_30;
    3.  
    4. for (int x = 0; x < 4; x++)
    5. {
    6. setOutput(db0, 0x48, db1, testOuts[x]);
    7. ui->testLabels[x]->setPixmap(stat_sBy);
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Updating QLabel Image

    Thanks, that did it. Just had to remove ui-> from line 7. of your post.
    The below works perfect. I would like to know if anyone out there had any other ways of doing this.

    Qt Code:
    1. QList< QLabel * > testLabels;
    2. testLabels << ui->label_27 << ui->label_28 << ui->label_29 << ui->label_30;
    3.  
    4. for (int x = 0; x < 4; x++)
    5. {
    6. setOutput(db0, 0x48, db1, testOuts[x]);
    7. testLabels[x]->setPixmap(stat_sBy);
    8. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Updating QLabel Image

    Just had to remove ui-> from line 7. of your post.
    Yes, sorry. Too much cut and paste after a long day at the keyboard. Glad it worked.

Similar Threads

  1. geting QLabel text ontop of other QLabel displaying image
    By krystosan in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2013, 17:35
  2. Where can Qlabel keep its image?
    By hind in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2013, 03:11
  3. Pixmap updating QLabel
    By Matt in forum Newbie
    Replies: 11
    Last Post: 17th August 2010, 21:11
  4. Replies: 6
    Last Post: 21st September 2009, 10:55
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.