Results 1 to 3 of 3

Thread: Problem setting central Widget layout

  1. #1
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem setting central Widget layout

    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.

  2. #2
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem setting central Widget layout

    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.

  3. #3
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem setting central Widget layout

    Solved the parenting problem also after some more work. Sorry to bother you for nothing.

Similar Threads

  1. Setting background image for Central Widget
    By xfurrier in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2010, 10:14
  2. Hide widget, but not change layout
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 14:32
  3. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32
  4. Widget resizing problem
    By fwohlfert in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2006, 08:16
  5. Replies: 7
    Last Post: 3rd February 2006, 10:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.