Results 1 to 9 of 9

Thread: dynamic QLabel update

  1. #1
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4

    Default dynamic QLabel update

    hi,

    I've got a parent QLabel with some graphics (setPixmap) and a child QLabel (with some text)
    I'm looking for a way to update the child QLabel text and show the change right after it's done
    already tried label->setText but nothing seems to happen
    what do I have to do to "refresh the view"?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: dynamic QLabel update

    Can you please post your code?

  3. #3
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4

    Default Re: dynamic QLabel update

    this is an attempt to do it after a button click (nothing happens)
    Qt Code:
    1. Explorer::Explorer()
    2. {
    3. imageLabel = new QLabel;
    4. imageLabel->setBackgroundRole(QPalette::Base);
    5. imageLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    6. imageLabel->setWindowFlags(Qt::FramelessWindowHint);
    7. imageLabel->setFrameStyle(QFrame::NoFrame);
    8.  
    9.  
    10. setCentralWidget(imageLabel);
    11.  
    12. setWindowTitle(tr(name));
    13.  
    14.  
    15. QLabel *test = new QLabel("<h1><font color=white>222</font></h1>", imageLabel);
    16. test->move(7,80);
    17.  
    18. QPushButton *button = new QPushButton("test", imageLabel);
    19. button->setFlat(true);
    20. QObject::connect(button, SIGNAL(clicked()),
    21. this, SLOT(updateHP()));
    22.  
    23. }
    24.  
    25. void Explorer::open(char *file)
    26. {
    27. QString fileName = file;
    28. if (!fileName.isEmpty()) {
    29. QImage image(fileName);
    30. if (image.isNull()) {
    31. QMessageBox::information(this, tr("Error"),
    32. tr("Cannot load %1.").arg(fileName));
    33. return;
    34. }
    35. imageLabel->setPixmap(QPixmap::fromImage(image));
    36.  
    37. }
    38.  
    39. }
    40.  
    41. void Explorer::updateHP()
    42. {
    43. QMessageBox::information(this, tr("Update HP"),
    44. tr("HP update button clicked"));
    45. test->setText("<h1><font color=white>444</font></h1>");
    46.  
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: dynamic QLabel update

    You're very lucky that you didn't get a segmentation fault, although, I guess it's better to get one instead of none.

    This is what you wrote in your program:
    Qt Code:
    1. Explorer::Explorer()
    2. {
    3. ...
    4. QLabel *test = new QLabel(...);
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    The QLabel object named test is ONLY available in the scope of your constructor.
    You did it correctly with imageLabel.

    Thus, your solution is: define your QLabel named test in your class definition, like you probably did with imageLabel.
    Then you can use test in the other functions of your class.

  5. #5
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4

    Default Re: dynamic QLabel update

    I had a QLabel named test all the time

    sorry for that, this is my header file:
    Qt Code:
    1. #ifndef EXPLORER_H
    2. #define EXPLORER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPrinter>
    6.  
    7. class QAction;
    8. class QLabel;
    9.  
    10. class Explorer : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Explorer(char *name);
    16. void open(char *fileName);
    17. void print();
    18. QLabel *test;
    19. QLabel *imageLabel;
    20.  
    21. void updateLife(QWidget *life);
    22. void updateMana(QWidget *mana);
    23. void updateGold(QWidget *gold);
    24.  
    25. public slots:
    26. void updateHP();
    27.  
    28.  
    29.  
    30. private:
    31. char *hitPoints;
    32. QPrinter printer;
    33. void createActions();
    34. void updateExplorer();
    35.  
    36.  
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: dynamic QLabel update

    My last comment is still valid!

    You created a local object test that is only available in your constructor.

    Write this instead:
    Qt Code:
    1. Explorer::Explorer()
    2. {
    3. ...
    4. test = new QLabel(...);
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4

    Default Re: dynamic QLabel update

    it came to me as soon i wrote the answer- sorry
    thx for the help

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: dynamic QLabel update

    Yes, but the way your code is currently written, the class member 'test' will never be initialized. The member variable 'test' will be initialized, then destroyed at the end of your constructor. Get rid of the declaration statement "QLabel*" in front of test in the constructor.

  9. #9
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4

    Default Re: dynamic QLabel update

    ok, another little problem with dynamic QLabels and buttons:
    Qt Code:
    1. void Explorer::setEquipment() {
    2.  
    3. QPushButton *items = new QPushButton("Friends", mainLabel);
    4. QObject::connect(items, SIGNAL(clicked()),
    5. mainLabel, SLOT(showFriends()));
    6. items-> move(47,277);
    7. items-> setStyleSheet("color: white; font-family:Verdana, Sans-serif; font-weight:bold; border: none;");
    8.  
    9. }
    10.  
    11. void Explorer::showFriends() {
    12.  
    13. QMessageBox msgBox;
    14. msgBox.setText("Friends label");
    15. msgBox.exec();
    16. friendsLabel = new QLabel("TEST");
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Explorer : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Explorer(char *name);
    7. void mainWindow();
    8. void open(char *fileName);
    9. QLabel *mainLabel;
    10. QLabel *friendsLabel;
    11. void setEquipment();
    12.  
    13. private slots:
    14. void showFriends();
    15.  
    16.  
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    the new QLabel doesn't appear after clicking
    BTW How to debug this kind of problems?

    EDIT:
    ok, all what was needed was:
    Qt Code:
    1. friendsLabel->show();
    To copy to clipboard, switch view to plain text mode 
    Last edited by satanowicz; 26th May 2010 at 00:53.

Similar Threads

  1. Dynamic linking on Mac OS X
    By Vnuce in forum Newbie
    Replies: 6
    Last Post: 19th October 2009, 14:31
  2. Replies: 1
    Last Post: 29th September 2009, 20:44
  3. Replies: 1
    Last Post: 2nd August 2008, 16:46
  4. Dynamic Animated QLabel
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 30th May 2008, 19:38
  5. Dynamic number of QLabel
    By jd in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 18:09

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.