[英]How to move a figure(created using paintEvent) by simply draging it in PyQt5
[英]How simply display a QColor using PyQt?
我有一個QColor,我從QColorDialog.getColor()得到。 我想在表單中將其顯示給用戶。
我怎樣才能做到這一點 ?
我嘗試使用QGraphicView並像這樣設置backgroundBrush:
self.displayColor = QtGui.QGraphicView(self)
self.color = QtGui.QColor(category.color)
self.displayColor.setBackgroundBrush(QtGui.QBrush(self.color))
但即使我更改了背景刷,小部件也會保持白色。
我怎么強迫它重繪背景?
謝謝
s=QGraphicsScene()
s.setBackgroundBrush(QColor(0,255,0))
g=QGraphicsView(s)
g.render(QPainter())
最后,我選擇創建一個這樣的小部件:
# -*- 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()更改顏色
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.