下面的代码创建一个QListWidget,其中QListWidgetItem分配了一个拇指图像。
如果你能展示一下如何在QListWidgetItem周围做一个彩色边框,我将不胜感激
这是一张照片,展示了一个概念:

from PyQt4 import QtGui, QtCore
import sys, os

class MyClassItem(QtGui.QListWidgetItem):
    def __init__(self, parent=None):
        super(QtGui.QListWidgetItem, self).__init__(parent)


class ThumbListWidget(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(ThumbListWidget, self).__init__(parent)
        self.setIconSize(QtCore.QSize(64, 64))

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()
        self.listItems={}

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = ThumbListWidget(self)

        for i in range(7):
            listItemAInstance=MyClassItem()
            name='A'+'%04d'%i
            listItemAInstance.setText(name)
            listItemAInstance.setBackgroundColor(QtCore.Qt.darkGray)
            if i%2: listItemAInstance.setBackgroundColor(QtCore.Qt.gray)

            icon=self.getIcon(name)
            listItemAInstance.setIcon( icon )

            self.listWidgetA.addItem(listItemAInstance)

            listItemBInstance=MyClassItem()
            name='B'+'%04d'%i
            listItemBInstance.setText(name)
            icon=self.getIcon(name)
            listItemBInstance.setIcon( icon )

        myBoxLayout.addWidget(self.listWidgetA)

    def getIcon(self, name):
        thumbpath=os.path.expanduser("~")+'/thumbs/' +name+'/'+name+'.jpg'
        if not thumbpath: return
        if not os.path.exists(os.path.dirname(thumbpath)):
            os.makedirs(os.path.dirname(thumbpath))

        img = QtGui.QImage(64, 64, QtGui.QImage.Format_RGB32)
        img.fill(QtGui.QColor(96,96,96))

        painter = QtGui.QPainter(img)
        font = painter.font()
        font.setBold(True)
        font.setPointSize(18)

        painter.setPen(QtGui.QColor('black'))
        painter.setFont(font)
        painter.drawText(img.rect(), QtCore.Qt.AlignCenter, name)
        painter.end()
        img.save(thumbpath, 'JPG')

        icon = QtGui.QIcon( thumbpath )
        pixmap = icon.pixmap(64, 64)
        icon = QtGui.QIcon(pixmap)
        return icon

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(720,480)
    sys.exit(app.exec_())

最佳答案

QWidget派生的每个类都支持CSS样式。你能做的就是这样:

lwi = QListWidget()
lwi.setStyleSheet("QListWidget::item { border: 0px solid red }")

有关更多信息,请参见http://qt-project.org/doc/qt-4.8/qwidget.html#styleSheet-prop
您也可以设置整个应用程序的样式,但通过将其作为前缀,将样式的某个部分仅应用于QListWidgetItem
我发现C++例子:
app.setStyleSheet("QListWidget { background: red; } QListWidget::item { background: yellow; } QListWidget::item:selected { background: blue; }");

关于python - 如何在QListWidgetItem周围绘制边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24045554/

10-09 17:46