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();
}

勉勉強強可以實現拖拽的效果吧!

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM