PDA

View Full Version : Auto resize Widget to maximum avail. space



pospiech
14th April 2008, 12:12
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:


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:


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

wysota
14th April 2008, 19: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?

pospiech
15th April 2008, 09:12
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


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


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

wysota
15th April 2008, 09:37
Use a layout and make sure the size policy of the child widget is not set to Fixed.

pospiech
15th April 2008, 09:44
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.

wysota
15th April 2008, 09:55
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.

pospiech
15th April 2008, 09:57
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.

Michiel
15th April 2008, 10:30
Perhaps you'd better post the .ui file, which is easier for humans to analyze. :-)

wysota
15th April 2008, 10:33
Blame Trolltech not me. That code is auto generated.
It doesn't mean the form was correct. Could you attach the ui file?

pospiech
15th April 2008, 12:48
I have now created an independent example: (Sorry it does not use an .ui file)

main.cpp


#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


#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


#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

wysota
15th April 2008, 12:55
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.

pospiech
15th April 2008, 13:23
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.

wysota
15th April 2008, 13:28
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.

pospiech
15th April 2008, 14:06
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.



Here is a corrected file.
If I download and compare with my own one they have not changed. So I cannot see your corrections.

wysota
15th April 2008, 14:16
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.

pospiech
15th April 2008, 14:22
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?

wysota
15th April 2008, 14:47
Click on the form itself and then on one of the layout buttons, like I did in my ui file.