PDA

View Full Version : Problem changing background of a child QWidget in Qt 4.1.X



thawkins
2nd June 2006, 19:05
Hi all,

I just recently moved from Qt 4.0.0/4.0.1 to Qt 4.1.3. I had some code that instantiated a bunch of QWidget derived classes to be children of another QWidget derived class. In Qt 4.0.X the code produced the results shown in the attached Qt400Result.jpg. In Qt 4.1.3, the results of the same code is shown in Qt413Result.jpg. I intentionally manipulate the palettes after the initial show() is called, and not during the constructors, yet it seems like I can't change the childrens palettes from what the parent was set to.

I know the change log for Qt 4.1.0 (http://www.trolltech.com/developer/notes/changes/changes-4.1.0/) said "Child widgets now always inherit the contents of their parent.", but I should be able to change the childrens palette yes? I've attached a stripped down working version of my code that reproduces the problem, any help would be appreciated.

I'm using Qt 4.1.3 on an x11 platform

The cpp file (ParentTestApp.cpp)


#include <ParentTestApp.h>

// ------------------------------------------------------------
// ------------------------------------------------------------
//main program execution loop
// ------------------------------------------------------------
// ------------------------------------------------------------
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ParentClass pParentTest;

// instantiating the main class for this app and
// calling ColorizeChildren() to change the childrens
// background color to green
pParentTest.show();
pParentTest.ColorizeChildren();

//go thread go!
return app.exec();
}


// ------------------------------------------------------------
// ------------------------------------------------------------
// ParentClass implementation
// ------------------------------------------------------------
// ------------------------------------------------------------

//default constructor for the main class for this app
ParentClass::ParentClass()
{
int i;
QPalette qTempPalette;

//setting the background color to black. I know that in
//4.1.3 QPalette::Background is replaced by QPalette::Window however,
//same results occur in 4.1.3 with either enum
qTempPalette = this->palette();
qTempPalette.setColor(QPalette::Background,Qt::bla ck);
this->setPalette(qTempPalette);

//changing the size of the main widget
resize(400,100);

//making the children and placing them into a horizontal box
//layout
for (i = 0; i < 4; i++)
{
this->pChildren[i] = new ChildClass(this);
this->pChildren[i]->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ig nored);
this->qMainLayout.addWidget(this->pChildren[i]);
}

//setting the layout
this->setLayout(&this->qMainLayout);
}

//basic destructor type class
ParentClass::~ParentClass()
{
int i;

//buh-bye children!
for (i = 0; i < 4; i++) delete this->pChildren[i];
}

void ParentClass::ColorizeChildren(void)
{
int i;

//giving the children color
for (i = 0; i < 4; i++) this->pChildren[i]->ColorMe();
}

// ------------------------------------------------------------
// ------------------------------------------------------------
// ChildClass implementation
// ------------------------------------------------------------
// ------------------------------------------------------------
//basic constructor
ChildClass::ChildClass(QWidget *pParent) : QWidget(pParent)
{
}


//changes the background color of the widget
void ChildClass::ColorMe(void)
{
QPalette qTempPalette;

//setting the background color to green. I know that in
//4.1.3 QPalette::Background is replaced by QPalette::Window however,
//same results occur in 4.1.3 with either enum
qTempPalette = this->palette();
qTempPalette.setColor(QPalette::Background,Qt::gre en);
this->setPalette(qTempPalette);
}


The h file (ParentTestApp.h)


#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QPalette>

// ------------------------------------------------------------
// ------------------------------------------------------------
// Object prototypes
// ------------------------------------------------------------
// ------------------------------------------------------------
class ChildClass;
class ParentClass;

class ParentClass : public QWidget
{
Q_OBJECT;

public:
ParentClass();
~ParentClass();
void ColorizeChildren(void);

private:
ChildClass *pChildren[4];
QHBoxLayout qMainLayout;
};

//the children of ParentClass
class ChildClass : public QWidget
{
Q_OBJECT;

public:
ChildClass(QWidget *pParent);
void ColorMe(void);
};

jacek
2nd June 2006, 19:12
Add "setAutoFillBackground( true )" to ChildClass constructor.

thawkins
2nd June 2006, 19:28
Add "setAutoFillBackground( true )" to ChildClass constructor.


Excellent, thank you, worked perfectly. I noticed that property is new to Qt 4.1.3. Is there any way of adding some type of conditional compile statement into the code which recognizes Qt 4.0.X versus Qt 4.1.3? I would like to have the code maintain backward compatability (transparently if possible) with older revisions of Qt 4.0.X or higher.


Thanks you for your help.

jpn
2nd June 2006, 19:54
Is there any way of adding some type of conditional compile statement into the code which recognizes Qt 4.0.X versus Qt 4.1.3? I would like to have the code maintain backward compatability (transparently if possible) with older revisions of Qt 4.0.X or higher.
QT_VERSION (http://doc.trolltech.com/4.1/qtglobal.html#QT_VERSION)

newby
16th August 2006, 23:32
I am still having the same problem with QFrame changing the background color.

I am using Qt 4.1.1 with Visual Studio .NET 2003.
It works perfectly fine with Visual Studio 6.0.

Has anybody seen this?

jacek
16th August 2006, 23:36
I am using Qt 4.1.1 with Visual Studio .NET 2003.
It works perfectly fine with Visual Studio 6.0.
Do you use the same Qt version with both of these toolchains?

newby
16th August 2006, 23:52
Yes. I am using the same Qt 4.1.1 with both Visual Studio .NET 2003 and Visual Studio 6.0

newby
16th August 2006, 23:55
It displays the correct color when the child QFrame is selected with mouse click or all children QFrames are selected.

jacek
17th August 2006, 00:10
It displays the correct color when the child QFrame is selected with mouse click or all children QFrames are selected.
Does it work when you start your application like this:

C:...\> app.exe -style plastique
or

C:...\> app.exe -style windows?

newby
17th August 2006, 14:57
Yes. It works with both those styles.

jacek
17th August 2006, 16:12
Yes. It works with both those styles.
So most likely the problem lies in QWindowsXPStyle. Try a newer Qt version to see if the problem was fixed.