Reputation: 17
I need to create an integration between Qt
and qml
, where from Qt
I ask for some info to a server, create a tablemodel
from Qt
and expose the info to qml
. I've reached this point, but now I've to sort the info. I'm using a QSortFilterProxyModel
, most of the examples I've found are created for Widgets, and create the tableview
from Qt
, but when I tried to create it from qml
, there's not an option "setSortingEnabled"
, in the qml
I've many objects no just the TableView
. I have used the order model->sort(1, Qt:AscendingOrder)
but there is not change in the qml
screen
SimMainWindow.cpp
#include "SimMainWindow.h"
#include "TurnDisplayTable.h"
#include "TurnDisplayItem.h"
#include <QtQml>
#include <QGraphicsObject>
#include <QDebug>
#include <QDeclarativeView>
#include <QQuickView>
#include <QQmlApplicationEngine>
#include <QtWidgets>
#include <QMainWindow>
#include <tablemodel.h>
#include <QSortFilterProxyModel>
SimMainWindow::SimMainWindow(int argc, char **argv): QGuiApplication(argc, argv), view()
{
qDebug() << "begin"; ///////////////////////////////
qmlRegisterType<TurnDisplayTable>("TurnDisplayTable", 1,0, "TurnDisplayTable");
filesModel= new TurnDisplayTable();
view.rootContext()->setContextProperty("idFilesModel", filesModel);
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(filesModel);
proxyModel->sort(1, Qt::AscendingOrder);
view.rootContext()->setContextProperty("pModel", proxyModel);
view.setSource( QUrl::fromLocalFile("./ui/main.qml") );
QObject::connect((QObject*)view.rootObject(), SIGNAL(refreshMsg()), this, SLOT(refreshHome()));
QObject::connect((QObject*)view.rootObject(), SIGNAL(changeMsg()), this, SLOT(changeHome()));
QObject::connect((QObject*)view.rootObject(), SIGNAL(sortMsg()), this, SLOT(sortHome()));
QObject::connect((QObject*)view.rootObject(), SIGNAL(infoMsg(QString, QString)), this, SLOT(infoHome(QString, QString)));
view.show();
qDebug() << "end"; ///////////////////////////////
}
SimMainWindow::~SimMainWindow()
{}
void SimMainWindow::refreshHome()
{
qDebug() << "refresh";
QList<TurnDisplayItem> list;
QString turn = "2";
QString date = "septiembre";
TurnDisplayItem item(turn, date);
list << item;
turn = "4";
date = "mayo";
TurnDisplayItem item2(turn, date);
list << item2;
turn = "1";
date = "julio";
TurnDisplayItem item3(turn, date);
list << item3;
turn = "3";
date = "abril";
TurnDisplayItem item4(turn, date);
list << item4;
turn = "2";
date = "mayo";
TurnDisplayItem item5(turn, date);
list << item5;
filesModel->setCurrencyMap(list);
proxyModel->setSourceModel(filesModel);
proxyModel->sort(1, Qt::AscendingOrder);
}
void SimMainWindow::changeHome()
{
qDebug() << "cambio";
QList<TurnDisplayItem> list;
QString turn = "3";
QString date = "sep";
TurnDisplayItem item(turn, date);
list << item;
turn = "1";
date = "may";
TurnDisplayItem item2(turn, date);
list << item2;
turn = "2";
date = "jul";
TurnDisplayItem item3(turn, date);
list << item3;
turn = "6";
date = "abr";
TurnDisplayItem item4(turn, date);
list << item4;
turn = "9";
date = "oct";
TurnDisplayItem item5(turn, date);
list << item5;
filesModel->setCurrencyMap(list);
}
void SimMainWindow::sortHome()
{
proxyModel->sort(0, Qt::AscendingOrder);
view.rootContext()->setContextProperty("pModel", proxyModel);
qDebug()<< "sort...";
view.show();
}
void SimMainWindow::infoHome(const QString &turnH, const QString &dateH)
{
qDebug() << "info";
QList<TurnDisplayItem> list;
QString turn = turnH;
QString date = dateH;
TurnDisplayItem item(turn, date);
list << item;
filesModel->setCurrencyMap(list);
}
Upvotes: 0
Views: 898
Reputation: 2149
Try this, it works in my case:
Model model;
QSortFilterProxyModel proxyModel;
proxyModel.setSortRole(Model::YOURCUSTOM_ROLE_HERE);
proxyModel.setSourceModel(&model);
proxyModel.setDynamicSortFilter(true);
proxyModel.sort(0);
I suspect your model is not sorting because you have to set the target role by setting setSortRole(int role)
and appling void QSortFilterProxyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder) [virtual]
.
I hope this solve your problem.
Upvotes: 1