Results 1 to 12 of 12

Thread: Window doesn't expand to show all elements of QHBoxLayout

  1. #1
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Window doesn't expand to show all elements of QHBoxLayout

    I'm building a tic-tac-toe game.

    In my main window, I've got a QHBoxLayout holding the board (UIBoard), and the settings (which now consist of a start and quit button). uiSettings uses a QGroupBox and a QVBoxLayout in order to hold the start and quit buttons.

    Everything works, but when I start the program, I have to manually enlarge the window so that everything is shown -- not just the board.

    Here's the code:

    uiMain.hpp
    Qt Code:
    1. #ifndef UIMAIN_HPP_
    2. #define UIMAIN_HPP_
    3.  
    4. #include <QPushButton>
    5. #include <QHBoxLayout>
    6. #include <QVBoxLayout>
    7. #include <QApplication>
    8.  
    9. #include "uiBoard.hpp"
    10. #include "uiSettings.hpp"
    11.  
    12. namespace ttt {
    13. class UIMain : public QWidget {
    14. Q_OBJECT
    15. private:
    16. UIBoard *uiboard_;
    17. UISettings *uisettings_;
    18. QHBoxLayout *mainLayout_;
    19. public:
    20. UIMain();
    21. };
    22. }
    23.  
    24. #endif /*UIMAIN_HPP_*/
    To copy to clipboard, switch view to plain text mode 

    uiMain.cpp
    Qt Code:
    1. #include "uiMain.hpp"
    2.  
    3. namespace ttt {
    4. UIMain::UIMain() :
    5. uiboard_( new UIBoard( 'X', true, this ) ),
    6. uisettings_( new UISettings( this ) ),
    7. mainLayout_( new QHBoxLayout( this ) ) {
    8.  
    9. uiboard_->setFixedSize( 300, 300 );
    10.  
    11. mainLayout_->addWidget( uiboard_ );
    12. mainLayout_->addWidget( uisettings_ );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    And finally, main.cpp
    Qt Code:
    1. #include <iostream>
    2.  
    3. #include <QApplication>
    4.  
    5. #include "uiMain.hpp"
    6.  
    7. int main( int argc, char *argv[] ) {
    8. QApplication app( argc, argv );
    9.  
    10. ttt::UIMain game;
    11. game.show();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I've attached images showing what I see when I start the program, and what I see after I manually enlarge the window.

    Any help would be greatly appreciated and I'd be happy to provide more information if it would help.
    Attached Images Attached Images
    Last edited by Jessehk; 2nd August 2007 at 17:55. Reason: spelling error

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    Add this line at the end of your constructor:
    Qt Code:
    1. setFixedSize(sizeHint());
    To copy to clipboard, switch view to plain text mode 

    EDIT: or if you don't want a fixed size for the window, try:
    Qt Code:
    1. resize(sizeHint());
    To copy to clipboard, switch view to plain text mode 

    Regards
    Last edited by marcel; 2nd August 2007 at 18:17.

  3. #3
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    Quote Originally Posted by marcel View Post
    Add this line at the end of your constructor:
    Qt Code:
    1. setFixedSize(sizeHint());
    To copy to clipboard, switch view to plain text mode 

    EDIT: or if you don't want a fixed size for the window, try:
    Qt Code:
    1. resize(sizeHint());
    To copy to clipboard, switch view to plain text mode 

    Regards
    When I try either of those in the UIMain() contructor, the board is still the only thing shown.

    EDIT: I should mention that I'm able to resize so that some elements are only half-visible (like in the attached image). Maybe it's a clue to the problem?



    Any other ideas (but thanks anyway ) ?
    Attached Images Attached Images

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    It looks as if the widget does not have a layout set.

    Could you try adding this as the last line in the constructor?
    Qt Code:
    1. setLayout(mainLayout_);
    To copy to clipboard, switch view to plain text mode 
    If this alone does not work, see what happens if you subclass QDialog instead of QWidget.
    Just replace QWidget with QDialog in the class declaration and in the constructor initialization list.

    EDIT: it could be a problem with the fact that you're using "this" in the constructor initializer list. I am not sure if all compiler handle that well. Do you get any compile time warnings about that?

    Regards
    Last edited by marcel; 2nd August 2007 at 19:34.

  5. The following user says thank you to marcel for this useful post:

    Jessehk (2nd August 2007)

  6. #5
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    I don't know what to tell you, marcel.

    I've inherited UIMain from QDialog and

    Qt Code:
    1. setLayout( mainLayout_);
    To copy to clipboard, switch view to plain text mode 

    And it still defaults to just showing the board.

    Maybe I could set the minimum size?

    In any case, thanks for the continued help.

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    Weird.
    Maybe if you can provide a compilable example it would be easier to spot the problem.

    There's no need for minimum size. Setting the fixed size should have done it.

    Regards

  8. #7
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    Quote Originally Posted by marcel View Post
    Weird.
    Maybe if you can provide a compilable example it would be easier to spot the problem.

    There's no need for minimum size. Setting the fixed size should have done it.

    Regards
    Sorry marcel, but the game and the GUI are pretty entwined.
    I'm releasing this under the GPL anyways (I'm just a hobbyst ) so I'm happy to just post the sources.

    The files that are problems here are uiSettings.[hpp, cpp] uiMain.[hpp, cpp] and main.cpp.
    Attached Files Attached Files
    Last edited by Jessehk; 2nd August 2007 at 20:28.

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    OK.
    I have to download boost first.

    Shouldn't take more than 30-45 minutes with the whole testing.

    regards

  10. #9
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    Quote Originally Posted by marcel View Post
    OK.
    I have to download boost first.

    Shouldn't take more than 30-45 minutes with the whole testing.

    regards
    Oy .

    This is highly appreciated.

    EDIT: Take all the time you need. I'm in no hurry.

  11. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    OK.
    There was a problem in UISettings. You did not have a layout for the holder widget( the container of the group box ).

    Here's the fixed version:

    Qt Code:
    1. //This file is part of QtTicTac.
    2. //
    3. // QTicTac is free software; you can redistribute it and/or modify
    4. // it under the terms of the GNU General Public License as published by
    5. // the Free Software Foundation; either version 3 of the License, or
    6. // (at your option) any later version.
    7. //
    8. // QTicTac is distributed in the hope that it will be useful,
    9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11. // GNU General Public License for more details.
    12. //
    13. // You should have received a copy of the GNU General Public License
    14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
    15. //
    16. // Copyright 2007 Jesse E. H-K
    17.  
    18. #include <QApplication>
    19.  
    20. #include "uiSettings.hpp"
    21.  
    22. namespace ttt {
    23. UISettings::UISettings( QWidget *parent ) :
    24. QWidget ( parent ),
    25. settingsGroup_( new QGroupBox( this ) ),
    26. startb_( new QPushButton( tr( "&Start Game" ), settingsGroup_ ) ),
    27. quitb_( new QPushButton( tr( "&Quit" ), settingsGroup_ ) ) {
    28.  
    29. QVBoxLayout *topLayout = new QVBoxLayout(this);
    30. QVBoxLayout *vbox_ = new QVBoxLayout();
    31. vbox_->addWidget( startb_ );
    32. vbox_->addWidget( quitb_ );
    33.  
    34. settingsGroup_->setLayout( vbox_ );
    35. topLayout->addWidget( settingsGroup_ );
    36. topLayout->addItem(new QSpacerItem(1,1, QSizePolicy::Fixed, QSizePolicy::Expanding ));
    37. connect( quitb_, SIGNAL( clicked() ), qApp, SLOT( quit() ) );
    38.  
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    I also set a fixed size for the dialog, since the table does have a fixed size.(i did not attach that )

    Regards

    Regards

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

    Jessehk (2nd August 2007)

  13. #11
    Join Date
    Jul 2007
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    That's fantastic! Thanks so much.

  14. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Window doesn't expand to show all elements of QHBoxLayout

    You're welcome.

    Regards

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.