tags: coding python pyqt5 pyside2 qt GUI programming
The QColor class provides a color based on RGB, HSV or CMYK values. Colors typically specify with RGB (red, green, and blue) components, but also specify with HSV (color phase, saturation and value) and CMYK (cyan, magenta, yellow and black) components. In addition, you can use the color name to specify a color (color name can be any color name of SVG 1.0).

Image source: doc.qt.io
QColor objects are created based on RGB colors. If you want to make QColor based on HSV (or CMYK), use functions toHSV () and TOCMYK (), these functions returns a desired format QColor object. In addition, use static functions fromRGB (), fromHSV () and fromCMYK () functions can create QColor objects in the corresponding format. Or use the converTto () function, which returns the QColor object in the desired format. You can also use SetRGB (), setHSV (), setCMYK () function to change the color format.
Colors can be set by passing the RGB string (e.g., "# 112233") or the Argb string (e.g., "# ff11233") or color name (eg, "Blue") to the setNamedColor () function. The color name is taken from the SVG 1.0 color name. The format returned by the Name () function is the name of the "#rggbb" color. You can also use setRGB (), setHSV () and setCMYK () to set color. To get a shallow or deeper color, use the LIGHTER () and DARKER () functions separately.
The isvalid () function represents whether QColor is legal. For example, the RGB value of the RGB color is illegal. Due to performance reasons, QColor basically does not consider illegal colors, so the result of using invalid colors cannot be defined.
Color components can be retrieved separately, for example. Red (), hue () and cyan () can be obtained by red, hue, and cyan color components. You can also use GETRGB (), getHSV () and getCmyk () functions at once. When using the RGB color model, you can use the RGB () to access the color component.
Qcolor supports floating point precision, providing floating-point versions of all color component functions, such as getRGBF (), huef (), and fromCMYKF (). Note that since the component is stored using a 16-bit integer, there may be a small deviation between the values returned by the setRGBF () and getRGBF () functions.
The value based on the integer range is 0-255 (hue () except, its value must be in the range of 0-359, based on the floating point function accepts a value of 0.0 - 1.0.
QColor also supports Alpha mix, the color Alpha channel specifies the transparent effect, 0 represents a completely transparent color, and 255 represents a completely opaque color. E.g:
# Specify a translucent red
painter.setBrush(QColor(255, 0, 0, 127))
painter.drawRect(0, 0, self.width()/2, self.height())
# Specify a translucent blue
painter.setBrush(QColor(0,0,255,127))
painter.drawRect(0, 0, self.width(), self.height()/2)
The above code produces the following display effect:

Image source: doc.qt.io
If the value of the color is an integer, you can use the alpha () and setalpha () functions to retrieve and set the color Alpha channel; if the value is QReal, Alphaf () and setalphaf () can be used to retrieve and set up colour. By default, the Alpha channel is set to 255 (opaque). To retrieve and set all RGB color components (including ALPHA channels), use the RGBA () and setRGBA () functions.
The test program uses QLabel to display the color of the QT internal predefined name, the full code is as follows:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel
class DemoColor(QWidget):
def __init__(self, parent=None):
super(DemoColor, self).__init__(parent)
# Set the window title
SELF.SETWINDOWTILE ('actual combat PYQT5: QColor predefined color table ")
self.initUi()
def initUi(self):
layout = QGridLayout()
LINE_COUNT = 8 # Months of each line
index = 0
print(len(QColor.colorNames()))
for name in QColor.colorNames():
color_label = QLabel()
color_label.setMinimumHeight(24)
color_label.setText(name)
# Set background color, border and text color
styleSheet = 'QLabel{background-color: ' + name + '; border:1px solid 0; font: 16px;color: black;}'
if (QColor(name).getHsv()[2] < 200) and (name != 'transparent'):
styleSheet = 'QLabel{background-color: ' + name + '; border:1px solid 0; font: 16px;color: white;}'
color_label.setStyleSheet(styleSheet)
layout.addWidget(color_label, index//line_count, index % line_count)
index += 1
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = DemoColor()
window.show()
sys.exit(app.exec())
The results are shown below:

Qt internal predefined color value
Previous Article:Battle PYQT5: 113-QSS + Custom Window Title Bar
QopengLwidget Introduction The QopengLwidget class is a component for rendering OpenGL graphics. QopengLWidget provides features for displaying OpenGL graphics integrated into the QT application. Usin...
Introduction to QGRAPHICSSCENE QGraphicsscene is an integral part of the graphical view frame. It provides a surface for managing a large number of 2D primitives (SURFACE). QGraphicsScene is used as a...
Another important interaction mode in the mouse GUI program, the mouse event in the QT includes the mouse click, release the mouse button, move the mouse to the specified area or leave the specific ar...
In a GUI desktop application, the interaction between users and programs is generally done by the keyboard and the mouse, and the processing with the keyboard and the mouse is a GUI program that is al...
Window events include changing window status, adjusting location, and size, window drawing, window shutdown events, etc. Change the window status The following method is used to change or determine th...
Create a custom event Although QT has built a large number of event classes to meet the majority of applications, it is sometimes we want to use custom events to achieve a purpose. The steps to implem...
The exchange data between applications can also use a clipboard in addition to the drag and drop technology. An application puts the data in the clipboard and another application proposes data from th...
Introduction to QPALETTE QPaletTTET (Palette, Palette) class is equivalent to a window or space palette, which manages color information of the control or window, each component (widget) contains a QP...
The QColor class provides colors based on RGB, HSV or CMYK values. Colors are usually specified with RGB (red, green, and blue) components, but can also be specified with HSV (hue, saturation, and val...
1.QColor class 2. Color debugger and others...