PDA

View Full Version : Backgound color of QWidget



jnana
15th March 2006, 11:24
Hi ,

I want to put different backgound color for different widget. In Qt3 there was some thing setBackgoundColor() methods. In Qt-4.1, I got setBackgroundRole(), it takes QPalete::-- as argument. I want to set backgound color through QColor object, because from QColorDialog i will get QColor object. Is there any method to achieve this.

Thanks
Jnana

jacek
15th March 2006, 11:40
QPalette p( widget->palette() );
p.setColor( QPalette::Window, Qt::red );
widget->setPalette( p );

guilugi
15th March 2006, 11:42
Yup,

You have to use QPalette now ;)

Something like this :



QPalette *palette = your_widget->palette();
palette->setBrush(QPalette::Base, QColor(your_color));
your_widget->setPalette(palette);


Guilugi.

guilugi
15th March 2006, 11:43
Oops :)

Too fast for me, Jacek.

I bow :D

Sarma
15th March 2006, 12:16
Even there is a simpler method than that:

QColor c1="<your_color>";
setPalette(c1);

jnana
15th March 2006, 12:27
Thanks all of u.

jnana
17th March 2006, 10:39
Hi All,

How to set background color of QLabel objects. I tried with setPalette(), it does not work for labels, it works for other case.

Thanks
Jnana

jpn
17th March 2006, 10:58
Hmm, it certainly does work for a QLabel..

Are you sure that you are setting the palette's color for correct role?
Btw, I suggest using the method introduced by jacek.. (just change Qt::Window -> QPalette::Window).

(Are you sure that you don't override the color in the paint event?)

jnana
17th March 2006, 12:41
Hi ,

I tried with following code. I got no result.


QPalette p( ui.label21->palette() );
p.setBrush( QPalette::Window, Qt::darkCyan );
ui.label21->setPalette(p);

jacek
17th March 2006, 12:46
#include <QApplication>
#include <QLabel>
#include <QPalette>

int main( int argc, char **argv )
{
QApplication app( argc, argv );

QLabel l;
QPalette p( l.palette() );
p.setColor( QPalette::Window, Qt::darkCyan );
l.setPalette( p );

l.show();

return app.exec();
}
I use Qt 4.1.1 under PLD Linux.

Moppel
19th March 2006, 13:24
Whilst the example below or above works,
this doesn't:



#ifndef BWIDGET_H
#define BWIDGET_H

#include <QWidget>
class QLabel;

class BWidget : public QWidget
{
public:
BWidget(QWidget *parent = 0);
~BWidget(){};

private:
QLabel *_myLabel;
};

#endif




#include "bwidget.h"
#include <QLabel>

BWidget::BWidget(QWidget *parent) : QWidget(parent)
{
_myLabel = new QLabel(this);
QPalette p = _myLabel->palette();
p.setColor(QPalette::Window, Qt::red);
_myLabel->setPalette(p);
_myLabel->setText("OK");
// setPalette(p);
}


After the window is redrawn, the background colour of the label is displayed as desired for a split second (e.g. after it was hidden behind another window and it's shown again. You have to toggle very fast to see it, though).
But something redraws it with the standard gray.
Any ideas?

http://home.arcor.de/mritscher/backColour.png

jacek
19th March 2006, 14:06
IMO this is a Qt bug (I use Qt 4.1.1 under PLD Linux). One can reproduce it even in Qt Designer (just create a form with a label, change its background colour with palette editor and see the preview).

Moppel
19th March 2006, 15:53
mmh, really looks like a bug.
I played around with it a bit and whilst your example works even if modified (using the background image) mine doesn't work.
Somewhere in the doc is an example how to transform the QT3 setPaletteBackgroundPixmap to QT4.
Using QT += qt3support int the pro file and a Q3TextEdit the background image or colour still shows up.
So if it's a bug. Is Trolltech aware of it?



#include "bwidget.h"

#include <QLabel>
#include <QTextEdit>
#include <Q3TextEdit>
#include <QPalette>
#include <QPixmap>
#include <QApplication>
#include <QVBoxLayout>

BWidget::BWidget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
_myLabel = new QLabel();
_myLabel->setText("QLabel");
QTextEdit *anEdit = new QTextEdit();
anEdit->append("QTextEdit");
anEdit->setFixedHeight(24);
Q3TextEdit *a3Edit = new Q3TextEdit;
a3Edit->append("Q3TextEdit");
layout->addWidget(_myLabel);
layout->addWidget(anEdit);
layout->addWidget(a3Edit);

QPalette p = _myLabel->palette();
// p.setColor(QPalette::Window, Qt::red);
p.setColor(_myLabel->backgroundRole(), Qt::red);
QPixmap pixmap(QCoreApplication::applicationDirPath () +"/img/debug.png");
p.setBrush( _myLabel->backgroundRole(), QBrush(pixmap));
_myLabel->setPalette(p);
anEdit->setPalette(p);

a3Edit->setPaletteBackgroundPixmap ( pixmap );
// a3Edit->setBackgroundColor(Qt::red);

// setPalette(p);
setLayout(layout);
}


(Using Qt 4.1.1 self compiled on Linux - SuSE 9.3)

jacek
19th March 2006, 16:08
So if it's a bug. Is Trolltech aware of it?
I couldn't find anything similar in the task tracker, so it will be better to ask the Trolls.

Moppel
21st March 2006, 10:07
My previous example was incorrect.
For the background of the text widget you need to set QPalette::Brush.
So the example below works correctly for the textedit.
Uncomment either the setColor or the setBrush to see the picture or the colour in the background of the edit.
No matter that you uncomment for the label, no changes can be seen.
That is so for qt 4.1.1 on Windows, SuSE 9.3, SuSE 10.0 and qt 4.1.0 on SuSE 9.3 and 10.0.




#include "bwidget.h"

#include <QLabel>
#include <QTextEdit>
#include <QPalette>
#include <QPixmap>
#include <QApplication>
#include <QVBoxLayout>

BWidget::BWidget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
_myLabel = new QLabel();
_myLabel->setText("OK");
QTextEdit *anEdit = new QTextEdit();
anEdit->append("an edit");

layout->addWidget(_myLabel);
layout->addWidget(anEdit);

QPixmap pixmap(QCoreApplication::applicationDirPath () +"/img/debug.png");

QPalette p = _myLabel->palette();
p.setColor(QPalette::WindowText, Qt::green);
p.setColor(QPalette::Window, Qt::red);
// p.setBrush(QPalette::Window, QBrush(pixmap));
// p.setColor(_myLabel->backgroundRole(), Qt::red);
_myLabel->setPalette(p);

// p.setColor(QPalette::Base, Qt::red);
p.setBrush( QPalette::Base, QBrush(pixmap));
anEdit->setPalette(p);


// setPalette(p);
setLayout(layout);
}

Moppel
21st March 2006, 21:08
Right, no bug. It's all in the documentation.
If the line below is added, it works.



_myLabel->setAutoFillBackground ( true );


Thanks Volker from Trolltech to pointing this out to me.