gpt4 book ai didi

python - 带有文本和图标项的 QListWidget

转载 作者:行者123 更新时间:2023-12-01 07:59:52 24 4
gpt4 key购买 nike

我有一个带有一系列 QListWidget 的 GUI,第一个 QListWidget 应该有文本项和图标项,图标的标签位于侧面。其他 QListWidget 的项目应与第一个 QListWidget 的项目行对齐。我想知道这是否可能?

这是我现在拥有的:

enter image description here

这将是目标:

enter image description here

最佳答案

在本例中,我创建了一个树形模型,其中每个分支都是每个 QListView 应显示的内容。然后通过跟踪第一个 QListView 中项目大小的委托(delegate),通过 SizeHintRole 角色将相同的大小设置为模型中的其他相应项目,因此其他 QListView 将使用该信息:

from PySide2 import QtCore, QtGui, QtWidgets


class AlignDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom


class IconAlignDelegate(AlignDelegate):
def sizeHint(self, option, index):
s = super(IconAlignDelegate, self).sizeHint(option, index)
row_parent = index.parent().row()
r, model = index.row(), index.model()
for i in range(model.rowCount()):
if i != row_parent:
root_ix = model.index(i, 0)
child_ix = model.index(r, 0, root_ix)
model.setData(child_ix, s, QtCore.Qt.SizeHintRole)
return s


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
grid_layout = QtWidgets.QGridLayout(self)

datas = [
{"data": ["name1", "1", "1", "entity1", "path1"]},
{"data": ["name2", "2", "2", "entity2", "path2"]},
{"data": ["name3", "3", "3", "entity3", "path3"]},
{
"icon": "img2.png",
"data": ["name4", "4", "4", "entity4", "path4"],
},
{"data": ["name5", "5", "5", "entity5", "path5"]},
]

titles = ("Name", "C-ver", "L-ver", "Entity Name", "Path")

model = QtGui.QStandardItemModel(5, 1, self)
for i, title in enumerate(titles):
it = QtGui.QStandardItem(title)
model.setItem(i, 0, it)

for r, data in enumerate(datas):
path_icon = data.get("icon")
for c, text in enumerate(data["data"]):
it = QtGui.QStandardItem(text)
if c == 0 and path_icon is not None:
it.setIcon(QtGui.QIcon(path_icon))
parent_item = model.item(c, 0)
parent_item.setChild(r, 0, it)

for i in range(model.rowCount()):
lv = QtWidgets.QListView()
if i == 0:
lv.setIconSize(QtCore.QSize(64, 64))
delegate = IconAlignDelegate(lv)
else:
delegate = AlignDelegate(lv)
lv.setItemDelegate(delegate)
lv.setModel(model)
root_index = model.index(i, 0)
title = root_index.data()
lv.setRootIndex(root_index)
grid_layout.addWidget(QtWidgets.QLabel(title), 0, i)
grid_layout.addWidget(lv, 1, i)

# To understand better the structure of the model
# uncomment the following lines
# treeview = QtWidgets.QTreeView()
# treeview.setModel(model)
# treeview.expandAll()
# grid_layout.addWidget(treeview, 2, 0, 1, 5)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

enter image description here

前面的方法使用 SizeHintRole,因此它可能会建立项目的大小而不仅仅是高度,使用相同的策略最好建立一个仅存储高度的新角色:

from PySide2 import QtCore, QtGui, QtWidgets

HeightRole = QtCore.Qt.UserRole + 100


class AlignDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom


class IconAlignDelegate(AlignDelegate):
def sizeHint(self, option, index):
s = super(IconAlignDelegate, self).sizeHint(option, index)
row_parent = index.parent().row()
r, model = index.row(), index.model()
for i in range(model.rowCount()):
if i != row_parent:
root_ix = model.index(i, 0)
child_ix = model.index(r, 0, root_ix)
model.setData(child_ix, s.height(), HeightRole)
return s


class OtherDelegate(AlignDelegate):
def sizeHint(self, option, index):
s = super(OtherDelegate, self).sizeHint(option, index)
height = index.data(HeightRole)
if height is not None:
s.setHeight(height)
return s


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)

datas = [
# icon, name, c_ver, l_ver, entity_name, path
{"data": ["name1", "1", "1", "entity1", "path1"]},
{"data": ["name2", "2", "2", "entity2", "path2"]},
{"data": ["name3", "3", "3", "entity3", "path3"]},
{
"icon": "img2.png",
"data": ["name4", "4", "4", "entity4", "path4"],
},
{"data": ["name5", "5", "5", "entity5", "path5"]},
]

titles = ("Name", "C-ver", "L-ver", "Entity Name", "Path")

model = QtGui.QStandardItemModel(5, 1, self)
for i, title in enumerate(titles):
it = QtGui.QStandardItem(title)
model.setItem(i, 0, it)

for r, data in enumerate(datas):
path_icon = data.get("icon")
for c, text in enumerate(data["data"]):
it = QtGui.QStandardItem(text)
if c == 0 and path_icon is not None:
it.setIcon(QtGui.QIcon(path_icon))
parent_item = model.item(c, 0)
parent_item.setChild(r, 0, it)

splitter = QtWidgets.QSplitter()
lay.addWidget(splitter)

for i in range(model.rowCount()):
lv = QtWidgets.QListView()
if i == 0:
lv.setIconSize(QtCore.QSize(64, 64))
delegate = IconAlignDelegate(lv)
else:
delegate = OtherDelegate(lv)
lv.setItemDelegate(delegate)
lv.setModel(model)
root_index = model.index(i, 0)
title = root_index.data()
lv.setRootIndex(root_index)
w = QtWidgets.QWidget()
vlay = QtWidgets.QVBoxLayout(w)
vlay.addWidget(QtWidgets.QLabel(title))
vlay.addWidget(lv)
splitter.addWidget(w)
# To understand better the structure of the model
# uncomment the following lines
# treeview = QtWidgets.QTreeView()
# treeview.setModel(model)
# treeview.expandAll()
# lay.addWidget(treeview, 2, 0, 1, 5)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

关于python - 带有文本和图标项的 QListWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778894/

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