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

时间:2018-08-24 14:50:29

标签: c++ qt qwidget qgraphicsscene qgraphicswidget

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

我该如何解决?

2 个答案:

答案 0 :(得分:1)

原因

source code看,

QGraphicsWidget 不会绘制任何内容(包括选择矩形):

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

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

答案 1 :(得分:0)

我知道这个问题很旧,但是对于任何寻求相同答案的人来说,这都是python代码。每当单击小部件时,它将绘制边框。

   class rectangle(QtWidgets.QGraphicsProxyWidget):
        def __init__(self):
            super().__init__()
            self.container = Container()  # a class inheriting Qwidget class
            self.setWidget(self.container)
            self.setFlag(QGraphicsItem.ItemIsSelectable, True) 
    
        def paint(self, qp, opt, widget):
            qp.save()
            pen = QtGui.QPen()
            pen.setColor(Qt.transparent)
            pen.setWidth(3)
    
    
            if self.isSelected():
                pen.setColor(Qt.yellow)  # selection color of your choice
    
            qp.setBrush(Qt.transparent)
            qp.setPen(pen)
            qp.drawRect(0, 0, self.container.width(), self.container.height())  # draws rectangle around the widget
    
    
            super().paint(qp, opt, widget)
            qp.restore()
    
        def mousePressEvent(self, event):
    
            if event.button() == Qt.LeftButton:
                self.setSelected(True)
            
            super().mousePressEvent(event)