PDA

View Full Version : Problem setting central Widget layout



frenk_castle
26th September 2009, 04:50
This is a beginning of a project of mine. I have problem setting up layout for my central widget. Probably a trivial problem but it annoys me.

I have created a CentralWidget class in my project.

CentralWidget header:


#ifndef CENTRALWIDGET_H
#define CENTRALWIDGET_H

#include <QWidget>
#include <QGroupBox>
#include <QComboBox>
#include <QLineEdit>
#include <QHBoxLayout>


class CentralWidget : public QWidget
{
public:
CentralWidget();

private:
void init();
void setClientGroupBox();

QGroupBox *clientGroupBox;
QComboBox *clientComboBox;
QLineEdit *clientLineEdit;
QHBoxLayout *clientLayout;
};

#endif // CENTRALWIDGET_H


CentralWidget code:


#include "centralwidget.h"

//**************
//public methods

CentralWidget::CentralWidget()
{
init();
}

//**************
//protected methods

//**************
//private methods

void CentralWidget::init()
{
setClientGroupBox();
}

void CentralWidget::setClientGroupBox()
{
clientGroupBox = new QGroupBox("Klijent", this);

clientComboBox = new QComboBox(clientGroupBox); // this line maybe needs to be clientComboBox = new QComboBox(this); not really sure
clientComboBox->addItem("Telekom");
clientComboBox->addItem("Ostalo");

clientLineEdit = new QLineEdit(clientGroupBox); //this line maybe needs to be clientLineEdit = new QLineEdit(this); not really sure

clientLayout = new QHBoxLayout(clientGroupBox);
clientLayout->addWidget(clientComboBox);
clientLayout->addWidget(clientLineEdit);
}


Now when I write this code in the method that initializes the main window:


centralWidget = new CentralWidget();
setCentralWidget(centralWidget);
//centralLayout = new QVBoxLayout(centralWidget);


Every thing is shown ok but the window doesn't rezise ok when I change its size with the mouse. Everything is stuck in the upper left corner and the windows starting size is to small. When I remove the comment the main window shows only the empty group box. Combo box and line edit are gone and the window still won't resize right when I change its size with the mouse and its starting size is the same as before.

When I make all this in designer. I put group box in my ui form add combo box and line edit then click on group box choose horizontal layout then click on the top of the window in the ui form and choose vertical layout save and compile it shows everything just fine. Ok group box takes the whole window and windows starting size is a little big but I can fix that with adding rest of the wigdets and adding spacers so that doesn't bother me. What is important is that window resize fine.

I want to learn how to do this in code not using the designer so if somebody can tell me what am I doing wrong I would appreciate it.

And while you're at it if somebody could help with my "parenting" problem concerning clientComboBox and clientLineEdit I would also appreciate it.

Thanks in advance.

frenk_castle
26th September 2009, 06:01
Well after one hour of thinking and trying I solved the central widget layout problem. Now if somebody could help me with "parenting" problem from my original post I would appreciated.

Thanks in advance.

frenk_castle
26th September 2009, 16:43
Solved the parenting problem also after some more work. Sorry to bother you for nothing. :)