使用 QGrahicsRectItem 拖动大于项目本身的矩形
我在 QGraphicsScene
中的 QGrahicsRectItem
遇到问题。我想要的是能够用鼠标移动该项目。但问题是,就我而言,拖动矩形比项目本身大。
这是我使用的代码:
class Test: public QGraphicsView
{
public:
Test();
private:
virtual void resizeEvent (QResizeEvent * event);
QGraphicsScene* m_pScene;
};
Test::Test()
{
m_pScene = new QGraphicsScene();
setScene(m_pScene);
m_pScene->setSceneRect(0, 0, 100, 100);
for (int i = 0 ; i < 10 ; i++)
{
QGraphicsRectItem * pItem = new QGraphicsRectItem();
pItem->setFlag(QGraphicsItem::ItemIsMovable);
pItem->setBrush(QBrush(QColor(190, 100, 100)));
pItem->setRect(QRectF(10*i, 10, 5, 80.f));
pItem->setCursor(Qt::SizeAllCursor);
m_pScene->addItem(pItem);
}
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
resize(600, 200);
fitInView(scene()->sceneRect());
show();
}
void Test::resizeEvent(QResizeEvent * event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect());
}
因此,当我运行程序时,我有这个窗口,并且可以拖动项目。一切看起来都不错。
但是如果我仔细观察,拖动区域比项目本身更大。 (请参见下面屏幕截图中的蓝色矩形)蓝色矩形意味着如果我在此矩形中移动鼠标,光标会发生变化,并且我可以拖动该项目。
我感觉问题与“fitInView(scene()->sceneRect());”有关。 ”线。如果我删除它,它就会起作用。 例如,如果我添加“scale(5,1)”,则会出现同样的问题。
您知道问题出在哪里吗?
I have a problem with a QGrahicsRectItem
in a QGraphicsScene
. What I would like is to be able to move the item with mouse. But the problem is that in my case, the dragging rectangle is bigger than the item itself.
Here is the code that I use:
class Test: public QGraphicsView
{
public:
Test();
private:
virtual void resizeEvent (QResizeEvent * event);
QGraphicsScene* m_pScene;
};
Test::Test()
{
m_pScene = new QGraphicsScene();
setScene(m_pScene);
m_pScene->setSceneRect(0, 0, 100, 100);
for (int i = 0 ; i < 10 ; i++)
{
QGraphicsRectItem * pItem = new QGraphicsRectItem();
pItem->setFlag(QGraphicsItem::ItemIsMovable);
pItem->setBrush(QBrush(QColor(190, 100, 100)));
pItem->setRect(QRectF(10*i, 10, 5, 80.f));
pItem->setCursor(Qt::SizeAllCursor);
m_pScene->addItem(pItem);
}
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
resize(600, 200);
fitInView(scene()->sceneRect());
show();
}
void Test::resizeEvent(QResizeEvent * event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect());
}
So when I run my program I have this window, and I can drag items. All seems OK.
But if I look closer the dragging zone is bigger than the item itself. (see the blue rectangle on following screenshot) The blue rectangle means that If I move the mouse in this rectangle, the cursor changes, and I can drag the item.
I have the feeling that the problem is related to the “fitInView(scene()->sceneRect());” line. If I remove it, then it works.
If I add a ‘scale(5,1)’ for example, there is the same problem.
Do you have an idea of what the problem could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您遇到了这个错误: http://bugreports.qt-project.org/browse/ QTBUG-17985
发生的情况是 Qt 使用大小为 1x1 的场景矩形来测试项目是否位于光标下方。在您(和我)的情况下,场景中的 1 个单位大于屏幕上的 1 个像素。因此,1x1 矩形覆盖了距离光标超过 1 个像素的区域,并触及不在光标下方的项目。
我已提交修复/合并请求。您可以修改您的 Qt 源代码以使其暂时正常工作。
I think you hit this bug: http://bugreports.qt-project.org/browse/QTBUG-17985
What happens is Qt uses a scene rectangle that is 1x1 in size to test if items are under the cursor. In your (and my) case, 1 unit in the scene is larger than 1 pixel on screen. So the 1x1 rectangle covers more than 1 pixel from the cursor, and touches items that are not under the cursor.
I have submitted a fix/merge request. You can modify your Qt source to make it work for now.have