Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python addText()更改QGraphicsView中的文本颜色_Python_Python 2.7_Pyqt4_Qgraphicsview - Fatal编程技术网

Python addText()更改QGraphicsView中的文本颜色

Python addText()更改QGraphicsView中的文本颜色,python,python-2.7,pyqt4,qgraphicsview,Python,Python 2.7,Pyqt4,Qgraphicsview,我已经设法在从showGui.UI导入的QGraphicsView中创建了一个垂直文本标签。一切都是我想要的,除了我不知道如何改变文本的颜色。setStyleSheet不适用于QGraphicsView。我用QPainter做了另一次尝试,但我无法让它正确地在我的图形视图中漫游。我发现的所有文件都是C++,这让我困惑不解。我想加上: self.trans_graphicsView_cat.drawForeground(QPainter(setPen(QtGui.QColor(168, 34,

我已经设法在从showGui.UI导入的QGraphicsView中创建了一个垂直文本标签。一切都是我想要的,除了我不知道如何改变文本的颜色。setStyleSheet不适用于QGraphicsView。我用QPainter做了另一次尝试,但我无法让它正确地在我的图形视图中漫游。我发现的所有文件都是C++,这让我困惑不解。我想加上:

  self.trans_graphicsView_cat.drawForeground(QPainter(setPen(QtGui.QColor(168, 34, 3)))
到createScene函数中,我就可以了,但我还没能破解它

我的代码:

class MainDialog(QtGui.QMainWindow, showGui.Ui_MainWindow):
    dbPath = appDataPath + "barter.db"
    dbConn = sqlite3.connect(dbPath)
    dbConn.text_factory = str



def __init__(self, parent=None):
    super(MainDialog, self).__init__(parent)
    self.setupUi(self)

    self.text = self.tr("Categories")
    self.createScene()

def createScene(self):
    scene = QtGui.QGraphicsScene()
    self.trans_graphicsView_cat.setScene(scene)
    item = scene.addText(self.text, QtGui.QFont('Arial Black', 15, QtGui.QFont.Light))
    item.rotate(270)
该方法返回一个。这个类有一个方法可以让你改变文本的颜色


或者,您可以创建自己的
QGraphicsTextItem
实例,然后使用将其添加到场景中。请注意,
QGraphicsTextItem
类有一个方法,可以让您更好地控制文本的格式。

谢谢,第一个建议成功了!我想更好地理解第二种选择,但我想我必须看到用python编写的代码示例。不过你已经做得够多了!再次感谢,我在下面发布了工作代码。