PDA

View Full Version : Change color of labels in status Bar



giusepped
3rd February 2009, 05:49
I wouldl like to change the color of a label in the statusBar



statusLabelWarning = new QLabel;
statusLabelWarning->setPixmap( warningPixmap);

warningPixmap is a member variable.
When I need to change the color



statusLabelWarning->setText(tr("Rad. stimata"));
warningPixmap.fill(Qt::red);


but nothing happens.
I am missing something?
G

spirit
3rd February 2009, 06:32
try to reset this pixmap to a lable.

giusepped
3rd February 2009, 08:36
How?
What do you mean to reset the QPixmap to a label?
G

spirit
3rd February 2009, 08:40
like this


...
statusLabelWarning->setText(tr("Rad. stimata"));
warningPixmap.fill(Qt::red);
statusLabelWarning->setPixmap( warningPixmap);
...

faldzip
3rd February 2009, 10:36
First of all I think that setting text:


statusLabelWarning->setText(tr("Rad. stimata"));

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:


const_cast<QPixmap*>(statusLabelWarning->pixmap())->fill(Qt::red);


P.S. As it stands in docs: "Setting the pixmap clears any previous content."

giusepped
4th February 2009, 01:17
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.


void MainWindow::changeColor( QLabel &s, const QString &color)
{
QPalette palette;
palette.setColor(QPalette::Active,QPalette::Window ,QColor(color));
s.setPalette(palette);
//qDebug()<<"Colore.."<<s.palette()<<color;
}

With this code nothing happen if the label is on a QToolBox. Nice if label is inside a standard QWidget....:confused:

spirit
4th February 2009, 06:36
could you prepare compilable example with the problem?

faldzip
4th February 2009, 07:45
ok I've just tested 2 solutions. Here is the code:


#include <QtGui/QApplication>
#include <QMainWindow>
#include <QStatusBar>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>


class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(int w) : QMainWindow(), way(w)
{
label = new QLabel("text");
button = new QPushButton("RED");
setCentralWidget(button);
QPixmap pm(64, 16);
pm.fill(Qt::black);
statusBar()->addPermanentWidget(label);
label->setPixmap(pm);
connect(button, SIGNAL(clicked()), this, SLOT(slot_clicked()));
}

private:
QLabel * label;
QPushButton * button;
int way;

private slots:
void slot_clicked()
{
if (way == 1) // <- first way to change color
{
QPixmap pm(64, 16);
pm.fill(Qt::red);
label->setPixmap(pm);
way = 2;
return;
}
if (way == 2) // <- second way
{
const_cast<QPixmap*>(label->pixmap())->fill(Qt::black);
label->update();
way = 1;
}
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w(1);
w.show();
return a.exec();
}

#include "main.moc"


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:

giusepped
10th February 2009, 01:50
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...

giusepped
10th February 2009, 02:00
I think a better way is to change di background of the label:


void MainWindow::changeColor( QLabel &s, const QString &color)
{
QPalette palette;
palette.setColor(QPalette::Active,QPalette::Window ,QColor(color));
s.setPalette(palette);

}


and

changeColor(myLabel,"red");