From a8a17fc9866cfe5569289016f66c0e2c0ca3b4ba Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete <paul.tvete@qt.io> Date: Tue, 22 May 2018 16:07:53 +0200 Subject: [PATCH] Use layouts instead of hardcoded sizes --- t14/cannon.h | 1 + t14/gamebrd.cpp | 51 +++++++++++++++++++++++++++++-------------------- t14/gamebrd.h | 2 -- t14/main.cpp | 1 - 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/t14/cannon.h b/t14/cannon.h index cf9724f..1e89709 100644 --- a/t14/cannon.h +++ b/t14/cannon.h @@ -20,6 +20,7 @@ public: int force() const { return f; } bool gameOver() const { return gameEnded; } bool isShooting() const { return shooting; } + QSize sizeHint() const { return QSize(400, 300); } public slots: void setAngle( int degrees ); void setForce( int newton ); diff --git a/t14/gamebrd.cpp b/t14/gamebrd.cpp index d7ad0c8..9874ef3 100644 --- a/t14/gamebrd.cpp +++ b/t14/gamebrd.cpp @@ -13,14 +13,15 @@ #include <qpushbutton.h> #include <qlcdnumber.h> +#include <QBoxLayout> +#include <QGridLayout> + #include "lcdrange.h" #include "cannon.h" GameBoard::GameBoard(QWidget *parent) : QWidget( parent ) { - setMinimumSize( 500, 355 ); - quit = new QPushButton( "Quit", this ); quit->setFont( QFont( "Times", 18, QFont::Bold ) ); @@ -35,7 +36,7 @@ GameBoard::GameBoard(QWidget *parent) frame = new QFrame( this ); frame->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); - cannonField = new CannonField( this ); + cannonField = new CannonField( frame ); cannonField->setPalette( QPalette(QColor(250, 250, 200)) ); cannonField->setAutoFillBackground(true); @@ -72,28 +73,36 @@ GameBoard::GameBoard(QWidget *parent) QShortcut *quitShortcut = new QShortcut(Qt::Key_Q, this); connect(quitShortcut, SIGNAL(activated()), qApp, SLOT(quit())); - quit->setGeometry( 10, 10, 75, 30 ); - angle->setGeometry( 10, quit->y() + quit->height() + 10, 75, 130 ); - force->setGeometry( 10, angle->y() + angle->height() + 10, 75, 130 ); - frame->move( angle->x() + angle->width() + 10, angle->y() ); - cannonField->move( frame->x() + 2, frame->y() + 2 ); - shoot->setGeometry( 10, 315, 75, 30 ); - restart->setGeometry( 380, 10, 110, 30 ); - hits->setGeometry( 130, 10, 40, 30 ); - hitsL->setGeometry( hits->x() + hits->width() + 5, 10, 60, 30 ); - shotsLeft->setGeometry( 240, 10, 40, 30 ); - shotsLeftL->setGeometry( shotsLeft->x()+shotsLeft->width()+5, 10, 60, 30 ); + QGridLayout *topLayout = new QGridLayout(this); + topLayout->addWidget(quit, 0, 0); + + QVBoxLayout *sideColumn = new QVBoxLayout; + topLayout->addLayout(sideColumn, 1, 0); + sideColumn->addWidget(angle); + sideColumn->addWidget(force); + sideColumn->addWidget(shoot); + + QHBoxLayout *topRow = new QHBoxLayout; + topLayout->addLayout(topRow, 0, 1); + topRow->addStretch(); + topRow->addWidget(hits); + topRow->addWidget(hitsL); + topRow->addStretch(); + topRow->addWidget(shotsLeft); + topRow->addWidget(shotsLeftL); + topRow->addStretch(); + topRow->addWidget(restart); + + topLayout->addWidget(frame, 1, 1); + + QVBoxLayout *frameLayout = new QVBoxLayout(frame); + frameLayout->addWidget(cannonField); + int margin = frame->frameWidth(); + frameLayout->setContentsMargins(margin, margin, margin, margin); newGame(); } -void GameBoard::resizeEvent( QResizeEvent * ) -{ - frame->resize( width() - frame->x() - 10, - height() - frame->y() - 10 ); - cannonField->resize( frame->width() - 4, frame->height() - 4 ); -} - void GameBoard::fire() { if ( cannonField->gameOver() || cannonField->isShooting() ) diff --git a/t14/gamebrd.h b/t14/gamebrd.h index 6f23eac..1347195 100644 --- a/t14/gamebrd.h +++ b/t14/gamebrd.h @@ -24,8 +24,6 @@ class GameBoard : public QWidget Q_OBJECT public: GameBoard( QWidget *parent=0 ); -protected: - void resizeEvent( QResizeEvent * ); protected slots: void fire(); void hit(); diff --git a/t14/main.cpp b/t14/main.cpp index 83cad08..314b7ad 100644 --- a/t14/main.cpp +++ b/t14/main.cpp @@ -14,7 +14,6 @@ int main( int argc, char **argv ) QApplication a( argc, argv ); GameBoard gb; - gb.setGeometry( 100, 100, 500, 355 ); gb.show(); return a.exec(); } -- GitLab