2012-06-23 63 views
1

我想要做的就是將「hello world」寫入QGraphicsSceneQGraphicsView。我究竟做錯了什麼?我在Designer中創建了一個QGraphicsView,然後在我的__init__中創建了一個QGraphicsScene並添加了一些文本...但我得到的只是一個黑色窗口。PySide + 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) 

以下是從設計的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)) 

我得到的是這樣的,有一個空白的黑色帆布:

screenshot

回答

3

有兩個問題..

首先,在QGraphicsView中將前景刷設置爲黑色。這意味着所有的孩子都會繼承,除非他們另有規定。

UI

brush = QtGui.QBrush(QtGui.QColor(0, 0, 0)) 
    brush.setStyle(QtCore.Qt.SolidPattern) 
    #self.graphicsView.setForegroundBrush(brush) 
    self.graphicsView.setBackgroundBrush(brush) 

而且,因爲你改變的背景爲黑色,你應該設置你的文字顏色就可以看到。因爲這將是黑色的默認:

主要

text = self.scene.addText('hello') 
    text.setDefaultTextColor(QtGui.QColor(QtCore.Qt.red)) 

如果你不關心的黑色背景,只是單純的想看到它的工作,只是註釋掉這行你的UI(或者取消設置器中的畫筆設置)並且不會改變其他任何東西:

# self.graphicsView.setForegroundBrush(brush) 
+0

所以我在黑色的黑色。哎呦。謝謝! – dmd