围绕QgraphicsScene移动QGraphicsView
move QGraphicsView around QGraphicsScene
我正在qt c 中制作侧视图拖动赛车游戏。我想将我的视野从左至右移动。我将场景设置为3600x800,但我希望视图在我场景的最左边,而不是一开始就位于中心。当我在键盘上按W时,我希望视图向左移动1px。我怎么做?我在网上找不到任何东西
scene=new QGraphicsScene(this);
view = new QGraphicsView;
scene->setSceneRect(0,0,3600,800);
view->setScene(scene);
您永远不会在互联网上找到如此特殊的东西,您应该分别寻找每个部分:
-
如果您希望它出现在左侧,则必须在显示
GraphicsView
的horizontalScrollBar()
时使用showEvent
方法。 -
如果在按任何键时要执行操作,则可以覆盖
keyPressEvent
方法。 -
要移动
sceneRect()
,您必须制作副本,将其移动并再次设置。
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QKeyEvent>
#include <QScrollBar>
class GraphicsView: public QGraphicsView{
public:
using QGraphicsView::QGraphicsView;
protected:
void keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_W){
if(scene()){
QRectF rect = scene()->sceneRect();
rect.translate(1, 0);
scene()->setSceneRect(rect);
}
}
}
void showEvent(QShowEvent *event){
QGraphicsView::showEvent(event);
if(isVisible()){
horizontalScrollBar()->setValue(0);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
GraphicsView view;
scene.setSceneRect(0,0,3600,800);
view.setScene(&scene);
scene.addRect(0, 200, 400, 400, Qt::NoPen, Qt::red);
view.show();
return a.exec();
}
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 按值 C++ 返回时进行双倍移动
- 如何在QGraphicsView中移动QWidget?
- 围绕QgraphicsScene移动QGraphicsView
- 在 QGraphicsView/QGraphicsScene 中的 QGraphicsProxyWidget 中移动嵌入
- 用鼠标在QGraphicsView/QGraphicsScene上移动项目