gpt4 book ai didi

python - 如何在 QListWidgetItem 周围绘制边框

转载 作者:行者123 更新时间:2023-11-28 21:54:33 24 4
gpt4 key购买 nike

下面的代码创建了一个 QListWidget,并为 QListWidgetItem 分配了缩略图。

enter image description here

如果您展示如何在 QListWidgetItem 周围制作彩色边框,我将不胜感激

这是显示概念的 Photoshop 图像:

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

最佳答案

派生自 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com