Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created January 15, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/deabfe030b8daca97e8de994a71ea094 to your computer and use it in GitHub Desktop.
Save eyllanesc/deabfe030b8daca97e8de994a71ea094 to your computer and use it in GitHub Desktop.
48257099
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, int)
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_())
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: row2.top
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 }
}
}
Row{
id: row2
height: 40
anchors.bottom: parent.bottom
spacing: 100
ComboBox {
id: comboBoxRole2
width: 150
model: [ "name", "value1", "value2", "value3", "value4"]
}
ComboBox {
id: number
width: 150
model: mymodel.rowCount()
}
Label{
id: output
text: mymodel.data(mymodel.index(number.currentIndex, 0), Qt.UserRole+1 + comboBoxRole2.currentIndex)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment