PDA

View Full Version : widget size does not change inside layout



br
19th November 2015, 12:14
Hi,

I am planning to add two widgets on my main widget in horizantal layout.
The width of left most widget (buttonsWindow) should be very small compared to width of right most widget (imageWindow).The purpose of left most widget is to show some buttons with some text , and purpose right most widget is to show some image based on button from left most widget.

I am using resize function to resize the widgets , but its not getting effected.

Somebody please show some pointers on how to change widgets size inside layouts.






#include "widget.h"
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QPushButton>
#include <QDesktopWidget>
#include<QRect>
#include <QApplication>
#include <QDebug>

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QRect rect;
rect = setScreenSize();

QWidget *buttonsWindow = new QWidget;
QWidget *imageWindow = new QWidget;
QHBoxLayout *hbox = new QHBoxLayout;


QVBoxLayout *vbox = new QVBoxLayout;
QPushButton *ltButton = new QPushButton(tr("&ltButton"));
QPushButton *NoC = new QPushButton(tr("&cdButton"));


// buttonsWindow->resize(rect.width()-800, rect.height()-100);
vbox->addWidget(ltButton);
vbox->addWidget(NoC);
buttonsWindow->setLayout(vbox);

QPalette Pal(palette());

// set black background
Pal.setColor(QPalette::Background, Qt::gray);
buttonsWindow->setAutoFillBackground(true);
buttonsWindow->setPalette(Pal);
// buttonsWindow->show();
hbox->addWidget(buttonsWindow);

//imageWindow->resize(rect.width()-200, rect.height()-200);
hbox->addWidget(imageWindow);

Pal.setColor(QPalette::Background, Qt::color1);
imageWindow->setAutoFillBackground(true);
imageWindow->setPalette(Pal);

setLayout(hbox);
}

Widget::~Widget()
{

}


QRect Widget:: setScreenSize()
{
QRect rec = QApplication::desktop()->screenGeometry();
resize(rec.width(), rec.height());

qDebug()<<"height="<<rec.height();
qDebug()<<"width="<<rec.width();

return rec;
}

anda_skoa
19th November 2015, 12:53
If imageWindow should take the available space that is not needed by buttonWindow, then add it with a stretch factor > 0
e.g.


hbox->addWidget(imageWindow, 1);


Cheers,
_

br
19th November 2015, 13:09
Wow, thanks, that worked well.

Now in my vertical layout , the space between buttons is more , one is at the top and other is at the bottom.
How can i arrange them with a fixed gap between them irrespective of buttons.
i tried


vbox->addWidget(ltButton);
vbox->setSpacing(0);
vbox->addWidget(NoC);


but does not work.

anda_skoa
19th November 2015, 14:25
So you want the two buttons to be vertically centered?
I.e. if the widget gets higher they stick together in the vertical middle?

If so you can add stretch before and after them



vbox->addStretch(1);
vbox->addWidget(ltButton);
vbox->addWidget(NoC);
vbox->addStretch(1);


Cheers,
_