我有一个pyside2qt5.13.1应用程序,带有TableView
。根据表中的行数,我体验到一些奇怪的行为。我正在试着改变行的颜色。当行数为偶数时,剥离按预期工作。当行数为奇数时,我的TableView
会得到一个不期望的棋盘格模式。下面两个截图的唯一区别是模型中的行数。你知道吗
你知道吗主.py你知道吗
import sys
from PySide2.QtCore import QUrl
from PySide2.QtQml import QQmlApplicationEngine, qmlRegisterType
from PySide2.QtWidgets import QApplication
from table_model import TableModel
import qml_rc # noqa: F401
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
qmlRegisterType(TableModel, "TableModel", 1, 0, "TableModel")
engine.load(QUrl("qrc:/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
你知道吗主.qml你知道吗
import QtQuick 2.13
import QtQuick.Controls 2.13
import TableModel 1.0
ApplicationWindow {
visible: true
Component.onCompleted: {
showMaximized()
}
width: 640
height: 480
TableView {
id: tableView
clip: true
anchors.fill: parent
model: TableModel {}
topMargin: columnsHeader.implicitHeight
columnWidthProvider: function (column) {
return tableView.model ? tableView.width / tableView.model.columnCount() : 0
}
onWidthChanged: tableView.forceLayout()
delegate: Rectangle {
implicitWidth: tableView.columnWidthProvider(column)
implicitHeight: 40
color: index % 2 == 0 ? "grey" : "lightgrey"
Text {
text: display
}
}
Row {
id: columnsHeader
y: tableView.contentY
z: 2
Repeater {
model: tableView.columns > 0 ? tableView.columns : 1
Rectangle {
width: tableView.columnWidthProvider(modelData)
height: 60
clip: true
Label {
id: headerText
width: parent.width
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: tableView.model.headerData(modelData, Qt.Horizontal)
elide: Text.ElideRight
clip: true
}
}
}
}
}
}
表_型号.py你知道吗
from typing import Any, Optional
from PySide2.QtCore import QAbstractTableModel, QModelIndex, QObject, Qt
from table import Table
class TableModel(QAbstractTableModel):
def __init__(self, parent: QObject = None) -> None:
super().__init__(parent)
self._model_data = Table(
["This", "Is", "A", "Test", "Of", "Headers"],
[
["A", 1, 2, "B", "C", "D"],
["E", 3, 4, "F", "G", "H"],
["I", 5, 6, "J", "K", "L"],
["M", 7, 8, "N", "O", "P"],
["A", 1, 2, "B", "C", "D"],
["E", 3, 4, "F", "G", "H"],
["I", 5, 6, "J", "K", "L"],
["M", 7, 8, "N", "O", "P"],
["A", 1, 2, "B", "C", "D"],
["E", 3, 4, "F", "G", "H"],
["I", 5, 6, "J", "K", "L"],
# ["I", 5, 6, "J", "K", "L"],
])
def rowCount(self, parent=QModelIndex()) -> int:
return len(self._model_data.rows)
def columnCount(self, parent=QModelIndex()) -> int:
return len(self._model_data.headers)
def data(self, index: QModelIndex, role=Qt.DisplayRole) -> Optional[Any]:
if role != Qt.DisplayRole:
return None
if not self.checkIndex(index, QAbstractTableModel.CheckIndexOption.IndexIsValid):
return None
return self._model_data.rows[index.row()][index.column()]
def headerData(self, section: int, orientation, role) -> Optional[str]:
if role != Qt.DisplayRole:
return None
if section < 0 or section >= len(self._model_data.headers):
return None
return self._model_data.headers[section]
def reset_with_data(self, model_data: Table) -> None:
self.beginResetModel()
self._model_data = model_data
self.endResetModel()
你知道吗表.py你知道吗
from dataclasses import dataclass
from typing import Any, List
@dataclass
class Table:
headers: List[str]
rows: List[List[Any]]
你知道吗质量保证书你知道吗
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
跑步:
pipenv run pyside2-rcc -o qml_rc.py qml.qrc
pipenv run python main.py
index
属性不表示行号,而是:而是使用
row
(或model.row
)属性:相关问题 更多 >
编程相关推荐