mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2024-11-22 19:00:07 +08:00
fix bug #439
This commit is contained in:
parent
e2d52f55b9
commit
6a73ac97df
60
src/FluTableSortProxyModel.cpp
Normal file
60
src/FluTableSortProxyModel.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "FluTableSortProxyModel.h"
|
||||
|
||||
#include <QJSValueList>
|
||||
|
||||
FluTableSortProxyModel::FluTableSortProxyModel(QSortFilterProxyModel *parent)
|
||||
: QSortFilterProxyModel {parent}
|
||||
{
|
||||
_model = nullptr;
|
||||
connect(this,&FluTableSortProxyModel::modelChanged,this,[=]{
|
||||
setSourceModel(this->model());
|
||||
sort(0,Qt::AscendingOrder);
|
||||
});
|
||||
}
|
||||
|
||||
bool FluTableSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FluTableSortProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FluTableSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const{
|
||||
QJSValue comparator = _comparator;
|
||||
if(comparator.isNull()){
|
||||
return true;
|
||||
}
|
||||
QJSValueList data;
|
||||
data<<source_left.row();
|
||||
data<<source_right.row();
|
||||
bool flag = comparator.call(data).toBool();
|
||||
if(sortOrder()==Qt::AscendingOrder){
|
||||
return !flag;
|
||||
}else{
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
||||
void FluTableSortProxyModel::setSortComparator(QJSValue comparator){
|
||||
this->_comparator = comparator;
|
||||
if(sortOrder()==Qt::AscendingOrder){
|
||||
sort(0,Qt::DescendingOrder);
|
||||
}else{
|
||||
sort(0,Qt::AscendingOrder);
|
||||
}
|
||||
}
|
||||
QVariant FluTableSortProxyModel::getRow(int rowIndex){
|
||||
QVariant result;
|
||||
QMetaObject::invokeMethod(_model, "getRow",Q_RETURN_ARG(QVariant, result),Q_ARG(int, mapToSource(index(rowIndex,0)).row()));
|
||||
return result;
|
||||
}
|
||||
|
||||
void FluTableSortProxyModel::setRow(int rowIndex,QVariant val){
|
||||
QMetaObject::invokeMethod(_model, "setRow",Q_ARG(int, mapToSource(index(rowIndex,0)).row()),Q_ARG(QVariant,val));
|
||||
}
|
||||
|
||||
void FluTableSortProxyModel::removeRow(int rowIndex,int rows){
|
||||
QMetaObject::invokeMethod(_model, "removeRow",Q_ARG(int, mapToSource(index(rowIndex,0)).row()),Q_ARG(int,rows));
|
||||
}
|
||||
|
28
src/FluTableSortProxyModel.h
Normal file
28
src/FluTableSortProxyModel.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef FLUTABLESORTPROXYMODEL_H
|
||||
#define FLUTABLESORTPROXYMODEL_H
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QAbstractTableModel>
|
||||
#include <QtQml/qqml.h>
|
||||
#include <QJSValue>
|
||||
#include "stdafx.h"
|
||||
|
||||
class FluTableSortProxyModel : public QSortFilterProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY_AUTO(QAbstractTableModel*,model)
|
||||
QML_NAMED_ELEMENT(FluTableSortProxyModel)
|
||||
public:
|
||||
explicit FluTableSortProxyModel(QSortFilterProxyModel *parent = nullptr);
|
||||
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
||||
bool filterAcceptsColumn(int sourceColumn, const QModelIndex &sourceParent) const override;
|
||||
bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override;
|
||||
Q_INVOKABLE void setSortComparator(QJSValue comparator);
|
||||
Q_INVOKABLE QVariant getRow(int rowIndex);
|
||||
Q_INVOKABLE void setRow(int rowIndex,QVariant val);
|
||||
Q_INVOKABLE void removeRow(int rowIndex,int rows);
|
||||
private:
|
||||
QJSValue _comparator;
|
||||
};
|
||||
|
||||
#endif // FLUTABLESORTPROXYMODEL_H
|
Loading…
Reference in New Issue
Block a user