Results 1 to 11 of 11

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

  1. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22: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.