| Qt Programming General Qt programming issues. |
 |

14th April 2008, 12:12
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| class Ui_QCurvePlot
{
public:
QWidget *gridLayout;
QGridLayout *gridLayout1;
QwtPlot *qwtPlot;
void setupUi(QWidget *QCurvePlot)
{
if (QCurvePlot->objectName().isEmpty())
QCurvePlot->setObjectName(QString::fromUtf8("QCurvePlot"));
QCurvePlot->resize(487, 297);
QCurvePlot->setAutoFillBackground(false);
gridLayout = new QWidget(QCurvePlot);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setGeometry(QRect(40, 10, 241, 251));
gridLayout1 = new QGridLayout(gridLayout);
gridLayout1->setObjectName(QString::fromUtf8("gridLayout1"));
gridLayout1->setContentsMargins(0, 0, 0, 0);
qwtPlot = new QwtPlot(gridLayout);
qwtPlot->setObjectName(QString::fromUtf8("qwtPlot"));
QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(qwtPlot->sizePolicy().hasHeightForWidth());
qwtPlot->setSizePolicy(sizePolicy);
gridLayout1->addWidget(qwtPlot, 0, 0, 1, 1); |
and I tried the follwing code which did not lead to a solution at all:
Qt Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
| QCurvePlot::QCurvePlot(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QWidget(parent, flags)
{
setupUi(this);
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
this->setSizePolicy(sizePolicy);
gridLayout1->setColumnStretch ( 0, 20);
gridLayout1->setRowStretch (0, 20);
gridLayout1->setAlignment(Qt::AlignLeft); |
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 12:17.
|

14th April 2008, 19:17
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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?
|

15th April 2008, 09:12
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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
2
3
4
5
| void MainWindowImpl::resizeEvent(QResizeEvent * event)
{
horizontalLayout->setGeometry(QRect(0, 0, this->width(), horizontalLayout->minimumSizeHint().height()));;
gridLayout->setGeometry(QRect(0, horizontalLayout->height(), this->width(), this->height()-horizontalLayout->height()));
} |
The gridLayout is loaded as
Qt Code:
1
2
3
4
5
6
7
8
| gridLayout = new QWidget(centralwidget);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setGeometry(QRect(10, 90, 351, 271));
gridLayout1 = new QGridLayout(gridLayout);
gridLayout1->setObjectName(QString::fromUtf8("gridLayout1"));
gridLayout1->setContentsMargins(0, 0, 0, 0);
widgetCurvePlot1 = new QCurvePlot(gridLayout);
widgetCurvePlot1->setObjectName(QString::fromUtf8("widgetCurvePlot1")); |
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
|

15th April 2008, 09:37
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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.
|

15th April 2008, 09:44
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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.
|

15th April 2008, 09:55
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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.
|

15th April 2008, 09:57
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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.
|

15th April 2008, 10:30
|
|
Advanced user
|
|
Join Date: Mar 2006
Location: The Netherlands
Qt products used:
Qt3
, Qt4
Qt platforms used:
Unix/X11
Posts: 300
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|

15th April 2008, 10:33
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by pospiech
Blame Trolltech not me. That code is auto generated.
|
It doesn't mean the form was correct. Could you attach the ui file?
|

15th April 2008, 12:48
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
2
3
4
5
6
7
8
9
10
| #include <qt/qapplication.h>
#include "MainWindow.h"
int main(int argc, char** argv)
{
QApplication app( argc, argv );
MainWindow mainWindow;
mainWindow.show();
return app.exec();
} |
MainWindow.h
Qt Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| #ifndef MAINWINDOW_H_
#define MAINWINDOW_H_
#include <QtGui/QApplication>
#include <QtGui/QGridLayout>
#include <QtGui/QMainWindow>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QWidget *m_centralwidget;
QWidget *gridLayout;
QGridLayout *gridLayout1;
QTextEdit *textEdit;
void setupUi(QMainWindow *MainWindow)
{
MainWindow->resize(500, 500);
m_centralwidget = new QWidget(MainWindow);
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
m_centralwidget->setSizePolicy(sizePolicy);
gridLayout = new QWidget(m_centralwidget);
gridLayout->setGeometry(QRect(50, 50, 300, 200));
gridLayout->setSizePolicy(sizePolicy);
textEdit = new QTextEdit(gridLayout);
gridLayout1 = new QGridLayout(gridLayout);
gridLayout1->setContentsMargins(0, 0, 0, 0);
gridLayout1->addWidget(textEdit, 0, 0, 1, 1);
MainWindow->setCentralWidget(m_centralwidget);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
public:
MainWindow(QWidget* parent = 0, Qt::WFlags flags = 0);
virtual ~MainWindow(){};
//void resizeEvent(QResizeEvent * /* event */);
};
#endif |
MainWindow.cpp
Qt Code:
1
2
3
4
5
6
7
8
9
10
11
| #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()));
//} |
In this example nothing is resized if I resize the dialog.
Matthias
|

15th April 2008, 12:55
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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.
|

15th April 2008, 13:23
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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.
Last edited by wysota; 15th April 2008 at 13:26.
Reason: Changed inlined code into attachment
|

15th April 2008, 13:28
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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.
Last edited by wysota; 15th April 2008 at 14:17.
Reason: Updated attachment
|

15th April 2008, 14:06
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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
Here is a corrected file.
|
If I download and compare with my own one they have not changed. So I cannot see your corrections.
|

15th April 2008, 14:16
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by pospiech
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.
Quote:
|
And I created the dialog as I created every dialog.
|
If so, then you created every dialog incorrectly.
Quote:
|
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.
|

15th April 2008, 14:22
|
|
Intermediate user
|
|
Join Date: Feb 2008
Qt products used:
Qt4
Qt platforms used:
Unix/X11
, Windows
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Re: Auto resize Widget to maximum avail. space
Quote:
Originally Posted by wysota
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?
|

15th April 2008, 14:47
|
|
Guru
|
|
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used:
Qt3
, Qt4
, Qt/Embedded
Qt platforms used:
Unix/X11
, Windows
Posts: 14,314
Thanks: 3
Thanked 2,068 Times in 2,008 Posts
|
|
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.
|
 |
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +1. The time now is 03:52.
|