Last active
November 4, 2021 00:09
-
-
Save eyllanesc/4f47e4f59100340b8328438a39011b31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtCore import pyqtSlot, QSortFilterProxyModel, Qt, QUrl, QDir, QAbstractListModel | |
from PyQt5.QtGui import QGuiApplication | |
from PyQt5.QtQml import QQmlApplicationEngine | |
class SortProxyModel(QSortFilterProxyModel): | |
@pyqtSlot(str, Qt.SortOrder) | |
def sortData(self, roleName, order): | |
if order == Qt.InitialSortOrderRole: | |
self.setSortRole(order) | |
self.invalidate() | |
else: | |
roles = [key for key, value in self.roleNames().items() if value == roleName.encode()] | |
if len(roles) > 0: | |
self.setSortRole(roles[0]) | |
self.sort(0, order) | |
class PersonModel(QAbstractListModel): | |
Name, value1, value2, value3, value4 = range(Qt.UserRole + 1, Qt.UserRole + 6) | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
self.persons = [ | |
{'name': 'item1', 'value1': 10, 'value2': 17, 'value3': 16, 'value4': 10}, | |
{'name': 'item3', 'value1': 11, 'value2': 13, 'value3': 17, 'value4': 16}, | |
{'name': 'item2', 'value1': 12, 'value2': 15, 'value3': 12, 'value4': 12}, | |
] | |
def data(self, index, role=Qt.DisplayRole): | |
if index.isValid(): | |
row = index.row() | |
if role in self.roleNames().keys(): | |
key = self.roleNames()[role] | |
return self.persons[row][key.decode()] | |
def rowCount(self, parent=None): | |
return len(self.persons) | |
def roleNames(self): | |
return {PersonModel.Name: b'name', PersonModel.value1: b'value1', PersonModel.value2: b'value2', | |
PersonModel.value3: b'value3', PersonModel.value4: b'value4'} | |
if __name__ == '__main__': | |
myApp = QGuiApplication(sys.argv) | |
engine = QQmlApplicationEngine() | |
model = PersonModel() | |
proxyModel = SortProxyModel() | |
proxyModel.setSourceModel(model) | |
engine.rootContext().setContextProperty('mymodel', proxyModel) | |
engine.load(QUrl.fromLocalFile(QDir.current().absoluteFilePath('main.qml'))) | |
if len(engine.rootObjects()) == 0: | |
sys.exit(-1) | |
sys.exit(myApp.exec_()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.9 | |
import QtQuick.Controls 2.2 | |
ApplicationWindow { | |
visible: true | |
width: 640 | |
height: 480 | |
property alias combo: comboBoxRole | |
property alias combo2: comboBoxOrder | |
function sort(){ | |
mymodel.sortData(combo.currentText, cbItems.get(combo2.currentIndex).order) | |
} | |
ScrollView { | |
id: scrollView | |
anchors.fill: parent | |
Row{ | |
height: 40 | |
anchors.left: parent.left | |
anchors.right: parent.right | |
spacing: 100 | |
id: row | |
ComboBox { | |
id: comboBoxRole | |
width: 150 | |
model: [ "name", "value1", "value2", "value3", "value4"] | |
onCurrentTextChanged: sort() | |
} | |
ComboBox { | |
id: comboBoxOrder | |
width: 150 | |
textRole: "text" | |
model: ListModel{ | |
id: cbItems | |
ListElement{text: "Reset"; order : Qt.InitialSortOrderRole} | |
ListElement{ text: "Ascendent"; order: Qt.AscendingOrder} | |
ListElement{ text: "Descendent"; order: Qt.DescendingOrder} | |
} | |
onCurrentTextChanged: sort() | |
} | |
} | |
ListView { | |
id:lv | |
anchors.top: row.bottom | |
anchors.bottom: parent.bottom | |
anchors.left: parent.left | |
anchors.right: parent.right | |
model: mymodel | |
delegate: Row { | |
spacing: 10 | |
height: 40 | |
Text { text: 'Name: ' + name } | |
Text { text: 'value1: ' + value1 } | |
Text { text: 'value2: ' + value2 } | |
Text { text: 'value3: ' + value3 } | |
Text { text: 'value4: ' + value4 } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment