Qt GUI에서 QMainWindow를 상속하는 클래스와 게임 로직을 처리하는 다른 클래스를 사용하고 있습니다.QMainWindow geometry()에서 잘못된 위치 얻기
코드의 목적은 UI 요소를 창에서 특정 위치에 배치하는 것입니다 (필요에 따라 주위로 이동시킬 수도 있습니다). 그러나 문제가 있습니다. 창의 크기를 늘리면 Y 축이 창보다 커지고 객체가 폴드 아래에 배치됩니다. 예상대로
game.h
#ifndef GAME_H
#define GAME_H
#include "util.h"
#include "myrect.h"
class Game: public QObject
{
Q_OBJECT
TheColony * TC;
public:
Game(TheColony * ThC);
QRect getBoard(){ return QRect(0,0,TC->geometry().width(),TC->geometry().height()); }
private slots:
virtual void periodic();
protected:
QGraphicsScene * scene;
QGraphicsView * view;
MyRect * player;
QTimer * periodic_timer;
};
#endif // GAME_H
game.cpp 부하에
#include "game.h"
Game::Game(TheColony * ThC)
: TC(ThC){
//prepare the scene and view
scene = new QGraphicsScene(getBoard(),TC);
view = new QGraphicsView(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setGeometry(getBoard());
view->show();
TC->setCentralWidget(view);
//setup the player's position and size
player = new MyRect(QRect((view->width()/2) - 50,view->height() - 100,100,100));
player->setFlag(QGraphicsItem::ItemIsFocusable);
scene->addItem(player);
player->setFocus();
//timer used to trigger periodic checks.
periodic_timer = new QTimer();
connect(periodic_timer,SIGNAL(timeout()),this,SLOT(periodic()));
periodic_timer->start(500);
}
void Game::periodic(){
static int tcHeight = getBoard().height();
if(tcHeight != getBoard().height()){
view->setGeometry(getBoard());
player->setRect(player->rect().x(), getBoard().height() - 100,100,100);
tcHeight = getBoard().height();
}
}
는 정방형 배치된다. 원본 크기보다 큰 창 크기를 조정하면 사각형이 스크롤해야 볼 수있는 부분 아래로 떨어집니다.