gpt4 book ai didi

python - 如何永久设置 QListView 行高

转载 作者:行者123 更新时间:2023-11-28 17:38:14 25 4
gpt4 key购买 nike

不幸的是,QTableView.resizeRowsToContents() 仅设置当前填充 TableView 的那些行(或项目)的高度。使用第一个 model 的 reset() 或更新 QTableView 的行高切换回某个默认值(似乎大约为 32 px 高)。大多数情况下,此默认行高不必要地高,留下大量宝贵的屏幕空间未填充。如何永久应用对行高的更改?

最佳答案

我发现了两种永久设置行高的方法,您必须在模型中使用 SizeHintRole 或在 QStyledItemDelegate 中实现 sizeHint 方法。代码如下所示:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class ListModel(QAbstractListModel):
def __init__(self, data=[], parent=None):
QAbstractListModel.__init__(self, parent)
self.data = data

def rowCount(self, parent=None):
return len(self.data)

def data(self, index, role=None):
if role == Qt.DisplayRole:
row = index.row()
value = self.data[row]
return value

# ==============Comment if you're using delegate===============
if role == Qt.SizeHintRole:
return QSize(100, 75)
# =============================================================


class ListDelegate(QStyledItemDelegate):

def __init__(self, parent=None):
QStyledItemDelegate.__init__(self, parent)

def sizeHint(self, option, index):
return QSize(100, 20)


app = QApplication(sys.argv)
listview = QListView()
model = ListModel([12, 15, 19])
listview.setModel(model)

# =============uncomment to use delegate=====================
# delegate = ListDelegate()
# listview.setItemDelegate(delegate)
# ============================================================

listview.show()
sys.exit(app.exec_())

关于python - 如何永久设置 QListView 行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28227406/

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