【问题标题】:Python - How to get backgroundColor from QListWidgetItemPython - 如何从 QListWidgetItem 获取 backgroundColor
【发布时间】:2016-12-16 22:03:08
【问题描述】:

我想从 QList Widget 中获取项目并将其添加到 QListWidgetItem var 中,一切都很好,但之后我需要这个项目的背景颜色和:

    item = self.Listbox.takeItem(i)
    print(item.backgroundColor)

为此,我得到了 backgroundColor 属性不存在

的错误

在 PyQt4 中存在该属性:

http://pyqt.sourceforge.net/Docs/PyQt4/qlistwidgetitem.html#backgroundColor

但是我可以在 PyQt5 上使用什么?

-

print(dir(item)) 的输出:

【问题讨论】:

  • print(dir(item)) 的结果是什么?

标签: python python-3.x pyqt pyqt5


【解决方案1】:

简短的回答是

item.background().color().getRgb()

获取 RGB 值,例如(0, 0, 0, 255)

更长的答案是 backgroundColor() 现在已过时,请参阅http://doc.qt.io/qt-4.8/qlistwidgetitem-obsolete.html

(过时的)QColor backgroundColor() const

所以你必须使用 background() 获取 QBrush,然后使用 color() 获取 QColor,然后获取 RGB 或任何你想要看到的东西 http://doc.qt.io/qt-4.8/qcolor.html

【讨论】:

  • 是的,它似乎很好:) 但我现在如何比较这些颜色?类似if Item.background().color().getRgb() == QColor(255,0,0,255):
  • 你可以使用数字的 item.background().color().value()
  • 我不能例如清除蓝色和清除红色都有来自 .value() 方法的 255 个数字
  • 正如你所说的绿色和红色不同,但红色和蓝色都有 255 :-O
  • 如果你使用 value(),红色给出 255 绿色给出 128
【解决方案2】:
#Get backgroundColor from QListWidgetItem
#This is the example code for Get backgroundColor from QListWidgetItem
#If your are not expecting this answer, sorry.
#Thanks, Subin Gopi

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        self.listWidget = QtGui.QListWidget(self)
        self.listWidget.setGeometry(QtCore.QRect(10, 10, 200, 150))
        self.listWidget.setObjectName ('listWidget')        
        self.resize(250, 200)

        self.listWidget.itemPressed.connect (self.getColor)        
        self.addItems ()


    def addItems (self) :
        itemList        = {'Apple':'red', 'Orange':'#ff5500', 'Grape':'blue', 'Bean':'green', 'Banana':'yellow'}
        for eachItem in itemList :
            currentItem = QtGui.QListWidgetItem (self.listWidget)           

            #Set Color
            bgBrush     = QtGui.QBrush()
            bgBrush.setStyle(QtCore.Qt.SolidPattern)

            #Qt.GlobalColor
            bgBrush.setColor (QtGui.QColor(itemList[eachItem]))            
            #RGB Value 
            #bgBrush.setColor (QtGui.QColor(170, 85, 255))            
            currentItem.setBackground(bgBrush)

            fgBrush     = QtGui.QBrush()
            fgBrush.setColor (QtGui.QColor('black'))            
            currentItem.setForeground (fgBrush)

            #Set text
            currentItem.setText(eachItem)

    def getColor (self) :
        currentItem     = self.listWidget.selectedItems ()
        currentBrush    = currentItem[-1].background ()
        rgbColor        = currentBrush.color ().red (), currentBrush.color ().green (), currentBrush.color ().blue ()
        strColor        = currentBrush.color ().name ()

        print ('rgb- Color\t', rgbColor)
        print ('str- Color\t', strColor, '\n')

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    app.exec_()  

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2013-05-22
    • 2019-05-11
    相关资源
    最近更新 更多