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?
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:
class Ui_QCurvePlot { public: QWidget *gridLayout; QGridLayout *gridLayout1; { if (QCurvePlot->objectName().isEmpty()) QCurvePlot->resize(487, 297); QCurvePlot->setAutoFillBackground(false); gridLayout1->setContentsMargins(0, 0, 0, 0); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(qwtPlot->sizePolicy().hasHeightForWidth()); qwtPlot->setSizePolicy(sizePolicy); 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:
the last line is the only one which does the right thing.Qt Code:
{ setupUi(this); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); this->setSizePolicy(sizePolicy); gridLayout1->setColumnStretch ( 0, 20); gridLayout1->setRowStretch (0, 20); gridLayout1->setAlignment(Qt::AlignLeft);To copy to clipboard, switch view to plain text mode
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 12:17.
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
The gridLayout is loaded asQt Code:To copy to clipboard, switch view to plain text mode
Qt Code:
gridLayout1->setContentsMargins(0, 0, 0, 0); widgetCurvePlot1 = new QCurvePlot(gridLayout);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
Use a layout and make sure the size policy of the child widget is not set to Fixed.
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.
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
I have now created an independent example: (Sorry it does not use an .ui file)
main.cpp
Qt Code:
#include <qt/qapplication.h> #include "MainWindow.h" int main(int argc, char** argv) { MainWindow mainWindow; mainWindow.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
MainWindow.h
Qt Code:
#ifndef MAINWINDOW_H_ #define MAINWINDOW_H_ #include <QtGui/QApplication> #include <QtGui/QGridLayout> #include <QtGui/QMainWindow> #include <QtGui/QTextEdit> #include <QtGui/QWidget> { Q_OBJECT public: QWidget *m_centralwidget; QWidget *gridLayout; QGridLayout *gridLayout1; QTextEdit *textEdit; { MainWindow->resize(500, 500); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); m_centralwidget->setSizePolicy(sizePolicy); gridLayout->setSizePolicy(sizePolicy); gridLayout1->setContentsMargins(0, 0, 0, 0); gridLayout1->addWidget(textEdit, 0, 0, 1, 1); MainWindow->setCentralWidget(m_centralwidget); } // setupUi public: virtual ~MainWindow(){}; //void resizeEvent(QResizeEvent * /* event */); }; #endifTo copy to clipboard, switch view to plain text mode
MainWindow.cpp
Qt Code:
#include "MainWindow.h" // MainWindow::MainWindow(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QMainWindow(parent, flags) { setupUi(this); } //void MainWindow::resizeEvent(QResizeEvent * /* event */) //{ // gridLayout->setGeometry(QRect(0, 0, this->width(), this->height())); //}To copy to clipboard, switch view to plain text mode
In this example nothing is resized if I resize the dialog.
Matthias
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.
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.
Last edited by wysota; 15th April 2008 at 14:17. Reason: Updated attachment
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.
If I download and compare with my own one they have not changed. So I cannot see your corrections.
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.
If so, then you created every dialog incorrectly.And I created the dialog as I created every dialog.
Hmm... sorry, must have forgotten to save the file. Try it again.If I download and compare with my own one they have not changed. So I cannot see your corrections.
Click on the form itself and then on one of the layout buttons, like I did in my ui file.
Bookmarks