2017-12-24 13 views
0

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(); 
    } 
} 

는 정방형 배치된다. expected 원본 크기보다 큰 창 크기를 조정하면 사각형이 스크롤해야 볼 수있는 부분 아래로 떨어집니다. unacceptable

답변

0

은 불화에 freqlabs에 의해 해결되었다

내가 QGraphicsScene의 RECT를 업데이트하는데 실패했다 (스택 오버 플로우 계정을 가지고 있지 않습니다.)

scene->setSceneRect(getBoard()); 

이렇게하면 장면의 좌표 공간이 잘못되어 개체가 잘못 번역 될 수 있습니다. Qt가 좌표를 얼마나 정확히 사용하고 있는지 오해했습니다. 실제로 좌표 공간을 매트릭스 변환으로 사용한다는 사실을 깨닫지 못했습니다.