【问题标题】:PySide + QGraphicsScene just showing up as blackPySide + QGraphicsScene 只是显示为黑色
【发布时间】:2012-06-25 16:52:54
【问题描述】:

我只想在QGraphicsView 中向QGraphicsScene 写“hello world”。我究竟做错了什么?我在 Designer 中创建了一个 QGraphicsView,然后在我的 __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))

我得到的只是这个,有一张空白的黑色画布:

【问题讨论】:

    标签: python pyqt pyside qgraphicsview


    【解决方案1】:

    有两个问题...

    首先,在 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 中注释掉这一行(或在 Designer 中取消设置画笔设置),不要进行其他任何更改:

        # self.graphicsView.setForegroundBrush(brush)
    

    【讨论】:

    • 所以我在黑色上做黑色。哎呀。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多