Results 1 to 17 of 17

Thread: Auto resize Widget to maximum avail. space

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Auto resize Widget to maximum avail. space

    I have a QWidget, qwtplot class (qcurveplot) which contains only one qwtplot widget.

    I want this widget to maximize within the dialog it is placed and also the qwtplot widget to maximize within its containing widget.

    The Layout examples of Qt however(http://doc.trolltech.com/4.3/examples.html#layouts) are not helpfull since the do not deal with this problem at all (as far as I can see).

    I have looked though the documentation now for approx 3 hours and tried some code but could not find any solution.

    Could someone be so kind to give me an example to get this absolutely simple task to work?

    What I have so far is an UI file which generates the following code:
    Qt Code:
    1. class Ui_QCurvePlot
    2. {
    3. public:
    4. QWidget *gridLayout;
    5. QGridLayout *gridLayout1;
    6.  
    7. void setupUi(QWidget *QCurvePlot)
    8. {
    9. if (QCurvePlot->objectName().isEmpty())
    10. QCurvePlot->setObjectName(QString::fromUtf8("QCurvePlot"));
    11. QCurvePlot->resize(487, 297);
    12. QCurvePlot->setAutoFillBackground(false);
    13. gridLayout = new QWidget(QCurvePlot);
    14. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    15. gridLayout->setGeometry(QRect(40, 10, 241, 251));
    16. gridLayout1 = new QGridLayout(gridLayout);
    17. gridLayout1->setObjectName(QString::fromUtf8("gridLayout1"));
    18. gridLayout1->setContentsMargins(0, 0, 0, 0);
    19. qwtPlot = new QwtPlot(gridLayout);
    20. qwtPlot->setObjectName(QString::fromUtf8("qwtPlot"));
    21. QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
    22. sizePolicy.setHorizontalStretch(0);
    23. sizePolicy.setVerticalStretch(0);
    24. sizePolicy.setHeightForWidth(qwtPlot->sizePolicy().hasHeightForWidth());
    25. qwtPlot->setSizePolicy(sizePolicy);
    26.  
    27. gridLayout1->addWidget(qwtPlot, 0, 0, 1, 1);
    To copy to clipboard, switch view to plain text mode 

    and I tried the follwing code which did not lead to a solution at all:
    Qt Code:
    1. QCurvePlot::QCurvePlot(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QWidget(parent, flags)
    2. {
    3. setupUi(this);
    4.  
    5. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    6. sizePolicy.setHorizontalStretch(0);
    7. sizePolicy.setVerticalStretch(0);
    8. this->setSizePolicy(sizePolicy);
    9.  
    10. gridLayout1->setColumnStretch ( 0, 20);
    11. gridLayout1->setRowStretch (0, 20);
    12.  
    13. gridLayout1->setAlignment(Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 
    the last line is the only one which does the right thing.

    The containing widget however has a much to small size which does not match with the size set in the designer and it is not as wide as possible. The qwtwidget as well is not fit within the size of the containing widget.

    Matthias
    Last edited by pospiech; 14th April 2008 at 13:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    You want to fill all the space of the parent widget with the child widget or expand the parent widget for the whole child widget to fit?

  3. #3
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    You want to fill all the space of the parent widget with the child widget or expand the parent widget for the whole child widget to fit?
    The first.

    I now tried to achieve such an effect by adjusting the Geometry manually
    Qt Code:
    1. void MainWindowImpl::resizeEvent(QResizeEvent * event)
    2. {
    3. horizontalLayout->setGeometry(QRect(0, 0, this->width(), horizontalLayout->minimumSizeHint().height()));;
    4. gridLayout->setGeometry(QRect(0, horizontalLayout->height(), this->width(), this->height()-horizontalLayout->height()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    The gridLayout is loaded as
    Qt Code:
    1. gridLayout = new QWidget(centralwidget);
    2. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    3. gridLayout->setGeometry(QRect(10, 90, 351, 271));
    4. gridLayout1 = new QGridLayout(gridLayout);
    5. gridLayout1->setObjectName(QString::fromUtf8("gridLayout1"));
    6. gridLayout1->setContentsMargins(0, 0, 0, 0);
    7. widgetCurvePlot1 = new QCurvePlot(gridLayout);
    8. widgetCurvePlot1->setObjectName(QString::fromUtf8("widgetCurvePlot1"));
    To copy to clipboard, switch view to plain text mode 

    However if I start this Project then they a stacked above each other but the gridLayout is about 20-50 Pixel to large so that parts of that widget are not shown.
    How can I correct this?

    Matthias

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Use a layout and make sure the size policy of the child widget is not set to Fixed.

  5. #5
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    Use a layout and make sure the size policy of the child widget is not set to Fixed.
    I could change the size policy to expanding. That does not change anything.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    First of all make sure the layout is setup properly. The form should have a layout set and inside that layout there should be your widget. The code you posted in the first post is a mess, so I didn't analyze it, but at a first glance it seems incorrect.

  7. #7
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    First of all make sure the layout is setup properly. The form should have a layout set and inside that layout there should be your widget. The code you posted in the first post is a mess, so I didn't analyze it, but at a first glance it seems incorrect.
    Blame Trolltech not me. That code is auto generated.
    It is simply a qwidget with a Gridlayout within the qwtwidget is placed.
    Last edited by pospiech; 15th April 2008 at 11:01.

  8. #8
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Auto resize Widget to maximum avail. space

    Perhaps you'd better post the .ui file, which is easier for humans to analyze. :-)
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by pospiech View Post
    Blame Trolltech not me. That code is auto generated.
    It doesn't mean the form was correct. Could you attach the ui file?

  10. #10
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    I have now created an independent example: (Sorry it does not use an .ui file)

    main.cpp
    Qt Code:
    1. #include <qt/qapplication.h>
    2. #include "MainWindow.h"
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. QApplication app( argc, argv );
    7. MainWindow mainWindow;
    8. mainWindow.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H_
    2. #define MAINWINDOW_H_
    3.  
    4. #include <QtGui/QApplication>
    5. #include <QtGui/QGridLayout>
    6. #include <QtGui/QMainWindow>
    7. #include <QtGui/QTextEdit>
    8. #include <QtGui/QWidget>
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13. public:
    14. QWidget *m_centralwidget;
    15. QWidget *gridLayout;
    16. QGridLayout *gridLayout1;
    17. QTextEdit *textEdit;
    18.  
    19. void setupUi(QMainWindow *MainWindow)
    20. {
    21. MainWindow->resize(500, 500);
    22. m_centralwidget = new QWidget(MainWindow);
    23.  
    24. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    25. sizePolicy.setHorizontalStretch(0);
    26. sizePolicy.setVerticalStretch(0);
    27. m_centralwidget->setSizePolicy(sizePolicy);
    28.  
    29. gridLayout = new QWidget(m_centralwidget);
    30. gridLayout->setGeometry(QRect(50, 50, 300, 200));
    31. gridLayout->setSizePolicy(sizePolicy);
    32.  
    33. textEdit = new QTextEdit(gridLayout);
    34.  
    35. gridLayout1 = new QGridLayout(gridLayout);
    36. gridLayout1->setContentsMargins(0, 0, 0, 0);
    37. gridLayout1->addWidget(textEdit, 0, 0, 1, 1);
    38.  
    39. MainWindow->setCentralWidget(m_centralwidget);
    40.  
    41. QMetaObject::connectSlotsByName(MainWindow);
    42. } // setupUi
    43.  
    44. public:
    45. MainWindow(QWidget* parent = 0, Qt::WFlags flags = 0);
    46.  
    47. virtual ~MainWindow(){};
    48. //void resizeEvent(QResizeEvent * /* event */);
    49.  
    50. };
    51. #endif
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. #include "MainWindow.h"
    2. //
    3. MainWindow::MainWindow(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QMainWindow(parent, flags)
    4. {
    5. setupUi(this);
    6. }
    7.  
    8. //void MainWindow::resizeEvent(QResizeEvent * /* event */)
    9. //{
    10. // gridLayout->setGeometry(QRect(0, 0, this->width(), this->height()));
    11. //}
    To copy to clipboard, switch view to plain text mode 

    In this example nothing is resized if I resize the dialog.

    Matthias

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Why don't you post the UI file as requested? From what I see you are not applying a layout to the central widget, but I'd like to verify that using your UI file.

  12. #12
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    Why don't you post the UI file as requested? From what I see you are not applying a layout to the central widget, but I'd like to verify that using your UI file.
    Here the UI file from which I took the resulting code for my example


    In my example I removed the statusbar and the menubar.
    Attached Files Attached Files
    Last edited by wysota; 15th April 2008 at 14:26. Reason: Changed inlined code into attachment

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Yes, I was right - you didn't apply a layout to the central widget itself. Instead you had a floating layout, hence the mess in code you provided. Didn't Designer warn you about a floating layout?

    Here is a corrected file.
    Attached Files Attached Files
    Last edited by wysota; 15th April 2008 at 15:17. Reason: Updated attachment

  14. #14
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    Yes, I was right - you didn't apply a layout to the central widget itself. Instead you had a floating layout, hence the mess in code you provided. Didn't Designer warn you about a floating layout?
    What is a floating layout, and what exactly did I wrong? And no the designer never warned me. And I created the dialog as I created every dialog.

    Quote Originally Posted by wysota View Post
    Here is a corrected file.
    If I download and compare with my own one they have not changed. So I cannot see your corrections.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by pospiech View Post
    What is a floating layout, and what exactly did I wrong?
    A floating layout is a layout without "parent" widget. Take your ui, open it, grab the layout with your mouse and try to move it. If it moves, it's floating.

    And I created the dialog as I created every dialog.
    If so, then you created every dialog incorrectly.

    If I download and compare with my own one they have not changed. So I cannot see your corrections.
    Hmm... sorry, must have forgotten to save the file. Try it again.

  16. #16
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto resize Widget to maximum avail. space

    Quote Originally Posted by wysota View Post
    A floating layout is a layout without "parent" widget. Take your ui, open it, grab the layout with your mouse and try to move it. If it moves, it's floating.
    Well, yes I can move every layout I have ever seen.

    So the question is how to have non floating layouts. What do I have to do in QtDesigner to do it correct?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Auto resize Widget to maximum avail. space

    Click on the form itself and then on one of the layout buttons, like I did in my ui file.

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.