//---The QModelIndex class is used to locate data in a data model | Overrides lessThan()---//
//---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{
}
}
//------Begin UserEventLog Class/Model-------//
//---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"
}
Bookmarks