Add proper sorting to the qml TreeView
parent
485f3161f8
commit
fce3ebedca
|
@ -71,7 +71,7 @@ if (Qt5Charts_FOUND)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(Demo_SRCS src/main.cpp src/Fibonacci.cpp src/Tree.cpp src/TimeSeries.cpp
|
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_executable(Demo ${Demo_SRCS})
|
||||||
add_dependencies(Demo rust_target)
|
add_dependencies(Demo rust_target)
|
||||||
|
|
|
@ -93,46 +93,65 @@ ApplicationWindow {
|
||||||
var first = sortedFileSystem.index(0, 0, root);
|
var first = sortedFileSystem.index(0, 0, root);
|
||||||
treeView.expand(first);
|
treeView.expand(first);
|
||||||
}
|
}
|
||||||
|
onSortIndicatorColumnChanged: sort()
|
||||||
|
onSortIndicatorOrderChanged: sort()
|
||||||
|
function sort() {
|
||||||
|
var role = getColumn(treeView.sortIndicatorColumn).role;
|
||||||
|
model.sortByRole(role, treeView.sortIndicatorOrder);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tab {
|
Tab {
|
||||||
title: "processes"
|
title: "processes"
|
||||||
TreeView {
|
Item {
|
||||||
id: processView
|
anchors.fill: parent
|
||||||
model: processes
|
TextField {
|
||||||
sortIndicatorVisible: true
|
id: processFilterInput
|
||||||
alternatingRowColors: true
|
width: parent.width
|
||||||
TableViewColumn {
|
placeholderText: "Filter processes"
|
||||||
title: "pid"
|
onTextChanged: {
|
||||||
role: "pid"
|
processes.filterRegExp
|
||||||
}
|
= new RegExp(processFilterInput.text);
|
||||||
TableViewColumn {
|
|
||||||
title: "name"
|
|
||||||
role: "name"
|
|
||||||
}
|
|
||||||
TableViewColumn {
|
|
||||||
title: "cpu"
|
|
||||||
role: "cpu"
|
|
||||||
}
|
|
||||||
onSortIndicatorColumnChanged: {
|
|
||||||
switch (processView.sortIndicatorColumn) {
|
|
||||||
case 0: model.sortRole = Qt.DisplayRole; break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onSortIndicatorOrderChanged: {
|
TreeView {
|
||||||
model.sort(Qt.DisplayRole, processView.sortIndicatorOrderChanged);
|
width: parent.width
|
||||||
}
|
anchors.top: processFilterInput.bottom
|
||||||
Component.onCompleted: {
|
anchors.bottom: parent.bottom
|
||||||
var r = processView.rootIndex;
|
id: processView
|
||||||
var a = processes.index(0, 0, r);
|
model: processes
|
||||||
var b = processes.index(1, 0, r);
|
sortIndicatorVisible: true
|
||||||
processView.expand(processView.rootIndex);
|
alternatingRowColors: true
|
||||||
processView.expand(a);
|
TableViewColumn {
|
||||||
processView.expand(b);
|
title: "pid"
|
||||||
processes.rowsInserted.connect(function (index) {
|
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(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 "Fibonacci.h"
|
||||||
#include "TimeSeries.h"
|
#include "TimeSeries.h"
|
||||||
#include "Processes.h"
|
#include "Processes.h"
|
||||||
|
#include "SortedModel.h"
|
||||||
|
|
||||||
#ifdef QT_CHARTS_LIB
|
#ifdef QT_CHARTS_LIB
|
||||||
#include <QtCharts>
|
#include <QtCharts>
|
||||||
|
@ -43,9 +44,9 @@ struct Models {
|
||||||
Fibonacci fibonacci;
|
Fibonacci fibonacci;
|
||||||
FibonacciList fibonacciList;
|
FibonacciList fibonacciList;
|
||||||
Tree fileSystem;
|
Tree fileSystem;
|
||||||
QSortFilterProxyModel sortedFileSystem;
|
SortedModel sortedFileSystem;
|
||||||
Processes processes;
|
Processes processes;
|
||||||
QSortFilterProxyModel sortedProcesses;
|
SortedModel sortedProcesses;
|
||||||
TimeSeries timeSeries;
|
TimeSeries timeSeries;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue