- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在使用 mousePressEvent(QGraphicsSceneMouseEvent *event)
时遇到问题,实际上可点击区域看起来很小并且偏离了它链接到的 QGraphicsPixmapItem
的中心。
红线是 QGraphicsPixmapItem 可点击的地方。
我如何将它居中并最终使其变大并改变它的形状?
以下是我的代码中可能有用的部分:
在player.h中
class Player:public QObject, public QGraphicsPixmapItem{
Q_OBJECT
public:
Player();
void place_player(int x, int y);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
在 player.cpp 中
Player::Player(): QGraphicsPixmapItem(){
}
void Player::place_player(int x,int y)
{
this->setPixmap(QPixmap("test.png"));
this->setPos(x,y);
game->scene->addItem(this);
}
void Player::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug()<< event;
};
在游戏.cpp中
Game::Game(){
setFixedSize(1600,900);
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,1600,900);
setScene(scene);
}
void Game::start(){
player1 = new Player();
player1->place_player(300,300);
}
void Game::mousePressEvent(QMouseEvent *event)
{
QGraphicsView::mousePressEvent(event);
}
最后是 main.cpp
int main(int argc, char *argv[]){
QApplication a(argc, argv);
game = new Game();
game->show();
game->start();
return a.exec();
}
非常感谢您的帮助
最佳答案
QGraphicsItem 的可点击区域由其 boundingRect 定义和 shape功能。
我将从不使用 QGraphicsPixmapItem 开始。你想要一个自定义图形项,它具有信号和槽的功能,所以从 QGraphicsObject 派生.
class Player : public QGraphicsObject
{
};
因为我们现在已经从这个类派生出来了,所以我们需要覆盖几个纯虚函数;即boundingRect , paint
class Player : public QGraphicsObject
{
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
};
boundingRect 函数定义局部坐标中的对象。例如,假设字符的宽度和高度为 100。如果我们将 boundingRect 设置为 return (0, 0, 100, 100),这将围绕左上角定向。相反,我们希望将边界矩形集中在我们的播放器上:
QRectF Player::boundingRect() const
{
return QRectF(-50, -50, 100, 100); // local coordinates, centered on the Player
}
要绘制我们的 Player,请在类中存储一个 QPixmap
class Player : public QGraphicsObject
{
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
private:
QPixmap m_playerPixmap;
};
我假设您知道如何加载像素图并且可以在播放器的构造函数中执行此操作。
现在我们只需要渲染播放器,我们还将显示可点击区域,该区域由 boundingRect() 函数定义:-
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
// draw the player
painter->drawPixmap(0, 0, m_playerPixmap);
// set the pen to draw debug rect
painter->setPen(QColor(255, 0, 0, 127));
// for debug purposes, show the bounding rect (clickable area)
painter->drawRect(boundingRect());
}
最初我提到可点击区域是由 boundingRect 和 shape 函数定义的。由于 Player 是统一形状(矩形),我们只关心 boundingRect。在形状不规则的情况下,您还可以覆盖形状函数。
How would I centre it and eventually make it bigger and change it's shape ?
希望您现在知道要使 Player 变大,只需增加 boundingRect 函数中返回的局部坐标即可。所以,如果我们想将它的宽度和高度加倍,我们会这样做:
QRectF Player::boundingRect() const
{
return QRectF(-100, -100, 200, 200); // local coordinates, centered on the Player
}
要更改其形状,请实现 shape() 函数并进行调试,绘制从该函数返回的 painterPath,而不是绘制 boundingRect。
例如,我们有一个圆形的可点击区域。
假设您已将形状声明添加到播放器 header :
QPainterPath Player::shape() const
{
QPainterPath path;
path.addEllipse(-100, -100, 200, 200);
return path;
}
void Player::paint(QPainter * painter, const QStyleOptionGraphicsItem, QWidget*)
{
// draw the player
painter->drawPixmap(0, 0, m_playerPixmap);
// set the pen to draw debug path
painter->setPen(QColor(255, 0, 0, 127));
// for debug purposes, show the path (clickable area)
painter->drawPath(shape());
}
最后要注意的是,如果您要重写形状函数,您仍然必须实现 boundingRect。
关于c++ - Qt "mousePressEvent"修改可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372383/
所以,我试图每次左键单击时打印鼠标位置,但它只能在布局之外工作,我不知道如何修复它。我的 GUI 看起来像这样: import sys from PyQt5 import QtGui, QtWidge
我有两个不同的类继承QGraphicsItem。它们都有各自特定的 mousePressEvent 函数。当这些项目在场景中重叠时,我只想执行其中一个项目的 mousePressEvent。我怎样才能
我正在尝试检测具有多个小部件的区域内任意位置的鼠标点击。为此,我使用以下代码: custom_widget = CustomWidget() custom_widget.mouse_pressed_s
我在使用 QGraphicsView 和 QGraphicsScene 时遇到了一些困难。当我在场景中缩放/取消缩放并使用 mousePressEvent 创建项目时,我在该位置有一个偏移量。如何避免
我想在 dicom 系列(使用 qt 和 vtk)上实现一些功能,并且我想在 qt 窗口和鼠标之间建立一些连接。 这是我的主要设计: 例如,如果我点击 zoombutton,然后我用鼠标左键点击我的图
如何进入方法 on_product_clicked,其中 QLabel 是一个 mousePressEvent? 在当前示例中,我在表单中看到了三个图像。当我点击第二张图片时,我需要在 on_prod
我正在尝试同时运行两个 QWidget::mousePressEvent() 处理程序。我有两个小部件,A 和 B。B继承自QLabel,A继承自QWidget。我已经在它们两个上重载了 QWidge
所以我有一个带有 QGraphicsPolygonItem 的 QraphicsScene ,我将其标记为可移动。我还重写了 MousePressEvent。我的代码片段现在已经出来了。 def mo
我有一个小部件,我想在用户按下鼠标时运行一些功能。但我只想在鼠标按下导致焦点改变时运行它。我尝试了以下方法: void MyWidget::mousePressEvent(QMouseEvent *e
我在使用 mousePressEvent(QGraphicsSceneMouseEvent *event) 时遇到问题,实际上可点击区域看起来很小并且偏离了它链接到的 QGraphicsPixmapI
在 Class Buttons 中,我有一个 btnRightClicked 信号和一个 mousePressEvent 插槽: void Buttons::mousePressEvent(QMous
请考虑以下代码: #include #include #include class Widget : public QWidget { public: void mousePressEv
我有一个包含多个子对象的小部件,用户可以通过单击它们来选择它们。 我想在用户点击小部件外部时清除当前选择,我想知道如何最好地检测这些点击。 一些约束: 通过eventFilter吃掉周围对象的mous
我正在尝试在 pyqt4 中调整照片查看器(我从本网站的另一个问题中获得了代码)。默认情况下,鼠标滚轮缩放,鼠标左键用于平移/拖动(不知道到底该怎么称呼)图像。我想添加左键单击图像时获取像素信息的功能
在最近的项目中,我尝试在 GraphicsView 中单击相应位置后在场景上绘制点,但没有任何反应。我将错误追踪到缺少对 ClickableMap::mousePressEvent(const QMo
我有一个继承自 qquickwindow 的对象,该对象具有重写的 mousePressEvent 方法。 .h class FWindow : public QQuickWindow { Q
我正在尝试子类化 QCheckBox 以使其在禁用时通过鼠标点击。当我的复选框未被禁用时(isEnabled() 为 true),Qt 按预期调用其 mousePressEvent()。当它被禁用时,
我有一个类,它是 QDialog 的子类,没有覆盖 exec()、accept() 或 reject() 和另一个调用 mousePaintEvent 中的 Dialog 类: void Canvas
为了熟悉 Qt 的图形 View ,我在 Qt 中实现了一个简单的棋盘。目前没有棋子。 我使用矩形来表示每个图 block ,所以我创建了 BoardTile 类(继承自 QGraphicsRectI
我在使用 installEventFilter 过滤 "mousePressEvent" 时遇到问题 MyTestxEdit 是一个包含 QTextEdit 的小部件我希望 QTextEdit 的所有
我是一名优秀的程序员,十分优秀!