PDA

View Full Version : Avoid resize of QLayout's?



vitaR
19th March 2014, 06:30
Hi, my question is simple, I dont like how the QLayouts resize an item that is added to them, how can I avoid it?

10140

anda_skoa
19th March 2014, 08:40
Maybe you should describe what you want to have?

Cheers,
-

Radek
19th March 2014, 09:07
Guessing from the picture above: (1) Set sizes of the button "fixed" and align it in the layout. (2) Put the button in a horizontal layout and add a spacer. Then put the horizontal layout in the vertical (?) layout with the text edit.

vitaR
19th March 2014, 23:18
Maybe you should...-
I just want a normal size, not a resized one that use the whole space.


Guessing from the picture....
Is that complicated? dam, but thanks.

ChrisW67
20th March 2014, 07:39
It would not be complicated if you told us what you wanted. Your thread title implies that you want to "Avoid resize of QLayout", i.e not resize anything. Your picture implies you want to resize the push button. Your last post says you want a "normal size, not a resized one" without telling us what "one" refers to.

vikaspachdha
20th March 2014, 09:09
Your problem definition is bit vague so here is my guess what you want.
10151

adutzu89
20th March 2014, 09:29
Here is a quick and short example of how I made a QDialog based on your picture.
dialog.h:

#ifndef DIALOG_H
#define DIALOG_H
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QSizePolicy>
#include <QDialog>

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
QVBoxLayout *layout;
QGridLayout *lineLayout;
QPushButton *buton;
};

#endif // DIALOG_H

dialog.cpp:

#include "dialog.h"

Dialog::Dialog(QWidget *parent):QDialog(parent){
layout=new QVBoxLayout;
lineLayout=new QGridLayout;
buton=new QPushButton("Ok");

lineLayout->addWidget(new QLabel("Name"),0,0);
lineLayout->addWidget(new QLineEdit,0,1);

layout->addLayout(lineLayout);
layout->addWidget(buton,0,Qt::AlignHCenter); //here I added it to the QVBoxLayout with stretch=0 and qt alignment...more info you can get on QVBoxLayout's page
setLayout(layout);
}

Dialog::~Dialog(){
}

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();

return a.exec();
}

Hope this helps.

vitaR
20th March 2014, 17:51
Here is a quick....
Thanks this helped me.
Sorry to everyone for the vague definition of my problem. The thing was that when I'll add a button to a layout, this button takes the whole free space of the widget.
With this alignment this re-size of the button that the layout makes, is avoided.

10152

There's is some other way to do this without default alignments of Qt? Just wanted to know, but my problem is solved with alignments.

adutzu89
20th March 2014, 18:00
Other ways I can think of is that you can put the button inside another layout and use the QSizePolicy class to make the layout fixed, or inside a QDialogButtonBox.

vitaR
20th March 2014, 19:03
Other ways I can think of is that you can put the button inside another layout and use the QSizePolicy class to make the layout fixed, or inside a QDialogButtonBox.

Thanks my friend, this works too, have a good day.

wysota
20th March 2014, 21:10
I just want a normal size, not a resized one that use the whole space.

Still not clear what you want however maybe you want to set the horizontal size policy of the button to Fixed.

anda_skoa
21st March 2014, 09:21
You can also add stretch (C++) or spacers (in designer) left and right of the button.

Cheers,
_