gpt4 book ai didi

c++ - 为什么 QGraphicsWidget 的选择边框在 QGraphicsScene 中不可见?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:02 27 4
gpt4 key购买 nike

我通过 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);

这是输出:

Output

我该如何解决这个问题?

最佳答案

原因

QGraphicsWidget 不绘制任何东西(包括选择矩形),如 source code 所示:

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

QGraphicsRectItem,但是,确实如此(参见 source code ):

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

此代码产生以下结果:

enter image description here

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

关于c++ - 为什么 QGraphicsWidget 的选择边框在 QGraphicsScene 中不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com