Results 1 to 4 of 4

Thread: Updating variable

  1. #1
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Updating variable

    Hi. In my gui i have some buttons. For example when i move a mouse on IRButton1 and next IRButton2 i increase liczbaLudzi1. When i change a direction (from IRButton2 to IRButton1) decrease liczbaLudzi1. I want to display as many pixmaps (using labels) as it's value liczbaLudzi1. A part of program reliable for increase and decrease of liczbaLudzi1 is in filter event and it is working (increasing and decreasing as i wanted). I have problem with painting pixmaps. The problem is that if i increase liczbaLudzi1 i also increase number of pixmaps on gui (it's ok) but when i decrease liczbaLudzi1 the number of pixmaps stays on the last highest value liczbaLudzi1.

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *paint)
    2. {
    3. ...
    4. switch (liczbaLudzi1)
    5. {
    6. case 1:
    7. ludzik1->setVisible(true); update();break;
    8. case 2:
    9. ludzik2->setVisible(true); update();break;
    10. case 3:
    11. ludzik3->setVisible(true); update();break;
    12. }
    13. ...
    14. }
    To copy to clipboard, switch view to plain text mode 
    Where ludzik_ is QLabel.
    What i should do to solve it. I try with update() but switch only works up (increase number of labels with increasing number of liczbaLudzi1) and don't works down (decrease number of labels with decreasing number of liczbaLudzi1).

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Updating variable

    I think its because you never call ludzikN->setVisible(false). When you show ludzikN, call setVisible(false) on each ludzik_N+1, ludzik_N+2 etc:
    Qt Code:
    1. switch (liczbaLudzi1)
    2. {
    3. case 1:
    4. ludzik1->setVisible(true); update();
    5. // call setVisible(false) on ludzik2 and ludzik3 here
    6. break;
    7. case 2:
    8. ludzik2->setVisible(true); update();
    9. // same here for ludzik3
    10. break;
    11. case 3:
    12. ludzik3->setVisible(true); update();break;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Another thing is that you can call "setVisible()" immediately when the liczbaLudzi1 value is changed, paintEvent() should only display the actual widget state, not change it.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating variable

    There are several problems with your approach:
    1. paintEvent() implements the low-level painting of a widget. In such a function you should only use paint commands such as QPainter::drawPixmap(). Since you are using QLabels to display the images, forget about paintEvent() and call your code whenever the value of the variable changes.
    2. It seems that you want to have exactly one QLabel visible at a time, which one being decided by the value of the variable. You correctly make the new QLabel visible, but you forget to hide the old one. Therefore once the frontmost QLabel becomes visible it hides all the others.
    3. You use several QLabels when you could simply use one and change the pixmap it displays.

    I thus recommend to have one QLabel and call setPixmap() with the appropriate pixmap whenever the value of the variable changes.

  4. #4
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating variable

    yeye_olive
    Now i paste code in eventFilter, where liczbaLudziN is changing.

    stampede
    Of course! I didn't call setVisible(false)

    Thanks for help.

Similar Threads

  1. QTableView top row not updating
    By murrekatt in forum Newbie
    Replies: 4
    Last Post: 3rd March 2010, 14:50
  2. variable
    By Atuti2009 in forum Qt Programming
    Replies: 8
    Last Post: 18th November 2009, 09:04
  3. QGrapghicsView updating
    By hayzel in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2009, 13:54
  4. updating .rc file
    By navi1084 in forum Qt Programming
    Replies: 0
    Last Post: 9th October 2008, 09:53
  5. updating database
    By locus in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2007, 05:54

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.