Results 1 to 13 of 13

Thread: Qt4 widget and nested layout issue.

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Question Qt4 widget and nested layout issue.

    Hey all,

    I've got a problem with a nested layout.
    First off I'm creating the main widget items.
    Then I'm passsing it to my first view to create its Qt components :

    Qt Code:
    1. ZeLoginView::ZeLoginView(QWidget *parent, ZeClientController& clientController) :
    2. QWidget(parent)
    To copy to clipboard, switch view to plain text mode 

    At the end of the process I'm deleting this view and free the layout of my main widget :
    Qt Code:
    1. void ZeGlobalController::SetView(ViewMode viewMode)
    2. {
    3. delete this->layout(); [...]
    4. }
    To copy to clipboard, switch view to plain text mode 

    Finally I'm passing my main widget to another view with a "Nested layout" :
    Qt Code:
    1. ZeUserView::ZeUserView(QWidget *parent, ZeUserController& userController) :
    2. QWidget(parent)
    3. {
    4. ZeLog::Get()->AddLine("--- Creating the user view ---\n");
    5.  
    6. mUserLayout = new QVBoxLayout(parent);
    7.  
    8. // User Infos
    9.  
    10. mUserInfoLayout = new QGridLayout;
    11.  
    12. // User picture
    13. mUserPictureLabel = new QLabel;
    14. mUserPictureLabel->setPixmap(QPixmap("zeData/profil_no_pic.jpg"));
    15. mUserInfoLayout->addWidget(mUserPictureLabel, 1, 1, Qt::AlignCenter);
    16.  
    17. // Login label
    18. mLoginLabel = new QLabel;
    19. mLoginLabel->setText(tr("Login"));
    20. mUserInfoLayout->addWidget(mLoginLabel, 2, 1, Qt::AlignCenter);
    21.  
    22. // Message combo box
    23. mMessageComboBox = new QLineEdit;
    24. mMessageComboBox->setFixedWidth(100);
    25. mUserInfoLayout->addWidget(mMessageComboBox, 3, 1, Qt::AlignCenter);
    26.  
    27. // Status combo box
    28. mStatusComboBox = new QComboBox;
    29. mStatusComboBox->setEditable(true);
    30. mStatusComboBox->setFixedWidth(100);
    31. mUserInfoLayout->addWidget(mStatusComboBox, 4, 1, Qt::AlignCenter);
    32.  
    33. // Layout stretch
    34. mUserInfoLayout->setColumnStretch(0, 10);
    35. mUserInfoLayout->setColumnStretch(2, 10);
    36.  
    37. mUserLayout->addLayout(mUserInfoLayout);
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    For some reason the nested layout : mUserInfoLayout, never appears : blank window.

    And even more strange, it's working in two cases:
    1. If I don't use nested layout to show my components.
    2. Working with nested stuff If I'm creating the second view at first.

    Looks like something is messed up between the first view and the second creation.

    There must be something wrong with the main widget, is there anything I could have forgot beside the delete layout() ?

    In advance thanks.

    Ben.
    Last edited by bunjee; 18th January 2007 at 12:08. Reason: spelling error

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt4 widget and nested layout issue.

    What is the base class of ZeUserController?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 widget and nested layout issue.

    Quote Originally Posted by bunjee View Post
    There must be something wrong with the main widget, is there anything I could have forgot beside the delete layout() ?
    Why do you delete that layout and where do you call QWidget::setLayout()?

  4. #4
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Post Re: Qt4 widget and nested layout issue.

    Here is the base class of the ZeUserController.

    Qt Code:
    1. class ZeUserController : public QWidget
    2. {
    3. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 

    I have one Widget for the main window of the application and many views.

    As I read in Qt doc, a Widget cannot have several layouts,
    I'm deleting the previous one in order to assign my next view.

    Qt Code:
    1. void ZeGlobalController::SetView(ViewMode viewMode)
    2. {
    3. ClearControllers();
    4.  
    5. delete this->layout();
    6.  
    7. switch (viewMode)
    8. {
    9. case LOGIN_VIEW:
    10. mClientController = new ZeClientController(*(ZeClient::Get()));
    11. mClientController->GenerateView();
    12. break;
    13.  
    14. case CONTACT_VIEW:
    15. mUserController = new ZeUserController();
    16. mUserController->GenerateView();
    17.  
    18. default:
    19. break;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 widget and nested layout issue.

    Quote Originally Posted by bunjee View Post
    I'm deleting the previous one in order to assign my next view.
    OK and where do you call setLayout()?

  6. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Post Re: Qt4 widget and nested layout issue.

    Qt Code:
    1. ZeLoginView::ZeLoginView(QWidget *parent, ZeClientController& clientController) :
    2. QWidget(parent)
    3. {
    4. // Login layout
    5. mLoginLayout = new QGridLayout(parent);
    To copy to clipboard, switch view to plain text mode 

    In the constructor of mLoginLayout in ZeLoginView.

    Qt Code:
    1. ZeUserView::ZeUserView(QWidget *parent, ZeUserController& userController) :
    2. QWidget(parent)
    3. {
    4. mUserLayout = new QVBoxLayout(parent);
    To copy to clipboard, switch view to plain text mode 

    In the constructor of mUserLayout in ZeUserView.
    Last edited by bunjee; 18th January 2007 at 13:13. Reason: spelling error

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt4 widget and nested layout issue.

    That's an awful way to do that (tightly couples the two widgets). Why not reuse the layout instead of creating a new one?

  8. #8
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Post Re: Qt4 widget and nested layout issue.

    In one case I need a QGrid in another a QHBox as main layout.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt4 widget and nested layout issue.

    But in both cases they can be children of the parent widget's layout. Your "view" should be a complete widget (having its own layout), not a complete layout. Then you can simply add the widget to the parent's layout.

  10. The following user says thank you to wysota for this useful post:

    bunjee (18th January 2007)

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 widget and nested layout issue.

    Quote Originally Posted by bunjee View Post
    mLoginLayout = new QGridLayout(parent);
    ...
    mUserLayout = new QVBoxLayout(parent);
    I see, but in this case you end with a widget that has no layout and no child widgets.

  12. The following user says thank you to jacek for this useful post:

    bunjee (18th January 2007)

  13. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Smile Re: Qt4 widget and nested layout issue.

    Thanks a lot guys,

    I've changed my approach :
    " One widget has one layout and sticks with it ".

    Even though I don't understand why this option:
    Qt Code:
    1. mLoginLayout = new QGridLayout(parent);
    2. ...
    3. mUserLayout = new QVBoxLayout(parent);
    To copy to clipboard, switch view to plain text mode 
    ... doesn't work.

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 widget and nested layout issue.

    Quote Originally Posted by bunjee View Post
    Even though I don't understand why this option: [...]
    ... doesn't work.
    Maybe you don't see any other widgets because they are hidden under those empty ZexxxView widgets?

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt4 widget and nested layout issue.

    Or you just didn't call show() on them...

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.