• 4642阅读
  • 8回复

QGraphicsProxyWidget如何移动 [复制链接]

上一主题 下一主题
离线songhuirong1
 

只看楼主 倒序阅读 楼主  发表于: 2016-09-24
在场景中加入了QGraphicsProxyWidget,在QGraphicsProxyWidget嵌入了QWidget,然后设置了QGraphicsProxyWidget的ItemIsMovable,但是发现QGraphicsProxyWidget还是无法移动。请问QGraphicsProxyWidget要如何才能和其它item一样可以移动。
离线songhuirong1

只看该作者 1楼 发表于: 2016-09-24
希望高人指点啊
离线songhuirong1

只看该作者 2楼 发表于: 2016-09-25
这论坛的人气怎么这么低
离线songhuirong1

只看该作者 3楼 发表于: 2017-02-08
这问题没人知道吗
离线kimtaikee

只看该作者 4楼 发表于: 2017-02-08

离线songhuirong1

只看该作者 5楼 发表于: 2017-02-09
回 kimtaikee 的帖子
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) 

非常感谢这位兄弟,我在网上搜索过相关内容,但是一直没找到。真是太感谢你了。
离线songhuirong1

只看该作者 6楼 发表于: 2017-02-09
回 kimtaikee 的帖子
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) 

再次致谢。问题解决了。
离线songhuirong1

只看该作者 7楼 发表于: 2018-01-22
新建一个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时,移动无效
离线songhuirong1

只看该作者 8楼 发表于: 2018-01-22
void ProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF pos = event->pos();
    QPointer<QWidget> alienWidget = widget()->childAt(pos.toPoint());
    if (qobject_cast<QPushButton*>(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);
}
快速回复
限100 字节
 
上一个 下一个