Results 1 to 11 of 11

Thread: Problem changing background of a child QWidget in Qt 4.1.X

  1. #1
    Join Date
    Jan 2006
    Location
    Florida
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Problem changing background of a child QWidget in Qt 4.1.X

    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/n...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)
    Qt Code:
    1. #include <ParentTestApp.h>
    2.  
    3. // ------------------------------------------------------------
    4. // ------------------------------------------------------------
    5. //main program execution loop
    6. // ------------------------------------------------------------
    7. // ------------------------------------------------------------
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. ParentClass pParentTest;
    12.  
    13. // instantiating the main class for this app and
    14. // calling ColorizeChildren() to change the childrens
    15. // background color to green
    16. pParentTest.show();
    17. pParentTest.ColorizeChildren();
    18.  
    19. //go thread go!
    20. return app.exec();
    21. }
    22.  
    23.  
    24. // ------------------------------------------------------------
    25. // ------------------------------------------------------------
    26. // ParentClass implementation
    27. // ------------------------------------------------------------
    28. // ------------------------------------------------------------
    29.  
    30. //default constructor for the main class for this app
    31. ParentClass::ParentClass()
    32. {
    33. int i;
    34. QPalette qTempPalette;
    35.  
    36. //setting the background color to black. I know that in
    37. //4.1.3 QPalette::Background is replaced by QPalette::Window however,
    38. //same results occur in 4.1.3 with either enum
    39. qTempPalette = this->palette();
    40. qTempPalette.setColor(QPalette::Background,Qt::black);
    41. this->setPalette(qTempPalette);
    42.  
    43. //changing the size of the main widget
    44. resize(400,100);
    45.  
    46. //making the children and placing them into a horizontal box
    47. //layout
    48. for (i = 0; i < 4; i++)
    49. {
    50. this->pChildren[i] = new ChildClass(this);
    51. this->pChildren[i]->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    52. this->qMainLayout.addWidget(this->pChildren[i]);
    53. }
    54.  
    55. //setting the layout
    56. this->setLayout(&this->qMainLayout);
    57. }
    58.  
    59. //basic destructor type class
    60. ParentClass::~ParentClass()
    61. {
    62. int i;
    63.  
    64. //buh-bye children!
    65. for (i = 0; i < 4; i++) delete this->pChildren[i];
    66. }
    67.  
    68. void ParentClass::ColorizeChildren(void)
    69. {
    70. int i;
    71.  
    72. //giving the children color
    73. for (i = 0; i < 4; i++) this->pChildren[i]->ColorMe();
    74. }
    75.  
    76. // ------------------------------------------------------------
    77. // ------------------------------------------------------------
    78. // ChildClass implementation
    79. // ------------------------------------------------------------
    80. // ------------------------------------------------------------
    81. //basic constructor
    82. ChildClass::ChildClass(QWidget *pParent) : QWidget(pParent)
    83. {
    84. }
    85.  
    86.  
    87. //changes the background color of the widget
    88. void ChildClass::ColorMe(void)
    89. {
    90. QPalette qTempPalette;
    91.  
    92. //setting the background color to green. I know that in
    93. //4.1.3 QPalette::Background is replaced by QPalette::Window however,
    94. //same results occur in 4.1.3 with either enum
    95. qTempPalette = this->palette();
    96. qTempPalette.setColor(QPalette::Background,Qt::green);
    97. this->setPalette(qTempPalette);
    98. }
    To copy to clipboard, switch view to plain text mode 

    The h file (ParentTestApp.h)
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QHBoxLayout>
    4. #include <QPalette>
    5.  
    6. // ------------------------------------------------------------
    7. // ------------------------------------------------------------
    8. // Object prototypes
    9. // ------------------------------------------------------------
    10. // ------------------------------------------------------------
    11. class ChildClass;
    12. class ParentClass;
    13.  
    14. class ParentClass : public QWidget
    15. {
    16. Q_OBJECT;
    17.  
    18. public:
    19. ParentClass();
    20. ~ParentClass();
    21. void ColorizeChildren(void);
    22.  
    23. private:
    24. ChildClass *pChildren[4];
    25. QHBoxLayout qMainLayout;
    26. };
    27.  
    28. //the children of ParentClass
    29. class ChildClass : public QWidget
    30. {
    31. Q_OBJECT;
    32.  
    33. public:
    34. ChildClass(QWidget *pParent);
    35. void ColorMe(void);
    36. };
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Add "setAutoFillBackground( true )" to ChildClass constructor.

  3. The following 2 users say thank you to jacek for this useful post:

    Carnuum (10th August 2006), thawkins (2nd June 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Florida
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Quote Originally Posted by jacek
    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.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Quote Originally Posted by thawkins
    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
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    thawkins (2nd June 2006)

  7. #5
    Join Date
    Jun 2006
    Location
    USA
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Problem changing background of a child QWidget in Qt 4.1.X

    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?

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Quote Originally Posted by newby
    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?

  9. #7
    Join Date
    Jun 2006
    Location
    USA
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Yes. I am using the same Qt 4.1.1 with both Visual Studio .NET 2003 and Visual Studio 6.0

  10. #8
    Join Date
    Jun 2006
    Location
    USA
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    It displays the correct color when the child QFrame is selected with mouse click or all children QFrames are selected.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Quote Originally Posted by newby
    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
    ?

  12. #10
    Join Date
    Jun 2006
    Location
    USA
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Yes. It works with both those styles.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem changing background of a child QWidget in Qt 4.1.X

    Quote Originally Posted by newby
    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.

Similar Threads

  1. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 23:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.