Results 1 to 17 of 17

Thread: Auto resize Widget to maximum avail. space

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  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
    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 10:01.

  6. #6
    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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?

  8. #8
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  10. #10
    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 13:26. Reason: Changed inlined code into attachment

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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 14:17. Reason: Updated attachment

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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

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
  •  
Qt is a trademark of The Qt Company.