Results 1 to 2 of 2

Thread: Issue with proxySort

  1. #1
    Join Date
    Jun 2016
    Posts
    99
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Issue with proxySort

    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...

    Qt Code:
    1. //---The QModelIndex class is used to locate data in a data model | Overrides lessThan()---//
    2. bool UserEventModelProxy::lessThan(const QModelIndex &one, const QModelIndex &two) const {
    3.  
    4. //---Sort the ListView model based off the roleType---//
    5. if(sortRole() == UserEventLog::idRole){
    6. QVariant idOne = sourceModel()->data(one, UserEventLog::idRole);
    7. QVariant idTwo = sourceModel()->data(two, UserEventLog::idRole);
    8.  
    9. qDebug() << "made it inside else-if UserEvetnLog::idRole condition";
    10.  
    11. QString int_IdOne = idOne.toString();
    12. QString int_IdTwo = idTwo.toString();
    13.  
    14. if (int_IdOne != int_IdTwo) {
    15. qDebug() << "int_IdOne != int_IdTwo";
    16. return int_IdOne < int_IdTwo;
    17. }
    18. //---Second sort criteria---//
    19. else {
    20. qDebug() << "else int_IdOne != int_IdTwo";
    21. QVariant idOne = sourceModel()->data(one, UserEventLog::nameRole);
    22. QVariant idTwo = sourceModel()->data(two, UserEventLog::nameRole);
    23. return (idOne.toInt() < idTwo.toInt());
    24. }
    25. }
    26. else if(sortRole() == UserEventLog::nameRole){
    27. QVariant nameOne = sourceModel()->data(one, UserEventLog::nameRole);
    28. QVariant nameTwo = sourceModel()->data(two, UserEventLog::nameRole);
    29.  
    30. qDebug() << "made it inside else-if UserEvetnLog::nameRole condition";
    31.  
    32. QString str_NameOne = nameOne.toString();
    33. QString str_NameTwo = nameTwo.toString();
    34.  
    35. if (str_NameOne != str_NameTwo) {
    36. qDebug() << "str_NameOne != str_NameTwo";
    37. return str_NameOne < str_NameTwo;
    38. }
    39. else {
    40. qDebug() << "else str_NameOne != str_NameTwo";
    41. QVariant nameOne = sourceModel()->data(one, UserEventLog::dateRole);
    42. QVariant nameTwo = sourceModel()->data(two, UserEventLog::dateRole);
    43. return (nameOne.toString() < nameTwo.toString());
    44. }
    45. }
    46. else if(sortRole() == UserEventLog::msgRole){
    47. QVariant msgOne = sourceModel()->data(one, UserEventLog::msgRole);
    48. QVariant msgTwo = sourceModel()->data(two, UserEventLog::msgRole);
    49.  
    50. qDebug() << "made it inside else-if UserEvetnLog::msgRole condition";
    51.  
    52. QString str_msgOne = msgOne.toString();
    53. QString str_msgTwo = msgTwo.toString();
    54.  
    55. if (str_msgOne != str_msgTwo) {
    56. return str_msgOne < str_msgTwo;
    57. }
    58. else {
    59. QVariant msgOne = sourceModel()->data(one, UserEventLog::nameRole);
    60. QVariant msgTwo = sourceModel()->data(two, UserEventLog::nameRole);
    61. return (msgOne.toString() < msgTwo.toString());
    62. }
    63. }
    64. else if(sortRole() == UserEventLog::dateRole){
    65. QVariant dataOne = sourceModel()->data(one, UserEventLog::dateRole);
    66. QVariant dataTwo = sourceModel()->data(two, UserEventLog::dateRole);
    67.  
    68. qDebug() << "made it inside else-if UserEvetnLog::dateRole condition";
    69.  
    70. QString dateOne = dataOne.toString();
    71. QString dateTwo = dataTwo.toString();
    72. if (dateOne != dateTwo) {
    73. return dateOne < dateTwo;
    74. }
    75. else {
    76. QVariant dataOne = sourceModel()->data(one, UserEventLog::nameRole);
    77. QVariant dataTwo = sourceModel()->data(two, UserEventLog::nameRole);
    78. return (dataOne.toString() < dataTwo.toString());
    79. }
    80. }
    81. else{
    82. return QSortFilterProxyModel::lessThan(one, two);
    83. }
    84. }
    85.  
    86. //------Begin UserEventLog Class/Model-------//
    87. UserEventLog::UserEventLog(QObject *parent):QAbstractListModel(parent){
    88.  
    89. //---allocates dynamic memory on heap | Instance of proxyModel class---//
    90. m_UserEventModelProxy = new UserEventModelProxy(this);
    91. m_UserEventModelProxy->setSourceModel(this);
    92. m_UserEventModelProxy->setDynamicSortFilter(true);
    93. m_UserEventModelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
    94. //m_UserEventModelProxy->setSortRole(UserEventLog::idRole);
    95. //m_UserEventModelProxy->sort(0);
    96. }
    97.  
    98. //---ProxyModel Sort setRoleName---//
    99. void UserEventLog::setSortRole (int col)
    100. {
    101. switch(col){
    102. case 0:
    103. //---idRole---//
    104. qDebug() << "case 0";
    105. if(m_UserEventModelProxy->sortRole() == UserEventLog::idRole){
    106. qDebug() << "inside if sortRole() == UserEventLog::idRole";
    107. if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
    108. m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
    109. else
    110. m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
    111. }else{
    112. qDebug() << "inside else sortRole() == UserEventLog::idRole";
    113. m_UserEventModelProxy->setSortRole(UserEventLog::idRole);
    114. m_UserEventModelProxy->sort(0);
    115. }
    116. break;
    117. case 1:
    118. //---nameRole---//
    119. qDebug() << "case 1";
    120. if(m_UserEventModelProxy->sortRole() == UserEventLog::nameRole){
    121. if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
    122. m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
    123. else
    124. m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
    125. }else{
    126. m_UserEventModelProxy->setSortRole(UserEventLog::nameRole);
    127. m_UserEventModelProxy->sort(0);
    128. }
    129. break;
    130. case 2:
    131. //---msgRole---//
    132. qDebug() << "case 2";
    133. if(m_UserEventModelProxy->sortRole() == UserEventLog::msgRole){
    134. if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
    135. m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
    136. else
    137. m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
    138. }else{
    139. m_UserEventModelProxy->setSortRole(UserEventLog::msgRole);
    140. m_UserEventModelProxy->sort(0);
    141. }
    142. break;
    143. case 3:
    144. //---dateRole---//
    145. qDebug() << "case 3";
    146. if(m_UserEventModelProxy->sortRole() == UserEventLog::dateRole){
    147. if(m_UserEventModelProxy->sortOrder() == Qt::AscendingOrder)
    148. m_UserEventModelProxy->sort(0, Qt::DescendingOrder);
    149. else
    150. m_UserEventModelProxy->sort(0, Qt::AscendingOrder);
    151. }else{
    152. m_UserEventModelProxy->setSortRole(UserEventLog::dateRole);
    153. m_UserEventModelProxy->sort(0);
    154. }
    155. break;
    156. }
    157. }
    158.  
    159. QML mouseArea to call setSortRole()
    160. MouseArea{
    161. id: id_col_mouseArea
    162. anchors.fill: parent
    163. onClicked: {
    164. console.log("id_col_mouseArea clicked")
    165. UserEventLog.setSortRole(0);
    166. }
    167. }
    168.  
    169. qml model listView
    170. ListView {
    171. id: listView
    172. anchors.fill: parent
    173. //---Proxy Model for sorting---//
    174. model: UserEventModelProxy
    175. delegate: msgDelegate
    176. keyNavigationWraps: true
    177. KeyNavigation.tab: userNameDropDown
    178. orientation : "Vertical"
    179. snapMode: ListView.SnapToItem
    180. boundsBehavior: Flickable.StopAtBounds
    181. clip: true;
    182. highlightMoveVelocity: 2000
    183. highlight: Rectangle{
    184. radius: 7;
    185. color: "red"
    186. }
    To copy to clipboard, switch view to plain text mode 


    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 http://doc.qt.io/qt-4.8/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

    Qt Code:
    1. //---Sets rootContext |Function is called made in xmui.cpp---//
    2. void UserEventLog::setContext(QQmlContext* root)
    3. {
    4.  
    5. root->setContextProperty("UserEventLog", this);
    6. root->setContextProperty("UserEventModelProxy", m_UserEventModelProxy);
    7. }
    8.  
    9. //-------------Create instance of class | Set the context property to expose C++ to QML------------//
    10. mUserEventLogModel = new UserEventLog();
    11. mUserEventLogModel->setContext(m_QmlEngine->rootContext());
    12. //--------------------------------------------------------------//
    To copy to clipboard, switch view to plain text mode 
    Last edited by jfinn88; 5th October 2016 at 01:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with proxySort

    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,
    _

Similar Threads

  1. Qt 5.0.1 Issue
    By kiboi in forum Qt-based Software
    Replies: 0
    Last Post: 22nd March 2013, 06:22
  2. Qt 4.7.1 VNC issue?
    By djstava in forum Newbie
    Replies: 0
    Last Post: 14th March 2011, 04:34
  3. MS C++ issue
    By sepehr in forum Installation and Deployment
    Replies: 4
    Last Post: 30th December 2008, 00:45
  4. XML issue
    By jbpvr in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2008, 14:01
  5. ui_....h issue
    By stevey in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 05:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.