• 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(
    600400));
        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(007540);
        
    auto *proxy = new QGraphicsProxyWidget(item);
        
    auto *btn = new QPushButton(QObject::tr("Click me"));
        btn->setGeometry(
    007726);
        item->setFlag(QGraphicsItem::ItemIsMovable);
        item->setBrush(Qt::darkYellow);
        proxy->setWidget(btn);
        proxy->setPos(
    015);
        scene.addItem(item);

        view.setFixedSize(QSize(
    600400));
        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(00120120));        // Created to move and select on scene
        auto *proxy2 = new QGraphicsProxyWidget(handle);                    // Adding the widget through the proxy

        dial->setGeometry(
    00100100);
        dial->move(
    1010);
        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(
    600400));
        view.show();

        
    return a.exec();
    }

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

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

  • 相关阅读:
    蓝牙协议分析(11)_BLE安全机制之SM
    蓝牙协议分析(10)_BLE安全机制之LE Encryption
    蓝牙协议分析(9)_BLE安全机制之LL Privacy
    蓝牙协议分析(8)_BLE安全机制之白名单
    蓝牙协议分析(7)_BLE连接有关的技术分析
    蓝牙协议分析(6)_BLE地址类型
    蓝牙协议分析(5)_BLE广播通信相关的技术分析
    蓝牙协议分析(4)_IPv6 Over BLE介绍
    蓝牙协议分析(3)_BLE协议栈介绍
    ActiveMq
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10364131.html
Copyright © 2020-2023  润新知