Results 1 to 2 of 2

Thread: Background problem that propagates badly to chils widgets

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Exclamation Background problem that propagates badly to chils widgets

    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):

    [ATTACH]screenshot..jpg[/ATTACH]

    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.

    Qt Code:
    1. /**
    2.  * @file mainwindow.h
    3.  */
    4. #include <QtGui>
    5. #include "widget.h"
    6.  
    7. class MainWindow : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. MainWindow(QWidget *parent = 0)
    12. : QWidget(parent)
    13. {
    14. setBackgroundRole(QPalette::Window);
    15. QPalette palette;
    16. palette.setBrush(backgroundRole(),
    17. QBrush(QImage(":/icons/background.png")));
    18. setPalette(palette);
    19. setAutoFillBackground(true);
    20.  
    21. Widget *widget = new Widget(this);
    22.  
    23. QVBoxLayout *layout = new QVBoxLayout;
    24. layout->setContentsMargins(0, 0, 0, 0);
    25. layout->addWidget(widget);
    26. layout->addStretch(1);
    27. setLayout(layout);
    28. }
    29. };
    To copy to clipboard, switch view to plain text mode 

    Here the code for Widget class:

    Qt Code:
    1. /**
    2. * @file widget.h
    3. */
    4.  
    5. #ifndef WIDGET_H
    6. #define WIDGET_H
    7.  
    8. #include <QtGui>
    9. #include "widget.h"
    10. #include "pages.h"
    11.  
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Widget(QWidget *parent = 0)
    17. : QWidget(parent), index(0)
    18. {
    19. setFixedSize(320, 240);
    20. setBackgroundRole(QPalette::Window);
    21.  
    22. selectedGroupBox = new QGroupBox;
    23. selectedGroupBox->setFlat(true);
    24.  
    25. selectedLayout = new QGridLayout;
    26. selectedGroupBox->setLayout(selectedLayout);
    27.  
    28. scrollArea = new QScrollArea();
    29. scrollArea->setWidgetResizable(true);
    30.  
    31. selectWidgets();
    32.  
    33. buttonBox = new QDialogButtonBox;
    34. buttonBox->setOrientation(Qt::Vertical);
    35.  
    36. rotateButton = buttonBox->addButton(tr("Select &Widgets"),
    37. QDialogButtonBox::ActionRole);
    38.  
    39. connect(rotateButton, SIGNAL(clicked()), this, SLOT(selectWidgets()));
    40.  
    41. mainLayout = new QGridLayout;
    42. mainLayout->addWidget(selectedGroupBox, 0, 0);
    43. mainLayout->setRowStretch(1, 1);
    44. mainLayout->addWidget(buttonBox, 0, 1);
    45. mainLayout->setContentsMargins(0, 0, 0, 0);
    46.  
    47. mainLayout->setSizeConstraint(QLayout::SetMinimumSize);
    48. setLayout(mainLayout);
    49. }
    50.  
    51. private slots:
    52. void selectWidgets()
    53. {
    54. switch (index) {
    55. case 0: widget = new ConfigPage;
    56. break;
    57. case 1: widget = new UpdatePage;
    58. break;
    59. case 2: widget = new QueryPage;
    60. break;
    61. }
    62.  
    63. scrollArea->setWidget(widget);
    64. scrollArea->ensureWidgetVisible(widget);
    65. selectedLayout->addWidget(scrollArea, 0, 0);
    66. index = (++index) % 3;
    67. }
    68.  
    69. private:
    70. QDialogButtonBox *buttonBox;
    71. QGroupBox *selectedGroupBox;
    72. QWidget *widget;
    73. int index;
    74. QPushButton *rotateButton;
    75. QGridLayout *mainLayout;
    76. QGridLayout *selectedLayout;
    77. QScrollArea *scrollArea;
    78. };
    79.  
    80. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Sep 2008
    Location
    Munich
    Posts
    32
    Thanked 8 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Background problem that propagates badly to chils widgets

    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...ackground-prop.

    Take care

  3. The following user says thank you to jryannel for this useful post:

    scrat75 (22nd May 2010)

Similar Threads

  1. background-image on a set of widgets
    By asieriko in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2010, 07:59
  2. Replies: 1
    Last Post: 18th September 2009, 10:32
  3. I recently used QT i need help badly
    By miguel_mark in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2007, 11:15
  4. dark gray widgets/background
    By momesana in forum Qt Programming
    Replies: 8
    Last Post: 17th June 2007, 09:39
  5. HDD & BIOS : why the hell is that so badly designed???
    By fullmetalcoder in forum General Discussion
    Replies: 13
    Last Post: 19th April 2006, 12:37

Tags for this Thread

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.