我想做的就是将“hello world”写入 aQGraphicsScene
中的 a QGraphicsView
。我究竟做错了什么?我QGraphicsView
在 Designer 中创建了一个,然后在我的中__init__
创建了一个QGraphicsScene
并添加了一些文本......但我得到的只是一个黑色窗口。
import sys
from PySide import QtCore, QtGui
from PySide.QtGui import *
class MyDialog(QDialog):
def __init__(self):
super(MyDialog, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.scene = QGraphicsScene()
self.ui.graphicsView.setScene(self.scene)
self.scene.setSceneRect(0,0,100,100)
self.scene.addText('hello')
def main(argv):
app = QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
app.exec_()
sys.exit()
if __name__ == "__main__":
main(sys.argv)
以下是 Designer 的 UI 代码:
# everything from here down was created by Designer
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.graphicsView = QtGui.QGraphicsView(Dialog)
self.graphicsView.setGeometry(QtCore.QRect(50, 30, 256, 192))
self.graphicsView.setMouseTracking(False)
self.graphicsView.setFrameShadow(QtGui.QFrame.Sunken)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
self.graphicsView.setForegroundBrush(brush)
self.graphicsView.setObjectName("graphicsView")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
我得到的就是这个,有一张空白的黑色画布: