我在PySide2 Qt 5.13.0中创建了一个TableView
。 我希望TableView
可以填充父项的宽度,但是除非我拖动表格,否则它不会调整列的大小。 从程序开始时,我希望列变得更宽。
main.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_())
main.qml
import QtQuick 2.13
import QtQuick.Controls 2.13
import TableModel 1.0
ApplicationWindow {
visible: true
Component.onCompleted: {
showMaximized()
}
TableView {
id: tableView
clip: true
anchors.fill: parent
model: TableModel {}
topMargin: columnsHeader.implicitHeight
columnWidthProvider: function () { return tableView.width / tableView.model.columnCount(); }
delegate: Rectangle {
implicitWidth: tableView.columnWidthProvider()
implicitHeight: 40
Text {
text: display
}
}
Row {
id: columnsHeader
y: tableView.contentY
z: 2
Repeater {
model: tableView.columns > 0 ? tableView.columns : 1
Rectangle {
width: tableView.columnWidthProvider()
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
}
}
}
}
}
}
table_model.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"],
["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"],
["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"],
["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"],
["M", 7, 8, "N", "O", "P"],
])
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()
table.py
from dataclasses import dataclass
from typing import Any, List
@dataclass
class Table:
headers: List[str]
rows: List[List[Any]]
qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
运行:
pipenv run pyside2-rcc -o qml_rc.py qml.qrc
pipenv run python main.py