Results 1 to 10 of 10

Thread: Change color of labels in status Bar

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change color of labels in status Bar

    I wouldl like to change the color of a label in the statusBar

    Qt Code:
    1. statusLabelWarning = new QLabel;
    2. statusLabelWarning->setPixmap( warningPixmap);
    To copy to clipboard, switch view to plain text mode 
    warningPixmap is a member variable.
    When I need to change the color

    Qt Code:
    1. statusLabelWarning->setText(tr("Rad. stimata"));
    2. warningPixmap.fill(Qt::red);
    To copy to clipboard, switch view to plain text mode 

    but nothing happens.
    I am missing something?
    G

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    try to reset this pixmap to a lable.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    How?
    What do you mean to reset the QPixmap to a label?
    G

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    like this
    Qt Code:
    1. ...
    2. statusLabelWarning->setText(tr("Rad. stimata"));
    3. warningPixmap.fill(Qt::red);
    4. statusLabelWarning->setPixmap( warningPixmap);
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    First of all I think that setting text:
    Qt Code:
    1. statusLabelWarning->setText(tr("Rad. stimata"));
    To copy to clipboard, switch view to plain text mode 
    clears the pixmap in label. If not, you are filling the pixmap, which is not that one in your label. In both cases you have to set it again, as my preposter said. Other way is to get your pixmap directly from label:
    Qt Code:
    1. const_cast<QPixmap*>(statusLabelWarning->pixmap())->fill(Qt::red);
    To copy to clipboard, switch view to plain text mode 

    P.S. As it stands in docs: "Setting the pixmap clears any previous content."
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar - nothing

    I am afraid to say that your solutions do not work.
    The fact that the label is in the statusBar might be a problem?
    I tried both solution to change the color of the label in the statusBar, but noting happen.

    The same happen if I want to change the color of a label in a QToolBox.
    Qt Code:
    1. void MainWindow::changeColor( QLabel &s, const QString &color)
    2. {
    3. QPalette palette;
    4. palette.setColor(QPalette::Active,QPalette::Window,QColor(color));
    5. s.setPalette(palette);
    6. //qDebug()<<"Colore.."<<s.palette()<<color;
    7. }
    To copy to clipboard, switch view to plain text mode 
    With this code nothing happen if the label is on a QToolBox. Nice if label is inside a standard QWidget....

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    could you prepare compilable example with the problem?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    ok I've just tested 2 solutions. Here is the code:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QMainWindow>
    3. #include <QStatusBar>
    4. #include <QLabel>
    5. #include <QPixmap>
    6. #include <QPushButton>
    7.  
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12. public:
    13. MainWindow(int w) : QMainWindow(), way(w)
    14. {
    15. label = new QLabel("text");
    16. button = new QPushButton("RED");
    17. setCentralWidget(button);
    18. QPixmap pm(64, 16);
    19. pm.fill(Qt::black);
    20. statusBar()->addPermanentWidget(label);
    21. label->setPixmap(pm);
    22. connect(button, SIGNAL(clicked()), this, SLOT(slot_clicked()));
    23. }
    24.  
    25. private:
    26. QLabel * label;
    27. QPushButton * button;
    28. int way;
    29.  
    30. private slots:
    31. void slot_clicked()
    32. {
    33. if (way == 1) // <- first way to change color
    34. {
    35. QPixmap pm(64, 16);
    36. pm.fill(Qt::red);
    37. label->setPixmap(pm);
    38. way = 2;
    39. return;
    40. }
    41. if (way == 2) // <- second way
    42. {
    43. const_cast<QPixmap*>(label->pixmap())->fill(Qt::black);
    44. label->update();
    45. way = 1;
    46. }
    47. }
    48. };
    49.  
    50. int main(int argc, char *argv[])
    51. {
    52. QApplication a(argc, argv);
    53. MainWindow w(1);
    54. w.show();
    55. return a.exec();
    56. }
    57.  
    58. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    In this app when you click on button "RED" the label in statusBar changes it color from black to red or from red to black. In the first direction it's first way to do this, and in second it's second.

    And here is screeshot:
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. The following user says thank you to faldzip for this useful post:

    giusepped (10th February 2009)

  10. #9
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    But if I wanna change also the text of the label along with the color?
    It seems that subsequent change of the content of the lable delete the previous content...

  11. #10
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change color of labels in status Bar

    I think a better way is to change di background of the label:
    Qt Code:
    1. void MainWindow::changeColor( QLabel &s, const QString &color)
    2. {
    3. QPalette palette;
    4. palette.setColor(QPalette::Active,QPalette::Window,QColor(color));
    5. s.setPalette(palette);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    and

    changeColor(myLabel,"red");

Similar Threads

  1. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  2. Replies: 1
    Last Post: 20th July 2006, 09: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
  •  
Qt is a trademark of The Qt Company.