Python Forum
How to get the color name of qlistwidget?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the color name of qlistwidget?
#1
Hi,

I'm trying to get the color name of the current item's foreground of a qlistwidget.

To set it I used:
self.listbox_medium.insertItem(0, "paper")
self.listbox_medium.item(0).setForeground(QColor("blue"))
To get the color name I tried:

    def button_apply_selection_clicked(self):
        if self.listbox_medium.currentItem().foreground().color().colorNames() == QColor("blue"):
            print("okay")
        else:
            print("bad")
In the listbox (qlistwidget) I've got 2 items:
one with a blue foreground,
one with a green foreground.

But if I try to check for "blue" or "green", it is always returned "bad"...

I couldn't find the correct syntax for checking for the color names...

Could you please give me a hint?

Thanks a lot...
Reply
#2
You could comapare color objects.
Output:
if self.listbox_medium.currentItem().foreground().color() == QColor("blue"):
colornames() is not going to be useful. It returns a list of all colornames that Qt knows about. The list has nothing to do with any particular color.
Reply
#3
Maybe it's easier with hex

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QVBoxLayout, QListWidgetItem
from PyQt5.QtGui import QColor


class Ui_MainWindow(QWidget):
    def __init__(self, parent = None):
        super(Ui_MainWindow, self).__init__(parent)
        self.window = QWidget()
        self.listWidget = QListWidget()
        self.window.setWindowTitle("ListWidget")
        
        self.listWidget.currentItemChanged.connect(self.index_changed)

        listWidgetItem = QListWidgetItem("Hello World")
        listWidgetItem.setForeground(QColor("#0000ff")) # blue
        self.listWidget.addItem(listWidgetItem)
        
        listWidgetItem = QListWidgetItem("Hello World")
        listWidgetItem.setForeground(QColor("#ff0000"))
        self.listWidget.addItem(listWidgetItem)
        
        self.listWidget.setCurrentRow(1)

        window_layout = QVBoxLayout(self.window)
        window_layout.addWidget(self.listWidget)
        self.window.setLayout(window_layout)
        self.window.show()
            
    def index_changed(self, item):
        if item.foreground().color().name() == "#0000ff": # blue
            print("is blue")
        else:
            print("is not blue")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Ui_MainWindow()
    sys.exit(app.exec_())
Reply
#4
Doesn't matter if you use the color name, or RGB.
from PySide6.QtGui import QColor
print(QColor("blue") == QColor("blue"))
print(QColor("blue") == QColor("red"))
print(QColor("blue") == QColor("#0000ff"))
Output:
True False True
Either way in is a bad idea using color as a state.
Reply
#5
Yes, but item.foreground().color().name() returns hex color.
Reply
#6
Dear deanhystad,
dear Axel_Erfurt!!

Thanks a lot for your answers, it helped me much...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QListWidget, no item was selected flash77 4 990 Aug-02-2023, 09:31 AM
Last Post: Axel_Erfurt
  GUI Problem / call another function / fill QListwidget flash77 5 895 Jul-30-2023, 04:29 PM
Last Post: flash77
  [PyQt] populate a QListWidget devilonline 1 1,610 Apr-10-2023, 02:52 AM
Last Post: deanhystad
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 5,160 Nov-30-2020, 02:59 AM
Last Post: Alfalfa
  Why QListWidget doesn't show new line? AlekseyPython 3 3,383 Feb-05-2019, 02:23 PM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020