PDA

View Full Version : Window doesn't expand to show all elements of QHBoxLayout



Jessehk
2nd August 2007, 17:52
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


#ifndef UIMAIN_HPP_
#define UIMAIN_HPP_

#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QApplication>

#include "uiBoard.hpp"
#include "uiSettings.hpp"

namespace ttt {
class UIMain : public QWidget {
Q_OBJECT
private:
UIBoard *uiboard_;
UISettings *uisettings_;
QHBoxLayout *mainLayout_;
public:
UIMain();
};
}

#endif /*UIMAIN_HPP_*/


uiMain.cpp


#include "uiMain.hpp"

namespace ttt {
UIMain::UIMain() :
QWidget(),
uiboard_( new UIBoard( 'X', true, this ) ),
uisettings_( new UISettings( this ) ),
mainLayout_( new QHBoxLayout( this ) ) {

uiboard_->setFixedSize( 300, 300 );

mainLayout_->addWidget( uiboard_ );
mainLayout_->addWidget( uisettings_ );
}
}


And finally, main.cpp


#include <iostream>

#include <QApplication>

#include "uiMain.hpp"

int main( int argc, char *argv[] ) {
QApplication app( argc, argv );

ttt::UIMain game;
game.show();

return app.exec();
}


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. :)

marcel
2nd August 2007, 18:06
Add this line at the end of your constructor:


setFixedSize(sizeHint());


EDIT: or if you don't want a fixed size for the window, try:


resize(sizeHint());


Regards

Jessehk
2nd August 2007, 19:07
Add this line at the end of your constructor:


setFixedSize(sizeHint());


EDIT: or if you don't want a fixed size for the window, try:


resize(sizeHint());


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? :confused:



Any other ideas (but thanks anyway :)) ?

marcel
2nd August 2007, 19:24
It looks as if the widget does not have a layout set.

Could you try adding this as the last line in the constructor?


setLayout(mainLayout_);
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

Jessehk
2nd August 2007, 19:44
I don't know what to tell you, marcel. :(

I've inherited UIMain from QDialog and



setLayout( mainLayout_);


And it still defaults to just showing the board.

Maybe I could set the minimum size?

In any case, thanks for the continued help. :)

marcel
2nd August 2007, 19:50
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

Jessehk
2nd August 2007, 20:24
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.

marcel
2nd August 2007, 20:29
OK.
I have to download boost first.

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

regards

Jessehk
2nd August 2007, 20:30
OK.
I have to download boost first.

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

regards

Oy :eek:.

This is highly appreciated. :o

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

marcel
2nd August 2007, 20:54
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:



//This file is part of QtTicTac.
//
// QTicTac is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// QTicTac is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2007 Jesse E. H-K

#include <QApplication>

#include "uiSettings.hpp"

namespace ttt {
UISettings::UISettings( QWidget *parent ) :
QWidget ( parent ),
settingsGroup_( new QGroupBox( this ) ),
startb_( new QPushButton( tr( "&Start Game" ), settingsGroup_ ) ),
quitb_( new QPushButton( tr( "&Quit" ), settingsGroup_ ) ) {

QVBoxLayout *topLayout = new QVBoxLayout(this);
QVBoxLayout *vbox_ = new QVBoxLayout();
vbox_->addWidget( startb_ );
vbox_->addWidget( quitb_ );

settingsGroup_->setLayout( vbox_ );
topLayout->addWidget( settingsGroup_ );
topLayout->addItem(new QSpacerItem(1,1, QSizePolicy::Fixed, QSizePolicy::Expanding ));
connect( quitb_, SIGNAL( clicked() ), qApp, SLOT( quit() ) );

}
}



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

Regards

Regards

Jessehk
2nd August 2007, 21:25
That's fantastic! Thanks so much. :)

marcel
2nd August 2007, 21:31
You're welcome.

Regards