在一个使用 QML 作为前端、Python 作为后端的 Qt 6.8 项目中,我试图实现一个 TableModel/ListModel 过滤系统。我阅读了
QSortFilterProxyModel
的文档,但没有找到Python中的工作示例。
如何修改下面的简化代码,以通过应用程序的后端集成模型过滤方法并将其链接到我的搜索栏?
这是我当前的代码:
main.py:
import sys
from pathlib import Path
from PySide6.QtCore import QtMsgType, qInstallMessageHandler
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
def handle_qml_message(msg_type, context, message):
print(f"QML: {message}")
if __name__ == "__main__":
qInstallMessageHandler(handle_qml_message)
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
qml_file = Path(__file__).resolve().parent / "main.qml"
engine.load(qml_file)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
main.qml:
import QtQuick
import QtQuick.Window
import Qt.labs.qmlmodels
import QtQuick.Controls
Window {
width: 640
height: 480
visible: true
TextField {
id: textField
anchors {
top: parent.top
left: parent.left
}
width: 300
height: 30
placeholderText: "Search ..."
onTextChanged: print(text)
}
HorizontalHeaderView {
id: horizontalHeader
z: 2
syncView: tableView
anchors {
top: textField.bottom
left: parent.left
}
resizableColumns: false
model: ["Name", "Color"]
delegate: Rectangle {
implicitHeight: 50
implicitWidth: 50
color: "lightblue"
Text {
anchors.centerIn: parent
text: modelData
}
}
}
TableView {
id: tableView
anchors {
top: horizontalHeader.bottom
bottom: parent.bottom
left: parent.left
right: parent.right
}
columnSpacing: 1
rowSpacing: 1
clip: true
model: TableModel {
TableModelColumn { display: "name" }
TableModelColumn { display: "color" }
rows: [
{ "name": "cat", "color": "black" },
{ "name": "dog", "color": "brown" },
{ "name": "bird", "color": "white" },
{ "name": "fish", "color": "blue" },
{ "name": "rabbit", "color": "gray" },
{ "name": "horse", "color": "brown" },
{ "name": "snake", "color": "green" },
{ "name": "hamster", "color": "golden" },
{ "name": "turtle", "color": "green" },
{ "name": "parrot", "color": "red" },
{ "name": "frog", "color": "green" },
{ "name": "lizard", "color": "brown" },
{ "name": "butterfly", "color": "multicolor" },
{ "name": "bee", "color": "yellow" },
{ "name": "sheep", "color": "white" },
{ "name": "goat", "color": "black" },
{ "name": "pig", "color": "pink" },
{ "name": "cow", "color": "black and white" },
{ "name": "duck", "color": "yellow" },
{ "name": "swan", "color": "white" },
{ "name": "eagle", "color": "brown" },
{ "name": "owl", "color": "gray" },
{ "name": "peacock", "color": "blue and green" },
{ "name": "deer", "color": "brown" },
{ "name": "bear", "color": "dark brown" },
{ "name": "wolf", "color": "gray" },
{ "name": "fox", "color": "orange" },
{ "name": "lion", "color": "golden" },
{ "name": "tiger", "color": "orange with stripes" },
{ "name": "elephant", "color": "gray" }
]
}
delegate: Rectangle {
implicitWidth: 100
implicitHeight: 50
border.width: 1
Text {
text: display
anchors.centerIn: parent
}
}
}
}
这篇文章为我提供了一个如何使其工作的简单示例:stackoverflow.com/q/34252413/11603485 就像@JarMan提到的,不可能在QML文件中声明模型,它应该在python文件中实例化。感谢您的帮助!