PDA

View Full Version : Issue with proxySort



jfinn88
5th October 2016, 00:00
I created a proxyModel class

I implemented my own custom lessThan() function in my proxyModel class

I created a rootContext Item to expose proxy model to c++ set the listView model to the proxy model

In the constructor of my UserEvetnLog class I create a instance of the proxyModel class in my userEventLog class
I set the sourceModel of the proxySortFilter to the UserEventLog model
I set the dynamicSortFilter
I set the caseSensitivity to insensitive

I created a function for setting the sortRole in my UserEventLog class

I use Q_INVOKABLE and the UserEventLog rootContext item to call the setSortRole() fucntion in QML
inside the setSortRole() function I check what roleName is being set to sort the data accordingly I call sort() which calls the virtual function lessThan()

with debug I can see I make it inside my setSortRole() function then form there I can see it enter the lessThan() with debugs however nothing displays in the model listView

nothing loads to the listView model at first either when dialog is loaded

When the model first loads I don’t want it sorted only sort onClicked function call in qml...



//---The QModelIndex class is used to locate data in a data model | Overrides lessThan()---//
bool UserEventModelProxy::lessThan(const QModelIndex &one, const QModelIndex &two) const {

//---Sort the ListView model based off the roleType---//
if(sortRole() == UserEventLog::idRole){
QVariant idOne = sourceModel()->data(one, UserEventLog::idRole);
QVariant idTwo = sourceModel()->data(two, UserEventLog::idRole);

qDebug() << "made it inside else-if UserEvetnLog::idRole condition";

QString int_IdOne = idOne.toString();
QString int_IdTwo = idTwo.toString();

if (int_IdOne != int_IdTwo) {
qDebug() << "int_IdOne != int_IdTwo";
return int_IdOne < int_IdTwo;
}
//---Second sort criteria---//
else {
qDebug() << "else int_IdOne != int_IdTwo";
QVariant idOne = sourceModel()->data(one, UserEventLog::nameRole);
QVariant idTwo = sourceModel()->data(two, UserEventLog::nameRole);
return (idOne.toInt() < idTwo.toInt());
}
}
else if(sortRole() == UserEventLog::nameRole){
QVariant nameOne = sourceModel()->data(one, UserEventLog::nameRole);
QVariant nameTwo = sourceModel()->data(two, UserEventLog::nameRole);

qDebug() << "made it inside else-if UserEvetnLog::nameRole condition";

QString str_NameOne = nameOne.toString();
QString str_NameTwo = nameTwo.toString();

if (str_NameOne != str_NameTwo) {
qDebug() << "str_NameOne != str_NameTwo";
return str_NameOne < str_NameTwo;
}
else {
qDebug() << "else str_NameOne != str_NameTwo";
QVariant nameOne = sourceModel()->data(one, UserEventLog::dateRole);
QVariant nameTwo = sourceModel()->data(two, UserEventLog::dateRole);
return (nameOne.toString() < nameTwo.toString());
}
}
else if(sortRole() == UserEventLog::msgRole){
QVariant msgOne = sourceModel()->data(one, UserEventLog::msgRole);
QVariant msgTwo = sourceModel()->data(two, UserEventLog::msgRole);

qDebug() << "made it inside else-if UserEvetnLog::msgRole condition";

QString str_msgOne = msgOne.toString();
QString str_msgTwo = msgTwo.toString();

if (str_msgOne != str_msgTwo) {
return str_msgOne < str_msgTwo;
}
else {
QVariant msgOne = sourceModel()->data(one, UserEventLog::nameRole);
QVariant msgTwo = sourceModel()->data(two, UserEventLog::nameRole);
return (msgOne.toString() < msgTwo.toString());
}
}
else if(sortRole() == UserEventLog::dateRole){
QVariant dataOne = sourceModel()->data(one, UserEventLog::dateRole);
QVariant dataTwo = sourceModel()->data(two, UserEventLog::dateRole);

qDebug() << "made it inside else-if UserEvetnLog::dateRole condition";

QString dateOne = dataOne.toString();
QString dateTwo = dataTwo.toString();
if (dateOne != dateTwo) {
return dateOne < dateTwo;
}
else {
QVariant dataOne = sourceModel()->data(one, UserEventLog::nameRole);
QVariant dataTwo = sourceModel()->data(two, UserEventLog::nameRole);
return (dataOne.toString() < dataTwo.toString());
}
}
else{
return QSortFilterProxyModel::lessThan(one, two);
}
}

//------Begin UserEventLog Class/Model-------//
UserEventLog::UserEventLog(QObject *parent):QAbstractListModel(parent){

//---allocates dynamic memory on heap | Instance of proxyModel class---//
m_UserEventModelProxy = new UserEventModelProxy(this);
m_UserEventModelProxy->setSourceModel(this);
m_UserEventModelProxy->setDynamicSortFilter(true);
m_UserEventModelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
//m_UserEventModelProxy->setSortRole(UserEventLog::idRole);
//m_UserEventModelProxy->sort(0);
}

//---ProxyModel Sort setRoleName---//
void UserEventLog::setSortRole (int col)
{
switch(col){
case 0:
//---idRole---//
qDebug() << "case 0";
if(m_UserEventModelProxy->sortRole() == UserEventLog::idRole){
qDebug() << "inside if sortRole() == UserEventLog::idRole";
if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
else
m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
}else{
qDebug() << "inside else sortRole() == UserEventLog::idRole";
m_UserEventModelProxy->setSortRole(UserEventLog::idRole);
m_UserEventModelProxy->sort(0);
}
break;
case 1:
//---nameRole---//
qDebug() << "case 1";
if(m_UserEventModelProxy->sortRole() == UserEventLog::nameRole){
if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
else
m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
}else{
m_UserEventModelProxy->setSortRole(UserEventLog::nameRole);
m_UserEventModelProxy->sort(0);
}
break;
case 2:
//---msgRole---//
qDebug() << "case 2";
if(m_UserEventModelProxy->sortRole() == UserEventLog::msgRole){
if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
else
m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
}else{
m_UserEventModelProxy->setSortRole(UserEventLog::msgRole);
m_UserEventModelProxy->sort(0);
}
break;
case 3:
//---dateRole---//
qDebug() << "case 3";
if(m_UserEventModelProxy->sortRole() == UserEventLog::dateRole){
if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
else
m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
}else{
m_UserEventModelProxy->setSortRole(UserEventLog::dateRole);
m_UserEventModelProxy->sort(0);
}
break;
}
}

QML mouseArea to call setSortRole()
MouseArea{
id: id_col_mouseArea
anchors.fill: parent
onClicked: {
console.log("id_col_mouseArea clicked")
UserEventLog.setSortRole(0);
}
}

qml model listView
ListView {
id: listView
anchors.fill: parent
//---Proxy Model for sorting---//
model: UserEventModelProxy
delegate: msgDelegate
keyNavigationWraps: true
KeyNavigation.tab: userNameDropDown
orientation : "Vertical"
snapMode: ListView.SnapToItem
boundsBehavior: Flickable.StopAtBounds
clip: true;
highlightMoveVelocity: 2000
highlight: Rectangle{
radius: 7;
color: "red"
}


Added after 1 49 minutes:

update: I may need to override the proxySortFilter setFilterRole() method not sure tho...

update: okay no need to override setFilterRole() is use Qt::DisplayRole as defualt (which I set to my idRole to)
I was messing thing up when creating setting rootContext corrected issue now everything displays fine and sorts, need to fix up my lessThan() but it seems to be working now after fixing context issues.

I added this function to my class and removed my other context obj and just make a call to my context function from there



//---Sets rootContext |Function is called made in xmui.cpp---//
void UserEventLog::setContext(QQmlContext* root)
{

root->setContextProperty("UserEventLog", this);
root->setContextProperty("UserEventModelProxy", m_UserEventModelProxy);
}

//-------------Create instance of class | Set the context property to expose C++ to QML------------//
mUserEventLogModel = new UserEventLog();
mUserEventLogModel->setContext(m_QmlEngine->rootContext());
//--------------------------------------------------------------//

anda_skoa
6th October 2016, 17:23
You needed that extra method because of the weird setup of creating the proxy inside the model.

Usually the a model is not involved in any way with the proxy, after all that is the whole point of having one.

Cheers,
_