回复: QGraphicsProxyWidget如何移动
#6 回 kimtaikee 的帖子 [songhuirong1 02-09 15:40]
kimtaikee:这问题不是没人知道,是你和我们一样懒。
http://www.qtcn.org/bbs/simple/?t19832.html
http://stackoverflow.com/questions/15413564/make-qgraphicsproxywidget-movable-selectable
http://www.qtcentre.org/threads/28434-Move-QGraphicsProxyWidget (2017-02-08 14:08)
再次致谢。问题解决了。
#7 [songhuirong1 01-22 21:41]
新建一个item从QGraphicsProxyWidget继承,添加的时候加你重写的这个item
然后要重写这个item的mouseMoveEven,在这个函数内调用QGraphicsItem::mouseMoveEvent,
默认的可能是调用的QGraphicsProxyWidget::mouseMoveEvent,这样的话鼠标事件被widget处理了,item没有取到,所以不会动
你也可以试试创建的widget是一个按钮, QWidget* widget = new QPushButton;
看你鼠标按上去按钮会不会动,如果会动,肯定鼠标事件被按钮处理了,按照上面的方法应该可以。
这位兄台,你分析的很有道理啊
不过还是出现了一点小问题,代码如下
void XGraphicsProxyWidget::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
if (event->type() == QEvent::GraphicsSceneMouseMove)
{
QGraphicsItem::mouseMoveEvent(event);
return ;
}
QGraphicsProxyWidget::mouseMoveEvent(event);
}
m_pScene->clear();
QPushButton* widget = new QPushButton;
//QWidget *widget = new QWidget;
XGraphicsProxyWidget *proxy = new XGraphicsProxyWidget();
proxy->setWidget(widget);
proxy->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_pScene->addItem(proxy);
这里有个很奇怪的现象是,当用QPushButton时,移动有效,但是控件的颜色看上去很奇怪,是渐变的,不是同一个颜色
但当用QWidget时,移动无效
#8 [songhuirong1 01-22 21:45]
void ProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos = event->pos();
QPointer alienWidget = widget()->childAt(pos.toPoint());
if (qobject_cast(alienWidget))
{
QGraphicsProxyWidget::mousePressEvent(event);
grabbedByWidget=true;
}
else
{
QGraphicsItem::mousePressEvent(event);
grabbedByWidget=false;
}
}
void ProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (grabbedByWidget)
QGraphicsProxyWidget::mouseReleaseEvent(event);
else
QGraphicsItem::mouseReleaseEvent(event);
grabbedByWidget=false;
}
void ProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (grabbedByWidget)
return;
QGraphicsItem::mouseMoveEvent(event);
}