如何在QListWidgetItem周围绘制边框

时间:2014-06-04 19:04:15

标签: python qt pyqt

下面的代码创建了一个QListWidget,其中QListWidgetItem分配了一个拇指图像。

enter image description here

如果您展示如何围绕QListWidgetItem

制作颜色边框,我将不胜感激

以下是展示概念的Photoshoped图片:

enter image description here

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_())

1 个答案:

答案 0 :(得分:5)

每个派生自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; }");