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

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

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

return a.exec();
}

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

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

QT QGraphicsProxyWidget对象可选择或移动的一些tricks的更多相关文章

  1. QT 实现QGraphicsProxyWidget对象可选择或移动(item管理实现)

    上篇博文<QT QGraphicsProxyWidget对象可选择或移动的一些tricks>介绍了实现QT QGraphicsProxyWidget对象可选择或移动的一些小的第三方技巧,但 ...

  2. Qt持久性对象进行序列化

    Mfc和Java中自定义类的对象都可以对其进行持久性保存,Qt持久性对象进行序列化当然也是必不可少的.不过这个问题还真困扰了我很长时间……Mfc通过重写虚函数Serialize().Java则是所属的 ...

  3. Qt持久性对象进行序列化(同时比较了MFC与Java的方法)

    Mfc和Java中自定义类的对象都可以对其进行持久性保存,Qt持久性对象进行序列化当然也是必不可少的.不过这个问题还真困扰了我很长时间……Mfc通过重写虚函数Serialize().Java则是所属的 ...

  4. 哪一个不是EL定义的隐式对象?(选择1项)

    哪一个不是EL定义的隐式对象?(选择1项) A cookie B.pageContext C.attributes D initParam 解答:C 1)pageContext:JSP 页的上下文.它 ...

  5. Qt jsoncpp 对象拷贝、删除、函数调用 demo

    /*************************************************************************************************** ...

  6. Qt 元对象系统(Meta-Object System)(不管是否使用信号槽,都推荐使用)

    Qt 元对象系统(Meta-Object System) Qt的元对象系统基于如下三件事情: 类:QObject,为所有需要利用原对象系统的对象提供了一个基类. 宏:Q_OBJECT,通常可以声明在类 ...

  7. 为基于OpenCV的图像处理程序编写界面—关于QT\MFC\CSharp的选择以及GOCW的介绍

            基于OpenCV编写图像处理项目,除了算法以外,比较重要一个问题就是界面设计问题.对于c++语系的程序员来说,一般来说有QT/MFC两种考虑.QT的确功能强大,特别是QML编写andr ...

  8. 【4opencv】为基于OpenCV的图像处理程序编写界面—关于QT\MFC\CSharp的选择以及GOCW的介绍

            基于OpenCV编写图像处理项目,除了算法以外,比较重要一个问题就是界面设计问题.对于c++语系的程序员来说,一般来说有QT/MFC两种考虑.QT的确功能强大,特别是QML编写andr ...

  9. Qt界面对象的事件调用

    QMetaObject::invokeMethod(m_mainToolBarItem, "change2DesktopMode", Q_ARG(QVariant, m_curMo ...

随机推荐

  1. 认知计算 Cognitive Computing

    认知计算代表一种全新的计算模式,它包含信息分析,自然语言处理和机器学习领域的大量创新技术. Cognnitive computing refers to systems that learn at s ...

  2. html标签之img

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 将本地的项目推送到github上

    好像还是不能用git在本地直接建一个repository,然后推送到github,这是把本地项目推送到github上已经建好的裤 …or create a new repository on the ...

  4. canconfig 配置命令

    canconfig 配置命令 canconfig can0 restart-ms 1000 bitrate 1000000 ctrlmode triple-sampling on canconfig ...

  5. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    在执行[maven clean]的时候报错,错误信息如下: -Dmaven.multiModuleProjectDirectory system property is not set. Check ...

  6. matlab知识点汇集

    1.设置图线宽度   set( haxis, 'LineWidth', 1.0 ); ----这是 set函数, 'LineWidth'就是axis的线宽度属性,其值默认为0.5,这里可以改成1.0了 ...

  7. 微信小程序滚动Tab实现左右可滑动切换

    --------------------------------------------------------wxml---------------------------------------- ...

  8. JS函数重载解决方案

    JS的函数定义可以指定形式参数名称,多多少少我们会以为js至少可以支持参数个数不同的方法重载,然而遗憾的是这仅仅是一个假象,js所有的参数都是以arguments传递过去的,这个参数类似于数组,在函数 ...

  9. SQLITE WITH ENTITY FRAMEWORK CODE FIRST AND MIGRATION

    Last month I’ve a chance to develop an app using Sqlite and Entity Framework Code First. Before I st ...

  10. php命令行脚本 mock数据

    <?php $con = mysql_connect("192.168.1.5:3306","root","123"); if (!$ ...