Python QGraphicsView/QGraphicsCenter中的按钮未正确绘制

Python QGraphicsView/QGraphicsCenter中的按钮未正确绘制,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,当我在场景和QGraphicsView中放置按钮时,按钮右侧的区域错误地变为灰色 我可以在Windows和Linux中复制这一点。有什么技巧可以消除这个不需要的特性吗 from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QPushButton, QLabel) from PyQt5.QtCore import (Qt, QRectF) fro

当我在场景和QGraphicsView中放置按钮时,按钮右侧的区域错误地变为灰色

我可以在Windows和Linux中复制这一点。有什么技巧可以消除这个不需要的特性吗

from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene,
                             QPushButton, QLabel)
from PyQt5.QtCore import (Qt, QRectF)
from PyQt5 import QtCore


class MyView(QGraphicsView):
    
    def __init__(self, parent = None):

        super(MyView, self).__init__(parent)
              
        self.button1 = QPushButton('Button1')
        self.button1.setGeometry(-60, -60, 80, 40)
        self.button2 = QPushButton('Button2')
        self.button2.setGeometry(10, 10, 80, 40)
        version  = 'PYQT_VERSION_STR: ' + QtCore.PYQT_VERSION_STR + '\n'
        version += 'QT_VERSION_STR: ' + QtCore.QT_VERSION_STR + '\n'
        self.label = QLabel(version)
        self.label.setGeometry(-100, 80, 160, 80)
        
        self.setScene(QGraphicsScene())
        self.scene().addWidget(self.button1)
        self.scene().addWidget(self.button2)
        self.scene().addWidget(self.label)
        
        self.scene().setSceneRect(QRectF(-150, -150, 300, 300))

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    widget = MyView()
    widget.show()
    

    sys.exit(app.exec_())

将小部件添加到图形场景时,其代理使用小部件的
minimumSizeHint()
作为其几何体的最小大小,无论您是否将小部件调整为较小的大小(我不知道这是错误还是设计造成的)

结果如下:

  • 您不能为代理设置小于源小部件的最小大小[提示]的几何图形
  • 将小部件的大小调整为小于最小[hint]的大小只会调整小部件的大小,但代理仍将使用该最小大小[hint]
例如,QPushButton的最小大小提示约为80x30(实际值取决于使用的样式和字体),因此即使将按钮调整为较小的大小,其代理仍将为80x30


为了避免这种情况,您可以手动将小部件的最小大小设置为合理的值,或者对小部件进行子类化并覆盖。

@eyllansc。问题已用版本更新。