PDA

View Full Version : QPalette help pls



munna
28th September 2006, 14:57
Hi,

I have a widget whose constructor is something like this :




QPalette pal;
pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/background.png")));
setPalette(pal);

nameLabel = new QLabel(tr("Name"),this);
nameLabel->setFont(QFont("Arial",9));

QIcon icon(":/images/plusbutton.png");
addButton = new QPushButton(tr(""),this);
addButton->setIcon(icon);
addButton->setFlat(true);
addButton->setFixedSize(20,20);
connect(addButton,SIGNAL(pressed()),this,SLOT(addC ontact()));

QHBoxLayout *hLay = new QHBoxLayout;
hLay->addSpacing(5);
hLay->addWidget(nameLabel);
hLay->addStretch(1);
hLay->addWidget(addButton);

nameListWidget = new NameListWidget(c,this);
connect(nameListWidget,SIGNAL(currentItemChanged(Q ListWidgetItem *,QListWidgetItem *)),
this,SLOT(selectionChanged(QListWidgetItem *,QListWidgetItem *)));
connect(nameListWidget,SIGNAL(clearContact()),this ,SLOT(clearContact()));

//connect(nameListWidget,SIGNAL(currentRowChanged(in t )),this,SLOT(selectionChanged(int )));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->addSpacing(5);
mainLayout->addLayout(hLay);
mainLayout->addWidget(nameListWidget);
setLayout(mainLayout);



The background is not changing though I have set the palette.

Can someone please help ?
Thanks a lot.

jpn
28th September 2006, 15:07
Try QWidget::setAutoFillBackground(true).

munna
28th September 2006, 15:25
Works. Thanx.

But there is another problem. If you see in the code, I have a label and a button for which I am setting an icon and then resizing after making it flat.

Everything on the label is fine. Label has the same backgound as the wiget.

But there is a problem with the button because of the icon.
The icon is not square and therefore the button's background is still gray.

Can you please tell me how can I change this also like label's background ?

Earlier I tried setting mask for the button, but it did not work.

Thanks.

jpn
28th September 2006, 15:29
I think QPushButton uses a different color role for drawing it's background:

From docs:


QPalette::Button

The general button background color. This background can be different from Window as some styles require a different background color for buttons.

munna
28th September 2006, 15:41
I tried



pal.setBrush(QPalette::Button,QBrush(QPixmap(":/images/background.png")));


But I cannot use this.

Let me explain:

The background is some kind of gradient and looks perfect for the label and the widget background.

If I use the above code, buttons background will have some gradient which is not the way label's gradient looks like.

Please look at the image attached. Please zoom it to get a clear picture.

Please help
Thanks.