Add proper sorting to the qml TreeView
parent
485f3161f8
commit
fce3ebedca
|
@ -71,7 +71,7 @@ if (Qt5Charts_FOUND)
|
|||
endif()
|
||||
|
||||
set(Demo_SRCS src/main.cpp src/Fibonacci.cpp src/Tree.cpp src/TimeSeries.cpp
|
||||
src/Processes.cpp resource_file.qrc)
|
||||
src/Processes.cpp src/SortedModel.cpp resource_file.qrc)
|
||||
|
||||
add_executable(Demo ${Demo_SRCS})
|
||||
add_dependencies(Demo rust_target)
|
||||
|
|
|
@ -93,46 +93,65 @@ ApplicationWindow {
|
|||
var first = sortedFileSystem.index(0, 0, root);
|
||||
treeView.expand(first);
|
||||
}
|
||||
onSortIndicatorColumnChanged: sort()
|
||||
onSortIndicatorOrderChanged: sort()
|
||||
function sort() {
|
||||
var role = getColumn(treeView.sortIndicatorColumn).role;
|
||||
model.sortByRole(role, treeView.sortIndicatorOrder);
|
||||
}
|
||||
}
|
||||
}
|
||||
Tab {
|
||||
title: "processes"
|
||||
TreeView {
|
||||
id: processView
|
||||
model: processes
|
||||
sortIndicatorVisible: true
|
||||
alternatingRowColors: true
|
||||
TableViewColumn {
|
||||
title: "pid"
|
||||
role: "pid"
|
||||
}
|
||||
TableViewColumn {
|
||||
title: "name"
|
||||
role: "name"
|
||||
}
|
||||
TableViewColumn {
|
||||
title: "cpu"
|
||||
role: "cpu"
|
||||
}
|
||||
onSortIndicatorColumnChanged: {
|
||||
switch (processView.sortIndicatorColumn) {
|
||||
case 0: model.sortRole = Qt.DisplayRole; break;
|
||||
Item {
|
||||
anchors.fill: parent
|
||||
TextField {
|
||||
id: processFilterInput
|
||||
width: parent.width
|
||||
placeholderText: "Filter processes"
|
||||
onTextChanged: {
|
||||
processes.filterRegExp
|
||||
= new RegExp(processFilterInput.text);
|
||||
}
|
||||
}
|
||||
onSortIndicatorOrderChanged: {
|
||||
model.sort(Qt.DisplayRole, processView.sortIndicatorOrderChanged);
|
||||
}
|
||||
Component.onCompleted: {
|
||||
var r = processView.rootIndex;
|
||||
var a = processes.index(0, 0, r);
|
||||
var b = processes.index(1, 0, r);
|
||||
processView.expand(processView.rootIndex);
|
||||
processView.expand(a);
|
||||
processView.expand(b);
|
||||
processes.rowsInserted.connect(function (index) {
|
||||
TreeView {
|
||||
width: parent.width
|
||||
anchors.top: processFilterInput.bottom
|
||||
anchors.bottom: parent.bottom
|
||||
id: processView
|
||||
model: processes
|
||||
sortIndicatorVisible: true
|
||||
alternatingRowColors: true
|
||||
TableViewColumn {
|
||||
title: "pid"
|
||||
role: "pid"
|
||||
}
|
||||
TableViewColumn {
|
||||
title: "name"
|
||||
role: "name"
|
||||
}
|
||||
TableViewColumn {
|
||||
title: "cpu"
|
||||
role: "cpu"
|
||||
}
|
||||
onSortIndicatorColumnChanged: sort()
|
||||
onSortIndicatorOrderChanged: sort()
|
||||
function sort() {
|
||||
var role = getColumn(processView.sortIndicatorColumn).role;
|
||||
model.sortByRole(role, processView.sortIndicatorOrder);
|
||||
}
|
||||
Component.onCompleted: {
|
||||
var r = processView.rootIndex;
|
||||
var a = processes.index(0, 0, r);
|
||||
var b = processes.index(1, 0, r);
|
||||
processView.expand(processView.rootIndex);
|
||||
processView.expand(index);
|
||||
});
|
||||
processView.expand(a);
|
||||
processView.expand(b);
|
||||
processes.rowsInserted.connect(function (index) {
|
||||
processView.expand(processView.rootIndex);
|
||||
processView.expand(index);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
#include "SortedModel.moc"
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef SORTED_MODEL
|
||||
#define SORTED_MODEL
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QDebug>
|
||||
|
||||
class SortedModel : public QSortFilterProxyModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SortedModel() :QSortFilterProxyModel() {}
|
||||
public slots:
|
||||
void sortByRole(const QString& role, Qt::SortOrder order) {
|
||||
QHashIterator<int, QByteArray> i(roleNames());
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
if (i.value() == role) {
|
||||
setSortRole(i.key());
|
||||
}
|
||||
}
|
||||
sort(0, order);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -2,6 +2,7 @@
|
|||
#include "Fibonacci.h"
|
||||
#include "TimeSeries.h"
|
||||
#include "Processes.h"
|
||||
#include "SortedModel.h"
|
||||
|
||||
#ifdef QT_CHARTS_LIB
|
||||
#include <QtCharts>
|
||||
|
@ -43,9 +44,9 @@ struct Models {
|
|||
Fibonacci fibonacci;
|
||||
FibonacciList fibonacciList;
|
||||
Tree fileSystem;
|
||||
QSortFilterProxyModel sortedFileSystem;
|
||||
SortedModel sortedFileSystem;
|
||||
Processes processes;
|
||||
QSortFilterProxyModel sortedProcesses;
|
||||
SortedModel sortedProcesses;
|
||||
TimeSeries timeSeries;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue