Results 1 to 18 of 18

Thread: Resize widget force layout resizing

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Resize widget force layout resizing

    Hi,

    I have a Dialog that contain one Widget and the tipical OK_Cancel buttons. The widgets are in a vertical layout.

    I want the dialog to be resized automatically when I resize the Widget . The Widget have to display an image and I want to resize it to a proportional size forcing the dialog to resize.

    Is it posible or have I to change the dialog size manually?

    The widget is a special one similar to OpenGL widget.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    I think it micht happen when you set the minimumSize of your widget. They layout will try to accomodate it and probably resize the dialog. Try and see and let me know.

  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    implement the resizeEvent function and do all relevant changes there, it shouldn't be difficult

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    Setting the minimum size of the widget don't work.

    Reimplementing the sizeEvent it's not easy as you said, I think. How can I know all the widgets sizes including layout sizes?
    Is there another way to do it? Or if I have to reimplement resizeEvent, how can I get all sizes?

    Thanks,
    Òscar Llarch i Galán

  5. #5
    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: Resize widget force layout resizing

    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    Thanks JPN. I think that this is what I need.

    Good job on this extension library.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    Not exactly waht I need. As I read from docs, "QxtLetterBoxWidget preserves the aspect ratio of its content widget". What I need is to resize the widget and also force the dialog to adjust the size to show the widgets on its new size.

    Really can QxtLetterBoxWidget helps me to this job?

    Thanks,
    Òscar Llarch i Galán

  8. #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: Resize widget force layout resizing

    Quote Originally Posted by ^NyAw^ View Post
    What I need is to resize the widget and also force the dialog to adjust the size to show the widgets on its new size.
    You can make the dialog non-resizable:
    Qt Code:
    1. dialog->layout()->setSizeConstraint(QLayout::SetFixedSize);
    To copy to clipboard, switch view to plain text mode 
    and it will follow the size hint of the content. You just need to implement sizeHint() for your content widget and call updateGeometry() whenever the size hint changes.

    Really can QxtLetterBoxWidget helps me to this job?
    Well, why do you care about the dialog size when the aspect ratio of the content is correct?
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    Quote Originally Posted by jpn View Post
    and it will follow the size hint of the content. You just need to implement sizeHint() for your content widget and call updateGeometry() whenever the size hint changes.
    The widget not will resize itself. On myDialog constructor I need to change the size of myWidget:
    Qt Code:
    1. myDialog::myDialog(...)
    2. {
    3. ...
    4. int iWidth = obj->getWidth();
    5. int iHeight = obj->getHeight();
    6. float dAspRat = (float)iWidth/(float)iHeight;
    7.  
    8. int iWidthWidget, iHeightWidget;
    9. iWidthWidget = 640;
    10. iHeightWidget = (640/dAspRat );
    11.  
    12. m_pqWidget->resize(iWidthWidget,iHeightWidget);
    13. ...
    14.  
    15. //Here I want to force "this"(dialog) to fit the widgets size.
    16. }
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by jpn View Post
    Well, why do you care about the dialog size when the aspect ratio of the content is correct?
    Because I want to force the Widget to be 640 width and Y width(that depends on the aspect ratio).

    Thanks,
    Òscar Llarch i Galán

  10. #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: Resize widget force layout resizing

    Quote Originally Posted by ^NyAw^ View Post
    The widget not will resize itself.
    I said you have to implement sizeHint(), not to call resize(). The latter has no effect together with QLayout::SetFixedSize. The layout will follow the size hint, strictly.
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    Qt Code:
    1. myLayout::myLayout(...)
    2. {
    3. ...
    4. this->layout()->setSizeConstraint(QLayout::SetFixedSize);
    5. ...
    6. }
    7.  
    8. QSize myWidget::sizeHint()
    9. {
    10. //Here I have to calculate the size -> 640xY
    11. }
    To copy to clipboard, switch view to plain text mode 
    The "sizeHint" method of myWidget is never called.

    I don't really understand "sizeHint" behaviour, maybe because I'm not english and don't understand it's function.

    See the attached image. I have this dialog and I use the QWidget(that is difficult) to see(it is on top of the other widgets). I use this QWidget as the parent of my widget(because I have not created the QtDesigner plugin).

    Thanks,
    Attached Images Attached Images
    Last edited by ^NyAw^; 10th February 2009 at 17:57. Reason: Atacched image forget!
    Òscar Llarch i Galán

  12. #12
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    u forgot to attach the "attached image"

  13. #13
    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: Resize widget force layout resizing

    Ok, here's an example dummy widget that changes its size hint periodically:
    Qt Code:
    1. class Widget : public QWidget
    2. {
    3. public:
    4. Widget(QWidget* parent = 0) : QWidget(parent), sh(320, 240) {
    5. setStyleSheet("background: red"); // just to make it easier to see
    6. startTimer(2000);
    7. }
    8. QSize sizeHint() const { return sh; } // notice "const"
    9.  
    10. protected:
    11. void timerEvent(QTimerEvent* /*event*/) {
    12. sh += QSize(40, 30);
    13. if (sh.width() > 640)
    14. sh = QSize(320, 240);
    15. updateGeometry(); // inform that the size hint has changed
    16. }
    17.  
    18. private:
    19. QSize sh;
    20. };
    To copy to clipboard, switch view to plain text mode 
    Now, a dummy dialog that contains the above widget and a dialog button box:
    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QDialog dialog;
    5. QVBoxLayout* layout = new QVBoxLayout(&dialog);
    6. layout->setSizeConstraint(QLayout::SetFixedSize); // make the layout follow size hint
    7. layout->addWidget(new Widget);
    8. layout->addWidget(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel));
    9. dialog.setLayout(layout);
    10. return dialog.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    This makes the whole dialog follow the size hint of the "red widget". Is this what you wanted?
    J-P Nurmi

  14. #14
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    I've been trying it but I'm not able to make it works. Why the Widget have a timer that informs that its sizeHint have changed? Can't it be done on "resizeEvent", so when I call resize of the Widget it informs that the sizeHint have changed? Althought, sizeHint method is never called

    It's so dificult to obtain the size of the internal widgets and the "spacing" between them and then resize the dialog to a calculated size?

    Thanks,
    Òscar Llarch i Galán

  15. #15
    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: Resize widget force layout resizing

    Quote Originally Posted by ^NyAw^ View Post
    Why the Widget have a timer that informs that its sizeHint have changed?
    I said it's a dummy widget that changes its size hint periodically. Just to demonstrate that the whole dialog automatically follows the size hint change.

    Can't it be done on "resizeEvent", so when I call resize of the Widget it informs that the sizeHint have changed?
    No, you can't resize widgets by hand when they are in layouts. Layouts own geometries of widgets that are installed in layouts.

    The layout calls sizeHint() first, calculates suitable geometry and calls setGeometry(). So if you start changing the size hint in resizeEvent(), you're already one round late and the layout has to do a complete recalculation. I recommend reading the Layout Classes document to get a basic understanding how layouts work.

    Althought, sizeHint method is never called
    In your example snippet it's missing the "const" keyword, thus you are not reimplementing the virtual method defined in the base class. In C++, to reimplement a virtual function, the function signature must match exactly, including function return value, parameter types and function constness.

    It's so dificult to obtain the size of the internal widgets and the "spacing" between them and then resize the dialog to a calculated size?
    That's a clear sign that you're doing something at wrong level. Let the layout calculate those things, just inform the layout about proper size for your widget by implementing the sizeHint() method properly.
    J-P Nurmi

  16. #16
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    Hi,

    It works now! Now I calculate the expected "sizeHint" on myWidget depending on proportional value.

    I was confused on "sizeHint" signature.
    So,
    Qt Code:
    1. QSize sizeHint() const;
    To copy to clipboard, switch view to plain text mode 
    is different to
    Qt Code:
    1. const QSize sizeHint();
    To copy to clipboard, switch view to plain text mode 

    I used to use the second signature and don't know wich is the difference.

    Thanks for your help.
    Òscar Llarch i Galán

  17. #17
    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: Resize widget force layout resizing

    Quote Originally Posted by ^NyAw^ View Post
    I used to use the second signature and don't know wich is the difference.
    The former is a const function meaning that it won't change the state of the object, the latter just returns a const value. More info, C++ FAQ Lite: Const correctness.
    J-P Nurmi

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

    ^NyAw^ (11th February 2009)

  19. #18
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget force layout resizing

    well, its very important to know the difference between the two.

    const QSize sizeHint() means the function would return a const QSize, whereas

    QSize sizeHint() const means the function would not modify any of the data members or are read-only functions. such functions can be called for a const and non const objects and such functions have const this pointers

  20. The following user says thank you to talk2amulya for this useful post:

    ^NyAw^ (11th February 2009)

Similar Threads

  1. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  2. Replies: 5
    Last Post: 25th May 2008, 08:22
  3. Move and resize with layout
    By waediowa in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2008, 08:16
  4. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52
  5. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00

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.