- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 QGraphicsView
区域,它显示一些 Item 项。我想实现鼠标移动。
class Item
{
public:
Item();
void update();
int x, y; // position
int z; // order - should correspond also to index in item list
};
class Collection : public QGraphicsView
{
Q_OBJECT
public:
Collection(QWidget *parent = 0);
void update();
QList<Item> *m_items;
protected:
virtual void paintEvent(QPaintEvent * event);
virtual void mousePressEvent(QMouseEvent * event);
virtual void mouseReleaseEvent(QMouseEvent *event);
private:
QPoint offset;
int itemMoved;
};
尝试:
void Collection::mousePressEvent(QMouseEvent* event)
{
Item *currentItem = NULL;
itemMoved = -1;
foreach (QGraphicsItem *item, this->items(event->pos()))
{
// never get into this loop since my items are not children of QGraphicsItem
currentItem = dynamic_cast<Item*>(item);
if (event->button() == Qt::LeftButton && currentItem)
{
itemMoved = currentItem->z;
offset = event->pos();
}
else if (event->button() == Qt::RightButton)
{
// set right click stuff
}
else
{
event->ignore();
}
break;
}
}
void CollectionView::mouseReleaseEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton && itemMoved > 0)
{
m_items->at(itemMoved).x = event->pos().x();
m_items->at(itemMoved).y = event->pos().y();
// So far multiple fails:
// error: assignment of data-member 'Item::x' in read-only structure
// error: assignment of data-member 'Item::y' in read-only structure
// error: passing 'const Item' as 'this' argument of 'void Item::update()' discards qualifiers
m_items->at(itemMoved).update();
update();
}
}
我会让我的 Item
继承 QGraphicsItem
但随后我收到有关我不知道如何定义的虚函数的错误(例如 boundingRect
这将取决于项目内容...可能是对象、图像、svg、文本...对于一些我知道如何获取边界矩形但对于其他人来说真的很奇怪)...这是唯一的方法吗?
我如何识别鼠标位置处的项目,是什么阻止我在鼠标移动后更改项目的 x
和 y
?
最佳答案
如果您要自己实现所有内容,则根本不应该使用 QGraphicsView
。如果您希望使用 QGraphicsView
,则无需重新实现其任何虚拟方法。只需按原样使用它,并设置项目的 QGraphicsItem::ItemIsMovable
标志。
this answer 中提供了一个完整的示例.重新实现 QGraphicsView::mousePressEvent
的唯一原因是实现新项目的创建。
关于c++ - 在 QGraphicsView/QGraphicsScene 上用鼠标移动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905460/
我总是在使用 QGraphicsScene 坐标系时遇到问题。我的场景创建起来非常简单,如下所示: scene = new QGraphicsScene(this); this->setScene(s
我将描述我的问题。 我有一个选项卡类,大致如下所示: class Tab : public QWidget { public: Tab(); ... private: ... QG
我正在尝试使用 QGraphics 变换矩阵进行平移和缩放。缩放有效,但平移参数 m31 和 m32 无效。看起来还有一个以场景为中心的功能。在下面的代码中,我操纵 rectScene 进行平移,但为
我想做一个 QGraphicsScene并显示在 QGraphicsView .我想通过鼠标中键滚动场景并通过左键选择橡皮筋。但我不知道如何使橡皮筋选择仅通过鼠标左键出现。 这是我的代码: # -*-
我将 QGraphicsView 子类化并在我的布局中显示小部件。在 View 中,我自然有 QGraphicsScene。 我可以很好地检索 QGraphicsScene 大小(即我设置的 2100
我想在我的 (Py)Qt 应用程序中的 QGraphicsView 上实现滚动/平移功能。它应该是这样工作的:用户按下鼠标中键, View 随着用户移动鼠标而滚动(这是一个非常常见的功能)。 我尝试使
我的QGraphicsView项目比其背景图像大很多,但我希望该背景图像仅绘制一次。现在,整个事物都被背景图像填充了多少次,只要有足够的空间即可。 那么,如何强制将背景图像仅绘制到 View 中心一次
我想要一个永远不会自动滚动的 QGraphicsView。 类似:基本上,我的问题与 http://developer.qt.nokia.com/forums/viewthread/2220 相同,但
在我的应用程序中,我添加了添加了像素图的 QGraphicsScene,并且所有内容都在关闭滚动条的情况下在 QGraphicsView 中查看。程序窗口比像素图小,当我按箭头键或移动鼠标滚轮时,像素
我想将声子视频小部件放置到 qgraphicsscene 上,以便我可以覆盖图形等。当我运行以下命令时,我听到声音,但 qgraphicsview 上没有播放视频。我将不胜感激,因为我认为我正在做示例
我有一个带有“ScrollBarAlwaysOff”垂直滚动条策略的 QGraphicsView。问题是,当我调整 View 的大小(通过 QSplitter 或仅通过调整窗口的大小)时, View
我正在 PyQt 中使用 QGraphicsView 构建一个 GUI,它将显示互连项目的大型网络,并且我希望能够叠加一个较小的门户来显示网络的远处部分 - 有点像“画中画” “之类的事情。这本身并不
您好,我有一个 GridLayout,上面有 64 个 GraphicsView(我知道它很多,但这是我目前唯一能想到的方法)。现在,我目前只是在计时器刻度上在这些图形 View 中的每一个上绘制一条
如何将新的矩形附加到 QgraphicView 在此代码中,当单击按钮时创建矩形。但是当我第二次单击按钮时,先前创建的矩形被删除。我需要创建 2 个矩形当我点击按钮 2 次时。当我点击按钮 3 次时创
我有一个 QDialog,它包含几个停靠小部件和一个 QGraphicsView。小部件布局设置为网格,QGraphicsView 大小策略设置为固定在 2 个轴上,并且 QGraphicsView
我有两个关于 QGraphicsView Framework 的问题。 第一个问题是关于一条特殊的线:我想问一下是否可以画一条蛇形线或波浪线(如链接图片中所示)?如果有任何解决方案,我将不胜感激,因为
我的 QGraphicsView 有一些问题。我将 QGraphicsView 子类化以重新定义 paintEvent。我想使用 QGraphicsItem 绘制某种树的节点和 QGraphicsLi
我在根据需要扩展 QWidgets 时遇到了极大的麻烦。这不是我第一次遇到这个问题,上次我通过 hacking 一个大的 sizeHint() 解决了它。这是错误的方法,我真的很想学习正确的方法。 如
我在我的应用程序中使用 QGraphicsScene/QGraphicsView 对。为了我的目的,我将它们分类。生成该对的代码片段如下: itsScene = new QGraphicsScene;
如何让QGraphicsView处理拖放?比如,从某个文件夹中拖出一个图像文件,然后放到 QGraphicsView 中? Windows、QT5.2、C++。 最佳答案 你需要创建一个 QGraph
我是一名优秀的程序员,十分优秀!