PDA

View Full Version : Background problem that propagates badly to chils widgets



scrat75
21st May 2010, 17:44
Hi,
I have a problem handling background in custom widget. I want to post here my code and attach a screenshoot of the final application to show you the problem I'm facing.

I start with the screenshot (bad result):

4665

I defined a main composed class, MainWindow (in mainwindow.h) that set in its constructor a background obtained from a image. Then a child subclass, Widget, are created and added to a vertical layout.



/**
* @file mainwindow.h
*/
#include <QtGui>
#include "widget.h"

class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0)
: QWidget(parent)
{
setBackgroundRole(QPalette::Window);
QPalette palette;
palette.setBrush(backgroundRole(),
QBrush(QImage(":/icons/background.png")));
setPalette(palette);
setAutoFillBackground(true);

Widget *widget = new Widget(this);

QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(widget);
layout->addStretch(1);
setLayout(layout);
}
};


Here the code for Widget class:



/**
* @file widget.h
*/

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>
#include "widget.h"
#include "pages.h"

class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0)
: QWidget(parent), index(0)
{
setFixedSize(320, 240);
setBackgroundRole(QPalette::Window);

selectedGroupBox = new QGroupBox;
selectedGroupBox->setFlat(true);

selectedLayout = new QGridLayout;
selectedGroupBox->setLayout(selectedLayout);

scrollArea = new QScrollArea();
scrollArea->setWidgetResizable(true);

selectWidgets();

buttonBox = new QDialogButtonBox;
buttonBox->setOrientation(Qt::Vertical);

rotateButton = buttonBox->addButton(tr("Select &Widgets"),
QDialogButtonBox::ActionRole);

connect(rotateButton, SIGNAL(clicked()), this, SLOT(selectWidgets()));

mainLayout = new QGridLayout;
mainLayout->addWidget(selectedGroupBox, 0, 0);
mainLayout->setRowStretch(1, 1);
mainLayout->addWidget(buttonBox, 0, 1);
mainLayout->setContentsMargins(0, 0, 0, 0);

mainLayout->setSizeConstraint(QLayout::SetMinimumSize);
setLayout(mainLayout);
}

private slots:
void selectWidgets()
{
switch (index) {
case 0: widget = new ConfigPage;
break;
case 1: widget = new UpdatePage;
break;
case 2: widget = new QueryPage;
break;
}

scrollArea->setWidget(widget);
scrollArea->ensureWidgetVisible(widget);
selectedLayout->addWidget(scrollArea, 0, 0);
index = (++index) % 3;
}

private:
QDialogButtonBox *buttonBox;
QGroupBox *selectedGroupBox;
QWidget *widget;
int index;
QPushButton *rotateButton;
QGridLayout *mainLayout;
QGridLayout *selectedLayout;
QScrollArea *scrollArea;
};

#endif // WIDGET_H


The Widget class uses others three child classes derived from QWidget, contained in pages.h: they are ConfigPage, QueryPage and UpdatePage and use various othe widgets from Qt library. They are layed into a QScroll area in the Widget class every time user pushs the rotateButton.

I don't like the way the background image propagates through the Widget object and its sub childs.
I think the problem is due to the fact that child widgets "import" the background image applying its window constraint and properties (size, styles, etc) when reproducing the background image.

How can I solve the problem?

Thaks in advance for your help or suggestions.

jryannel
22nd May 2010, 07:54
I would expect it's an issue of a widget not drawing it's background. Maybe this will help: http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#autoFillBackground-prop.

Take care