QT QGraphicsProxyWidget对象可选择或移动的一些tricks

我在QT图形视图框架中使用QGraphicsProxyWidget嵌入widget,但是无法使其和其它的QGraphicsItem一样可以选择或移动,使用如下语句无效:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
 
// Create new QGraphicsScene and assign to graphicsView
scene =  new  QGraphicsScene( this );
ui->graphicsView->setScene(scene);

// Create widget and add to scene
MyWidget *widget =  new  MyWidget;
QGraphicsProxyWidget *proxy = scene->addWidget(widget);

// Make selectable
proxy->setFlag(QGraphicsItem::ItemIsSelectable,  true );
// Make movable
proxy->setFlag(QGraphicsItem::ItemIsMovable,  true );

于是,我尝试继承QGraphicsProxyWidget获得自己的ProxyWidget,并通过重写鼠标事件响应函数来实现,在具体的实现中我尝试过调用QGraphicsProxyWidget的鼠标事件函数,也尝试过调用QGraphicsItem的鼠标事件函数,但是仅仅能捕获鼠标按下事件(mousePressEvent),其它诸如移动和释放均无法响应。

紧接着,我去Stack Overflow上查找解决方案,倒是有一些方案,纯属tricks性的现在总结如下:

实例1:给代理widget设置一个比其大一点的父类QGraphicsWidget充当拖拽处理效果

 

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
int  main( int  argc,  char  *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsView  view(&scene);
    
    QGraphicsProxyWidget *proxy = scene.addWidget(
new  QPushButton( "MOVE IT" ));
    
// make parent widget larger that button
    QGraphicsWidget* parentWidget =  new  QGraphicsWidget();
    parentWidget->setMinimumSize(QSizeF(proxy->widget()->width(), proxy->widget()->height()));
    parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
    
//默认灰色,不设置则为背景色
     //parentWidget->setAutoFillBackground(true);
    scene.addItem(parentWidget);
    
// put your wrapped button onto the parent graphics widget
    proxy->setParentItem(parentWidget);

    view.setFixedSize(QSize(
600 400 ));
    view.show();

    
return  a.exec();
}

实例2:给代理widget设置一个比其大一点的QGraphicsRectItem充当窗口标题栏样式的拖拽处理效果

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
int  main( int  argc,  char  *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsView  view(&scene);

    
auto  *item =  new  QGraphicsRectItem( 0 0 75 40 );
    
auto  *proxy =  new  QGraphicsProxyWidget(item);
    
auto  *btn =  new  QPushButton(QObject::tr( "Click me" ));
    btn->setGeometry(
0 0 77 26 );
    item->setFlag(QGraphicsItem::ItemIsMovable);
    item->setBrush(Qt::darkYellow);
    proxy->setWidget(btn);
    proxy->setPos(
0 15 );
    scene.addItem(item);

    view.setFixedSize(QSize(
600 400 ));
    view.show();

    
return  a.exec();
}

 实例3:给代理widget设置一个比其大一点的QGraphicsRectItem充当边缘的拖拽处理效果

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
int  main( int  argc,  char  *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsView  view(&scene);

    
auto  *dial=  new  QDial();                                             // The widget
     auto  *handle =  new  QGraphicsRectItem(QRect( 0 0 120 120 ));         // Created to move and select on scene
     auto  *proxy2 =  new  QGraphicsProxyWidget(handle);                     // Adding the widget through the proxy

    dial->setGeometry(
0 0 100 100 );
    dial->move(
10 10 );
    proxy2->setWidget(dial);
    handle->setPen(QPen(Qt::transparent));
    handle->setBrush(Qt::gray);
    
//handle->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    scene.addItem(handle);                                               // adding to scene

    view.setFixedSize(QSize(
600 400 ));
    view.show();

    
return  a.exec();
}

勉勉强强可以实现拖拽的效果吧!

更直接的效果请期待后续博文深入研究!

转载于:https://www.cnblogs.com/MakeView660/p/10364131.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值