PDA

View Full Version : Button in Layout help



IsleWitch
9th October 2007, 13:43
Hi,

I'am having a problem in my code. I used addSpacing() and SeFixedSized(), to fix the buttons in my code. But the problem is once i enlarge the window, the buttons stay the same size and they dont get bigger with the same ratio as the window. I would appreciate any assistance on how i can fix this problem.

This is my code:



#include <QLabel>
#include <QPushButton>
#include <QProgressBar>
#include <QLineEdit>
#include <QIcon>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QToolButton>
#include <QApplication>
#include "auto.h"

Auto::Auto(QWidget *parent) : QWidget(parent)
{
// Title of the window
this->setWindowTitle("Main Menu");
this->resize(500, 350); // I'm using your old values

b_start = new QPushButton(tr("START/STOP"));
b_exit = new QPushButton(tr("Exit"));


b_start->setFixedSize(70,30);
b_exit->setFixedSize(50,30);

l_welcome = new QLabel(tr("--Automatic Interface--"));
l_welcome->setFont(QFont("Times", 18, QFont::Bold));


l_welcome->setAlignment(Qt::AlignCenter);

l_load = new QLabel(tr("Load Images"));
l_load->setFont(QFont("Times", 12, QFont::Bold));

l_load->setAlignment(Qt::AlignLeft);

t_text = new QLineEdit(this);
t_text->setFixedSize(150,20);
t_browse = new QToolButton(this);
p_prog = new QProgressBar(this);
p_prog->setFixedSize(100,20);
QHBoxLayout *v_1 = new QHBoxLayout;
v_1->setAlignment(Qt::AlignLeft);

v_1->addWidget(l_load);
v_1->addSpacing(10);
v_1->addWidget(t_text);
v_1->addWidget(t_browse);
v_1->addSpacing(20);
v_1->addWidget(p_prog);

QVBoxLayout *v_2 = new QVBoxLayout;
v_2->setAlignment(Qt::AlignCenter);

v_2->addWidget(b_start);
v_2->addWidget(b_exit);

QVBoxLayout *layout_vert = new QVBoxLayout;
layout_vert->setAlignment(Qt::AlignCenter);

layout_vert->addWidget(l_welcome);
layout_vert->addSpacing(30);
layout_vert->addLayout(v_1);
layout_vert->addSpacing(30);
layout_vert->addLayout(v_2);


this->setLayout(layout_vert);

}

Thanks!!

wysota
9th October 2007, 14:09
First of all get rid of all size fixing. From what I understand your widget looks like the form in the attachment (apart from the fonts of course), is that correct?

Now you have to decide which widgets should change their size when you resize the dialog and where there should be additional space when the widget is resized.Take the widget from attachment and add spacers everywhere you want additional space to be located when you resize the widget and change size policies of widgets you wish to expand to minimumExpanding. Then preview the form and see if that's what you want to get. If not, correct and try again.

IsleWitch
9th October 2007, 16:06
The Main problem is that i wasnt able to change the buttons in the size i want them, so i used the setfixedsize() function which gave me another problem on resizing the window. so is there a way to set the size of the buttons without using the setfixedsize() function??

wysota
9th October 2007, 16:21
In what way do you want to change the button size? At worst you'll have to subclass and reimplement sizeHint() but I'm almost sure you can achieve the effect you want by tweaking the properties appropriately (namely by adjusting the size policy).

IsleWitch
9th October 2007, 16:59
well, it turned out that its better keeping them the same size:D, i'll just have to worry about having them in the right places, i guess a couple of spaces would do it.

thanks alot for ur help:)