为什么在 QGraphicsScene 中看不到 QGraphicsWidget 的选择边框?

萨加尔AW

我通过QGraphicsProxyWidget添加了一个小部件到图形场景(QGraphicScene)问题是当我选择它被选中的项目时,但选择边框不可见。

这是代码:

    QDial *dial= new QDial(); // Widget 
    dial->setGeometry(3,3,100,100);// setting offset for graphicswidget and widget

    QGraphicsWidget *ParentWidget = new QGraphicsWidget();// created to move and select on scene
    ParentWidget->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    Scene->addItem(ParentWidget); // adding to scene

    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();// adding normal widget through this one
    proxy->setWidget( DialBox );
    proxy->setParentItem(ParentWidget);

这是输出:

输出

我怎么能解决这个问题?

斯科恰诺夫

原因

源代码可以看出,QGraphicsWidget不绘制任何内容(包括选择矩形)

void QGraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}

然而,QGraphicsRectItem确实(请参阅源代码):

void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                              QWidget *widget)
{
    ...

    if (option->state & QStyle::State_Selected)
        qt_graphicsItem_highlightSelected(this, painter, option);
}

解决方案

我的解决方案是使用QGraphicsRectItem而不是QGraphicsWidget作为选择/移动表盘的句柄,如下所示:

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

dial->setGeometry(0, 0, 100, 100);
dial->move(10, 10);

proxy->setWidget(dial);

handle->setPen(QPen(Qt::transparent));
handle->setBrush(Qt::gray);
handle->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

Scene->addItem(handle); // adding to scene

此代码产生以下结果:

在此处输入图片说明

单击表盘小部件周围的深灰色区域以选择/移动它或小部件本身以与其进行交互。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在TFS中看不到分支树?

为什么在C#中看不到Ping类?

为什么在SSMS结果列表中看不到全名?

为什么在图形绘制中看不到弯曲的边缘?

为什么在PowerShell中看不到脚本的参数?

为什么在UIScrollView中看不到UIStackView?

为什么我在gdb backtrace中看不到行号?

为什么在Swift中看不到图像?

为什么在Nautilus中看不到缩略图?

为什么我的GridView对象在代码中看不到?

为什么在cloudboost中看不到我的表?

为什么模块在引擎代码中看不到类?

在Shiny mainPanel中看不到条件选择吗?

为什么我的SelectionGrid在Unity Editor中看不到GameObject?

为什么在Windows 10上已安装的应用程序中看不到WIndows Powershell

为什么在新打开的窗口中看不到glyphicon图标?

为什么在终端输出中看不到单位分隔符(ASCII 31)?

为什么在迭代过程中看不到Iterator的修改?

为什么在同一包装中看不到包装保护方法?

为什么在优胜美地的Xcode中看不到iOS 7.0模拟器?

为什么在此Java动态Web项目中看不到任何主要方法?

为什么在dfs.data.dir指定的路径中看不到阻止文件?

为什么我在 Fabric 中看不到我的 android 应用程序图标?

为什么在调用函数中看不到 putTMVar 的函数中填充了 TMVar?

为什么我的Java文件在我的R文件中看不到新元素?

为什么python在virtualenv中看不到dist-packages?

为什么在 Next.js 项目中看不到轮播?

为什么在XCode 5 Organizer中看不到预配配置文件?

为什么我在Chrome中看不到我的HTTP标头?