我的QGraphicsItem项目没有被鼠标注意到
My QGraphicsItem items are not noticed by the mouse
我正在尝试移动我的项目...唯一的方法是,如果我遍历所有项目并检查鼠标位置:
class Item : public QGraphicsItem
{
Item() { setFlag(ItemIsMovable); setFlag(ItemIsSelectable); scale = 10; }
QRectF boundingRect() const;
void paint(QPainter *painter); // had to implement this because I don't know how to get the QStyleOptionGraphicsItem to call the paint below
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) {paint(painter);} // never called
void update() {}
int scale, x, y;
};
// scale is Item property set by caller, x and y also set by caller based on viewport position of item center
QRectF Item::boundingRect() const
{
return QRectF(x - 30 - scale, y - 30 - scale,
30 + 3 * scale, 20 + 3 * scale);
}
void Item::paint(QPainter *painter)
{
update();
painter->drawRect(boundingRect());
}
class CollectionView : public QGraphicsView
{
Q_OBJECT
public:
CollectionView(QWidget *parent = 0);
QList<Item*> *m_items;
protected:
virtual void paintEvent(QPaintEvent * event);
virtual void mousePressEvent(QMouseEvent * event);
virtual void mouseReleaseEvent(QMouseEvent *event);
};
CollectionView::CollectionView(QWidget *parent)
: QGraphicsView(parent)
{
QGraphicsScene *s = new QGraphicsScene(this);
setScene(s);
setViewportUpdateMode(BoundingRectViewportUpdate);
m_items = new QList<Item*>();
}
void CollectionView::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this->viewport());
for(int i = 0; i< m_items->size(); i++)
{
Item* item = m_items->at(i);
//scene()->addItem(item); // this crashes
item->paint(&painter);
}
}
void CollectionView::mousePressEvent(QMouseEvent* event)
{
// if I add this and comment the rest, items don't move
//QGraphicsView::mousePressEvent(event);
foreach (QGraphicsItem *item, this->items(event->pos()))
{ /* never gets inside */ }
foreach (QGraphicsItem *item, this->items(event->globalPos()))
{ /* never gets inside */ }
// the following works though:
for (int i = 0; i < m_items->size(); i++)
{
Item* currentItem = m_items->at(i);
if(!currentItem->boundingRect().contains(event->pos()))
continue;
if (event->button() == Qt::LeftButton)
{ /* I can get the item index and its relative position to mouse
then pass this info to the mouseReleaseEvent, and it works */ }
break;
}
}
如果我可以使用QGraphicsView/QGraphicsScene/QGraphicsItem方法来移动项目或获取上下文菜单,那将是最好的...我还没想通怎么做。
但是,如果我必须实现鼠标操作,最好是我可以遍历在鼠标位置找到的项目,而不是列表中的所有项目(可能更大)。
为什么我的尝试不起作用? (或者,我怎样才能让QGraphicsScene/QGraphicsView完成所有工作并移动项目,而无需编写任何代码?这就是ItemIsMovable的目的......对吧?)
更新:添加了paintEvent
的代码...不幸的是,我无法打电话给
空隙涂料(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
油漆的版本 - 它可能会被类似 scene()->addItem(item);
的东西使用 - 但该特定调用使程序崩溃......上面的油漆方法从未被调用过,但我知道这是他QGraphicsItem
的官方油漆方法......这么简单的代码却这么乱。
正如我们在注释中建立的那样,问题是从 boundingRect( ) 函数返回的值。
QGraphicsItem 的边界矩形定义项的本地坐标。如果该项是常规的,匹配矩形,则只需要实现 boundingRect()。这用于碰撞检测,其中包括检测鼠标在项目上。
如果你有一个非规则(矩形)对象,并且想要更精细的碰撞检测,那么除了 boundingRect() 之外,还要实现 shape() 函数。两者都位于项目的本地坐标中。
首先,尝试使用一个简单的项目和一个基本的图形场景和视图。诸如此类:
class GraphicsItem : public QGraphicsItem
{
public:
GraphicsItem(QWidget* parent) {
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
}
QRectF boundingRect() const
{
qreal penWidth = 1;
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
}
};
这是一个基本的项目,当你使用基本的QGraphicsItem和QGraphicsScene时,将会移动。
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
scene.addItem(new GraphicsItem(NULL));
QGraphicsView view(&scene);
view.show();
return a.exec();
}
如果代码中的问题似乎您的项目没有移动它,则可能是您的 MousePressEvent。
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 瓦尔格林德:注意到但未处理的国际奥克特尔;是否有必要处理以及如何找到?
- 我在QChartView中找不到缩放图形的鼠标滚轮滚动事件
- Qt将鼠标移动事件转移到新窗口
- 在鼠标单击Qt时将信号从子级发送到父级
- GetAsyncKeyState 未检测到鼠标输入
- 发送到另一个窗口的鼠标移动消息不执行任何操作
- Visual Studio 2017在我将鼠标移到Mat关键字(OpenCv 3.4.1)上时冻结
- 如何在后台模拟鼠标单击到 DirectX 应用程序?
- 使用 Cmake 编译 LZO 时,它注意到找不到任何 VS 实例
- 如何将双精度类型的鼠标移动 x 和 y 从主机传递到 Qemu 中的虚拟机
- GLFW 不会检测到 GLFW 窗口外的鼠标移动
- 增量构建:Eclipse CDT没有注意到源已更改
- 我的QGraphicsItem项目没有被鼠标注意到
- 在C++窗口代码中有没有任何方法可以阻止鼠标移动到窗口边界之外
- 在矩阵乘法中使用C++2011线程而不是OpenMP时注意到异常加速
- 对话框上的 MFC 控件在鼠标移到它们上之前不会显示
- 如果我碰巧注意到 weak_ptr 已过期,我应该在 上调用重置吗?
- TCP套接字能注意到网络中断的异常吗?
- 鼠标坐标到3D世界/投影点碰撞