teuno
teuno

Reputation: 61

Trying to create a QColor Enum

I have a tablewidget where I want to color certains cells based on the integer value that is in the cell. For this I want to create a enum with QColors.

from enum import Enum
from PyQt5.QtCore import *

class Color(Enum):
    Qt.white = 0
    Qt.black = 1
    Qt.red = 2
    Qt.blue = 3
    Qt.yellow = 4
    Qt.green = 5

when for example I write color.1 the cell should become black. The value will be read from the cells but atm I can not get this enum working. When I do:

item.setBackground(Qt.black)

it works the way I want so the problem is in this Color enum.

Does anyone know how to get this working?

Upvotes: 1

Views: 673

Answers (1)

cdonts
cdonts

Reputation: 9609

You should use a dictionary instead of an enum.

colors = {
    0: Qt.white,
    1: Qt.black,
    2: Qt.red,
    # ...
}

And if your item contains a number:

item.setBackground(colors[int(item.text())])

Hope it helps!

Upvotes: 1

Related Questions