簡體   English   中英

從QMainWindow geometry()獲取錯誤的位置

[英]Getting the wrong position from QMainWindow geometry()

我正在使用一個在Qt GUI中繼承QMainWindow的類,以及另一個處理游戲邏輯的類。

該代碼的目的是將UI元素放置在窗口中的特定位置(以及根據需要移動它們。)但是,我遇到了問題。 如果增加窗口的大小,則Y軸會變得比窗口大,並將對象放在折疊之下。

游戲

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

加載時,正方形按預期放置。 預期 在將窗口調整為大於原始窗口的大小之后,正方形降到折疊以下。 不可接受的

由freqlabs解決了不和諧(誰沒有堆棧溢出帳戶。)

我無法更新QGraphicsScene的rect;

scene->setSceneRect(getBoard());

這使場景的坐標空間不正確,並導致對象平移錯誤。 我誤解了Qt使用坐標的確切程度,但我沒有意識到Qt使用帶有矩陣平移的實際坐標空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM