Results 1 to 10 of 10

Thread: QWidget::resizeEvent is not called

  1. #1
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default QWidget::resizeEvent is not called

    hi,

    so, for the last few hours or so i tried to get the layout of some custom widgets right.
    i got a widget, that serves as a container for other widgets and when resizing, i want to resize the children, too.

    i overloaded resizeEvent for that - but it never gets called.

    declaration:
    Qt Code:
    1. protected:
    2. virtual void resizeEvent(QResizeEvent *i_pEvent);
    To copy to clipboard, switch view to plain text mode 

    i really don't know what other code to post here. i call resize(...), the widget actually gets resized, but resizeEvent never gets called.

    are there any side effects involved? some property or whatever that has to be set?

    btw: i know, i could use layouts for that purpose. but then i could open another thread on why sizeHint() of the child widgets just never gets called, resulting in a messed up layout.

  2. #2
    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: QWidget::resizeEvent is not called

    Can you make a small, simplest possible example which reproduces the problem?
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QWidget::resizeEvent is not called

    so, i made a sample targeting both the resizing and layout issue.

    basically, i subclassed a QScrollArea, what sets a container widget (subclassed from QFrame) as the viewport.
    the container widget has a QVBoxLayout holding Obj - widgets (also subclassed from QFrame).

    now, several problems arise:
    the container (ScrollArea's viewport) gets resized although it shouldn't, while the container's resizeEvent() never gets called.
    the Objs inside the container seem to align more or less OK, except the height of the minimumSizeHint is underrun, but i'm not sure, if this is avoidable.

    Qt Code:
    1. #include <QApplication>
    2. #include <QScrollArea>
    3. #include <QFrame>
    4. #include <QVBoxLayout>
    5. #include <QDebug>
    6.  
    7. // the objects for the container
    8. // should behave like pushbuttons, so horizontal resizig but fixed vertical size
    9. // the vertical sizeHint is used for the max size but underrun for the min size
    10. class Obj : public QFrame
    11. {
    12. public:
    13. Obj(QWidget *i_pParent = NULL):
    14. QFrame(i_pParent)
    15. {
    16. this->setFrameStyle(QFrame::Raised | QFrame::Box);
    17.  
    18. this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    19.  
    20. this->updateGeometry();
    21. }
    22.  
    23. virtual QSize sizeHint() const
    24. {
    25. return QSize(100, 20);
    26. }
    27.  
    28. virtual QSize minimumSizeHint() const
    29. {
    30. return this->sizeHint();
    31. }
    32. };
    33.  
    34.  
    35. // hold the objects in a vertical layout
    36. class Container : public QFrame
    37. {
    38. public:
    39. Container(QWidget *i_pParent = NULL):
    40. QFrame(i_pParent)
    41. {
    42. this->setFrameStyle(QFrame::Sunken | QFrame::Box);
    43.  
    44. this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    45.  
    46. this->setLayout(new QVBoxLayout(this));
    47.  
    48. for(int i = 0; i < 10; ++i)
    49. {
    50. this->layout()->addWidget(new Obj(this));
    51. }
    52.  
    53. this->updateGeometry();
    54. }
    55.  
    56. // never gets called
    57. virtual QSize sizeHint() const
    58. {
    59. return QSize(1000, 1000);
    60. }
    61.  
    62. // never gets called
    63. virtual QSize minimumSizeHint() const
    64. {
    65. return this->sizeHint();
    66. }
    67.  
    68. protected:
    69. // never gets called
    70. virtual void resizeEvent(QResizeEvent *i_pEvent)
    71. {
    72.  
    73. }
    74. };
    75.  
    76. class ScrollArea : public QScrollArea
    77. {
    78. public:
    79. Container *m_pContainer;
    80.  
    81. ScrollArea(QWidget *i_pParent = NULL):
    82. QScrollArea(i_pParent)
    83. {
    84. this->m_pContainer = new Container(this);
    85. this->setViewport(this->m_pContainer);
    86.  
    87. this->setWidgetResizable(false);
    88.  
    89. // some messed up value "QRect(9,9 -18x-18)"
    90. qDebug() << this->m_pContainer->layout()->contentsRect();
    91. // QSize(1000,1000) as it ought to be
    92. qDebug() << this->m_pContainer->size();
    93. // false
    94. qDebug() << this->widgetResizable();
    95. }
    96. };
    97.  
    98. int main(int argc, char **argv)
    99. {
    100. QApplication p_App(argc, argv);
    101.  
    102. ScrollArea p_ScrollArea;
    103.  
    104. p_ScrollArea.resize(250, 250);
    105.  
    106. // viewport downsized, no scollbars
    107. p_ScrollArea.show();
    108.  
    109. // QSize(230,230) - where did the QSize(1000, 1000) go?
    110. // resizeEvent wasn't called
    111. // and it shouldn't resize at all!
    112. qDebug() << p_ScrollArea.m_pContainer->size();
    113.  
    114. return p_App.exec();
    115. }
    To copy to clipboard, switch view to plain text mode 

  4. #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: QWidget::resizeEvent is not called

    Viewport events are delivered through viewportEvent(). QAbstractScrollArea filters all events of the viewport and delivers them through this specialized event handler.

    By default QScrollArea resizes the content widget to its sizeHint() by the time its added. If the size hint changes, you will have to call for example adjustSize() yourself. This behavior can be changed by setting widgetResizable property to true. In this mode QScrollArea will resize the content widget on the fly but still honoring its min/max sizes etc.
    J-P Nurmi

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

    maximAL (3rd February 2008)

  6. #5
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Thumbs up Re: QWidget::resizeEvent is not called

    Quote Originally Posted by jpn View Post
    Viewport events are delivered through viewportEvent(). QAbstractScrollArea filters all events of the viewport and delivers them through this specialized event handler.
    ok, now that explains A LOT.
    Quote Originally Posted by jpn View Post
    By default QScrollArea resizes the content widget to its sizeHint() by the time its added.
    hmm, in my example it doesn't

  7. #6
    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: QWidget::resizeEvent is not called

    Hmm, I don't see QScrollArea::setWidget() being called anywhere.
    J-P Nurmi

  8. #7
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QWidget::resizeEvent is not called

    oh, you are right. but what exactly is the difference between setViewport and setWidget? i mean, both seems to set the central widget?!

    in my original program i used setViewport since i subclassed a QAbstractItemView, what again is a subclass of QAbstractScollArea...

  9. #8
    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: QWidget::resizeEvent is not called

    Viewport is a scrollable "viewport" to the content. You will see the content through the viewport. The viewport widget is laid next to scroll bars. This is usually a plain QWidget. The content widget, however, as set with QScrollArea::setWidget() is the actual scrollable content. I'm quite sure you want to set the content widget instead of a custom viewport.
    J-P Nurmi

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

    maximAL (3rd February 2008)

  11. #9
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Lightbulb Re: QWidget::resizeEvent is not called

    so, to sum it up, that means when subclassing QAbstractItemView, i shouldn't set the viewport, but instead add the content as a child to the viewport, right?

    ok, obviously many things have gone wrong at once in my code *goes refactoring*

  12. #10
    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: QWidget::resizeEvent is not called

    Quote Originally Posted by maximAL View Post
    so, to sum it up, that means when subclassing QAbstractItemView, i shouldn't set the viewport, but instead add the content as a child to the viewport, right?
    I'm not sure what kind of content do you mean, but for QAbstractItemView there are index widgets, editors, persistent editors, custom delegates... but of course, some kind of overlays could be implemented as children of viewport too.
    J-P Nurmi

Similar Threads

  1. connect() returns true but slot not called
    By OriginalCopy in forum Qt Programming
    Replies: 6
    Last Post: 4th November 2007, 18:54
  2. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 09:52
  3. Drag and Drop, dropEvent not being called?
    By steg90 in forum Qt Programming
    Replies: 36
    Last Post: 22nd May 2007, 07:03
  4. Replies: 4
    Last Post: 10th March 2007, 18:01
  5. QWidget::paintEngine() should no longer be called
    By Greeny in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2007, 10:12

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.