如何使用PyQt简单地显示QColor?

2024-10-17 08:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QColor,是从QColorDialog.getColor()获得的。 我想在表单中向用户显示它。

我该怎么做?

我尝试使用QGraphicView并设置背景画笔,如下所示:

    self.displayColor = QtGui.QGraphicView(self)
    self.color = QtGui.QColor(category.color)
    self.displayColor.setBackgroundBrush(QtGui.QBrush(self.color))

但是即使我换了背景刷,小部件还是保持白色。

我怎么能强迫它重新粉刷背景呢?

谢谢


Tags: 用户self表单color背景categoryqtgui中向
2条回答

最后,我选择创建这样的小部件:

# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore

class ColorDisplay(QtGui.QWidget):
    def __init__(self, parent):
        super(ColorDisplay, self).__init__(parent)

        self.color = None

    def setColor(self, color):
        self.color = QtGui.QColor(color)
        self.update()

    def paintEvent(self, event=None):
        painter = QtGui.QPainter(self)
        if self.color is not None:
            painter.setBrush(QtGui.QBrush(self.color))
            painter.drawRect(self.rect())

    def getColorName(self):
        return unicode(self.color.name())

我可以使用setColor()更改颜色

s=QGraphicsScene()
s.setBackgroundBrush(QColor(0,255,0))
g=QGraphicsView(s)
g.render(QPainter())

相关问题 更多 >