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:
Qt Code:
  1. #ifndef CENTRALWIDGET_H
  2. #define CENTRALWIDGET_H
  3.  
  4. #include <QWidget>
  5. #include <QGroupBox>
  6. #include <QComboBox>
  7. #include <QLineEdit>
  8. #include <QHBoxLayout>
  9.  
  10.  
  11. class CentralWidget : public QWidget
  12. {
  13. public:
  14. CentralWidget();
  15.  
  16. private:
  17. void init();
  18. void setClientGroupBox();
  19.  
  20. QGroupBox *clientGroupBox;
  21. QComboBox *clientComboBox;
  22. QLineEdit *clientLineEdit;
  23. QHBoxLayout *clientLayout;
  24. };
  25.  
  26. #endif // CENTRALWIDGET_H
To copy to clipboard, switch view to plain text mode 

CentralWidget code:
Qt Code:
  1. #include "centralwidget.h"
  2.  
  3. //**************
  4. //public methods
  5.  
  6. CentralWidget::CentralWidget()
  7. {
  8. init();
  9. }
  10.  
  11. //**************
  12. //protected methods
  13.  
  14. //**************
  15. //private methods
  16.  
  17. void CentralWidget::init()
  18. {
  19. setClientGroupBox();
  20. }
  21.  
  22. void CentralWidget::setClientGroupBox()
  23. {
  24. clientGroupBox = new QGroupBox("Klijent", this);
  25.  
  26. clientComboBox = new QComboBox(clientGroupBox); // this line maybe needs to be clientComboBox = new QComboBox(this); not really sure
  27. clientComboBox->addItem("Telekom");
  28. clientComboBox->addItem("Ostalo");
  29.  
  30. clientLineEdit = new QLineEdit(clientGroupBox); //this line maybe needs to be clientLineEdit = new QLineEdit(this); not really sure
  31.  
  32. clientLayout = new QHBoxLayout(clientGroupBox);
  33. clientLayout->addWidget(clientComboBox);
  34. clientLayout->addWidget(clientLineEdit);
  35. }
To copy to clipboard, switch view to plain text mode 

Now when I write this code in the method that initializes the main window:
Qt Code:
  1. centralWidget = new CentralWidget();
  2. setCentralWidget(centralWidget);
  3. //centralLayout = new QVBoxLayout(centralWidget);
To copy to clipboard, switch view to plain text mode 

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.