PDA

View Full Version : QTreeWidget backgroundcolor



vmferreira
8th August 2006, 23:25
Hi,
I'm trying to change the background color of a qtreewidget without sucess:

QApplication *qa = new QApplication(argc, argv);
QWidget *qw = new QWidget();
QTreeWidget *tw = new QTreeWidget(qw);
QStringList lst;
lst.append(QString("testing"));
QTreeWidgetItem *test = new QTreeWidgetItem(tw, lst);
tw->setBackgroundColor(Qt::blue);
What's wrong? I'm using qt 4.2

In fact, I'm trying to change the color of a QGraphicsView (implemented in 4.2) ..
Thanks in advance

jacek
8th August 2006, 23:34
What's wrong?
QTreeWidgetItem::setBackgroundColor() takes two parameters.

If you want to change the whole QTreeWidget background, try:
QPalette p( treeWidget->palette() );
p.setColor( QPalette::Base, Qt::blue );
treeWidget->setPalette( p );


In fact, I'm trying to change the color of a QGraphicsView (implemented in 4.2)
Then why you are bothering with QTreeWidgetItem? Try QGraphicsView::setBackgroundBrush().

vmferreira
9th August 2006, 19:10
Thanks a lot jacek, your answer helped a lot.
Now, I'm trying to get the view of the graphic transparent, in order to see a pixmap that is loaded from the widget parent, above is how I'm doing:

QApplication *qa = new QApplication(argc, argv);
QWidget *qd = new QWidget();
QPixmap pix(":/images/bola_verde.png");
qd->setBackgroundPixmap(pix);

QGraphicsScene *scene = new QGraphicsScene(qd);
QGraphicsView *view = new QGraphicsView ( scene, qd );
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QBrush alphaBrush = view->palette().brush(QPalette::Base);
QColor alphaColor(Qt::transparent);
alphaBrush.setColor(alphaColor);
//view->setBackgroundBrush(alphaBrush);
view->setWindowOpacity(0.3);
scene->setBackgroundBrush(alphaBrush);

It does not work. This can be done? I don't think that mask() can solve my problem..

Thanks

jacek
9th August 2006, 19:56
This can be done?
Of course:
QGraphicsScene *scene = new QGraphicsScene(qd);
QGraphicsView *view = new QGraphicsView ( scene, qd );
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

QPalette p( view->palette() );
p.setBrush( QPalette::Base, Qt::NoBrush );
view->setPalette( p );


I don't think that mask() can solve my problem..
No, setMask() is for making "holes" in widgets.

vmferreira
9th August 2006, 20:45
Thanks again jacek, but now it didn't work
My code:
QApplication *qa = new QApplication(argc, argv);
QWidget *qd = new QWidget();
QPixmap pix(":/images/bola_verde.png");
qd->setBackgroundPixmap(pix);

QGraphicsScene *scene = new QGraphicsScene(qd);
QGraphicsView *view = new QGraphicsView ( scene, qd );
QPalette p( view->palette() );
p.setBrush( QPalette::Base, Qt::NoBrush );
view->setPalette( p );
//scene->setBackgroundBrush(p.brush(QPalette::Base));
This is my result:
535

Looks like there is another background before the widget's background. I've already tried removing scene's background, but it didn't work either.. :confused:

jacek
9th August 2006, 21:21
Looks like there is another background before the widget's background.
It's hard to tell what's wrong without seeing the code, but setPalette() worked for me. Maybe you change the background afterwards? Or it's not a background, but part of the item?