为什么在 QGraphicsScene 中看不到 QGraphicsWidget 的选择边框? [英] Why is the selection border of a QGraphicsWidget not visible in QGraphicsScene?
问题描述
我通过 QGraphicsProxyWidget 向图形场景 (QGraphicScene) 添加了一个小部件.问题是当我选择它被选中的项目时,但选择边框不可见.
代码如下:
QDial *dial= new QDial();//小部件dial->setGeometry(3,3,100,100);//设置图形小部件和小部件的偏移量QGraphicsWidget *ParentWidget = new QGraphicsWidget();//创建用于在场景中移动和选择ParentWidget->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);场景->addItem(ParentWidget);//添加到场景QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();//通过这个添加普通widget代理-> setWidget( DialBox );代理-> setParentItem(ParentWidget);
输出如下:
我该如何解决这个问题?
原因
QGraphicsWidget 不绘制任何内容(包括选择矩形),如
单击表盘小部件周围的深灰色区域以选择/移动它或小部件本身以与其进行交互.
I have added a widget to a graphic scene (QGraphicScene) through a QGraphicsProxyWidget. The problem is that when I select the item it's selected, but the selection border is not visible.
Here is the code:
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);
Here is the output:
How could I fix this?
Cause
QGraphicsWidget does not paint anything (including a selection rectangle), as seen from the source code:
void QGraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(painter);
Q_UNUSED(option);
Q_UNUSED(widget);
}
QGraphicsRectItem, however, does (see the source code):
void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
...
if (option->state & QStyle::State_Selected)
qt_graphicsItem_highlightSelected(this, painter, option);
}
Solution
My solution would be to use QGraphicsRectItem instead of QGraphicsWidget as a handle to select/move the dial like this:
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
This code produces the following result:
Click the dark gray area around the dial widget to select/move it or the widget itself in order to interact with it.
这篇关于为什么在 QGraphicsScene 中看不到 QGraphicsWidget 的选择边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!