![](/img/trans.png)
[英]QComboBox with autocompletion works in PyQt4 but not in PySide
[英]QGraphicsGridLayout works in PySide but not PyQt4
我一直在努力幾個小時,以使QGraphicsGridLayout
與PyQt4
。 我已經安裝了PySide
所以我將導入切換到該導入以進行快速檢查,並且按預期運行! 對於下面的代碼,使用PySide
時, PySide
預期方式調用RectangleWidget
上的paint
方法,但是當您使用PyQt4
,則永遠不會調用paint
方法。
我知道RectangleWidget
應該重寫一些更多的虛擬方法以實現正確的實現,但是我正在剝離一些東西以嘗試並獲取最少的代碼來縮小問題的范圍。
from PySide import QtGui, QtCore
# from PyQt4 import QtGui, QtCore
class RectangleWidget(QtGui.QGraphicsWidget):
def __init__(self, rect, parent=None):
super(RectangleWidget, self).__init__(parent)
self.rect = rect
def paint(self, painter, *args, **kwargs):
print('Paint Called')
painter.drawRect(self.rect)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.central_widget = QtGui.QWidget(self)
central_layout = QtGui.QHBoxLayout()
self.central_widget.setLayout(central_layout)
self.setCentralWidget(self.central_widget)
self.resize(500, 500)
self.view = QtGui.QGraphicsView()
self.scene = QtGui.QGraphicsScene()
self.view.setScene(self.scene)
panel = QtGui.QGraphicsWidget()
self.scene.addItem(panel)
layout = QtGui.QGraphicsGridLayout()
panel.setLayout(layout)
for i in range(4):
for j in range(4):
rectangle = RectangleWidget(QtCore.QRectF(0, 0, 50, 50))
layout.addItem(rectangle, i, j)
central_layout.addWidget(self.view)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
widget = MainWindow()
widget.show()
app.exec_()
任何幫助表示贊賞! 我想保持與PyQt4
和PySide
兼容性,因此僅繼續使用PySide
並不是一個理想的解決方案。 謝謝
QGraphicsGridLayout
擁有添加到其中的項目的所有權(更多信息請參見文檔 )。
在您的示例中,似乎所有RectangleWidget
項目一旦超出范圍,就有被垃圾回收的危險。 因此,如果您給他們父母:
rectangle = RectangleWidget(QtCore.QRectF(0, 0, 50, 50), panel)
一切都應該很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.