Results 1 to 7 of 7

Thread: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

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

    Default Re: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    here is my model definition I would like to implement a QSortFilterProxyModel
    Qt Code:
    1. //------Begin UserEventLog Class/Model-------//
    2. UserEventLog::UserEventLog(QObject *parent):QAbstractListModel(parent){
    3.  
    4. }
    5.  
    6. UserEventLog::~UserEventLog(){
    7.  
    8. }
    9.  
    10. int UserEventLog::rowCount(const QModelIndex &parent) const{
    11. Q_UNUSED(parent);
    12. return m_userEventList.count();
    13. }
    14.  
    15. QHash<int, QByteArray> UserEventLog::roleNames() const{
    16. QHash<int, QByteArray> roleNames;
    17. roleNames.insert(idRole, "id");
    18. roleNames.insert(nameRole, "userName");
    19. roleNames.insert(msgRole, "eventMessage");
    20. roleNames.insert(dateRole, "dateTime");
    21. return roleNames;
    22. }
    23.  
    24. QVariant UserEventLog::data(const QModelIndex &index, int role) const{
    25. if (index.row() < 0 || index.row() >= m_userEventList.count()){
    26. return QVariant();
    27. }
    28.  
    29. QVariant text;
    30.  
    31. if(role == idRole){
    32. userEventLogMsg msg = m_userEventList.at(index.row());
    33. text = msg.id;
    34. }
    35. else if(role == nameRole){
    36. userEventLogMsg msg = m_userEventList.at(index.row());
    37. text = msg.username;
    38. }
    39. else if(role == msgRole){
    40. userEventLogMsg msg = m_userEventList.at(index.row());
    41. text = msg.eventmessage;
    42. }
    43. if(role == dateRole){
    44. userEventLogMsg msg = m_userEventList.at(index.row());
    45. text = msg.datetime;
    46. }
    47. return text;
    48. }
    49.  
    50. void UserEventLog::addEvent(const userEventLogMsg &msg){
    51. beginInsertRows(QModelIndex(), 0, 0);
    52. m_userEventList.insert(0, msg);
    53. endInsertRows();
    54. }
    55.  
    56. void UserEventLog::init(){
    57. //emit showBusy(true);
    58. dbConnect();
    59. archiveEvent();
    60. selectEvent();
    61. //emit showBusy(false);
    62. }
    63.  
    64. bool UserEventLog::dbConnect(){
    65. //---check if database is connected---//
    66. if(!m_selectDataBase.isValid()){
    67. qDebug() << "error in connecting to DB";
    68. m_selectDataBase = QSqlDatabase::addDatabase("QSQLITE", "conn2");
    69. m_selectDataBase.setDatabaseName(Paths::root() + "/userLog.db");
    70.  
    71. qDebug() << "database connect path: "+Paths::root()+"/userLog.db";
    72. m_selectDataBase.open();
    73. }
    74. else{
    75. qDebug() <<"connected to DB" ;
    76. m_selectDataBase.open();
    77. }
    78. return m_selectDataBase.isValid();
    79. }
    80. bool UserEventLog::selectEvent(){
    81. dbConnect();
    82. QDate beginDate = QDate::currentDate();
    83. QDate endDate = QDate::currentDate();
    84.  
    85. emit showBusy(true);
    86.  
    87. QSqlQuery selectQuery("SELECT * FROM userlogevents WHERE dateTime BETWEEN ? and ?", m_selectDataBase);
    88. selectQuery.addBindValue(beginDate);
    89. selectQuery.addBindValue(endDate);
    90. if(selectQuery.exec()){
    91. qDebug()<<"sql statement executed fine";
    92. }
    93. else{
    94. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 1", "Error: sql select script..."+selectQuery.lastError().text());
    95. return selectQuery.exec();
    96. }
    97.  
    98. userEventLogMsg msg;
    99. beginResetModel();
    100. m_userEventList.clear();
    101. while (selectQuery.next()){
    102. msg.id = selectQuery.value(0).toString();
    103. msg.username = selectQuery.value(1).toString();
    104. msg.eventmessage = selectQuery.value(2).toString();
    105. msg.datetime = selectQuery.value(3).toString();
    106. addEvent(msg);
    107. }
    108. endResetModel();
    109. emit showBusy(false);
    110. m_selectDataBase.close();
    111. return selectQuery.exec();
    112. }
    113.  
    114. bool UserEventLog::archiveEvent(){
    115. //---connect to DB---//
    116. dbConnect();
    117.  
    118. emit showBusy(true);
    119.  
    120. QSqlQuery attachDbQry(m_selectDataBase);
    121. QString sql_str = "ATTACH DATABASE '" + Paths::root() + "/userLogArchive.db' as db2";
    122. attachDbQry.prepare(sql_str);
    123.  
    124. qDebug() << "attached database path: "+sql_str;
    125.  
    126. //---execute attach DB---//
    127. if(attachDbQry.exec()){
    128. qDebug()<<"attached database succesfully";
    129. }
    130. else{
    131. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 2", "Error searching database..."+attachDbQry.lastError().text());
    132. }
    133.  
    134. //---start DB transaction---//
    135. m_selectDataBase.transaction();
    136.  
    137. //---get archive date vlaue--//
    138. QSqlQuery dateQry("SELECT * FROM userlogevents", m_selectDataBase);
    139.  
    140. //---get currentDate minus 30days---//
    141. QString dateStr;
    142. QDate archiveDate = QDate::currentDate().addDays(-30);
    143.  
    144. qDebug() << "archiveDate: "+archiveDate.toString("yyyy-MM-dd");
    145.  
    146. //---copy from one Db table to another---//
    147. QSqlQuery copyDataQry(m_selectDataBase);
    148. bool prepareSqlBool;
    149. prepareSqlBool = copyDataQry.prepare("INSERT INTO db2.userlogarchive (userName, eventMessage, dateTime) SELECT userName, eventMessage, dateTime FROM main.userlogevents WHERE dateTime < ?");
    150. copyDataQry.addBindValue(archiveDate);
    151.  
    152. //---prepare sql copy---//
    153. if(prepareSqlBool){
    154. qDebug()<<"prepare copy sql statement exicuted fine";
    155. }
    156. else{
    157. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 3", "Error: prepare script..."+copyDataQry.lastError().text());
    158. }
    159.  
    160. bool copySqlBool;
    161. copySqlBool = copyDataQry.exec();
    162.  
    163. QSqlQuery archiveDataQry(m_selectDataBase);
    164. QDate rowDate;
    165.  
    166. //---execute sql copy---//
    167. if(copySqlBool){
    168. qDebug()<<"insert copy sql statement exicuted fine";
    169.  
    170. //---Copy rows to archive then delete old rows---//
    171. while(dateQry.next()){
    172. //---Get date value from dateQry---//
    173. dateStr = dateQry.value(3).toString();
    174. rowDate = QDate::fromString(dateStr, "yyyy-MM-dd");
    175.  
    176. //---Executes only if copy qry executes---//
    177. if(rowDate < archiveDate){
    178.  
    179. //---Sql delete statement---//
    180. archiveDataQry.prepare("DELETE FROM userlogevents WHERE dateTime < ?");
    181. archiveDataQry.addBindValue(archiveDate);
    182.  
    183. //---execute delete---//
    184. if(archiveDataQry.exec()){
    185. qDebug()<<"delete sql statement exicuted fine";
    186. }
    187. else{
    188. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 5", "Error: deleting old data..."+archiveDataQry.lastError().text());
    189. //m_selectDataBase.rollback();
    190. return archiveDataQry.exec();
    191. }
    192. }
    193. else{
    194. //qDebug() << "no old records to delete";
    195. return false;
    196. }
    197. }
    198. }
    199. else{
    200. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 4", "Error: copy/insert archive data script..."+copyDataQry.lastError().text());
    201. //m_selectDataBase.rollback();
    202. return copySqlBool;
    203. }
    204.  
    205. //---commit transaction & close---//
    206. m_selectDataBase.commit();
    207. m_selectDataBase.close();
    208. emit showBusy(false);
    209. return copySqlBool;
    210. }
    211.  
    212. //---Leaving out search fcn definitions---//
    To copy to clipboard, switch view to plain text mode 

    I'm just not sure how to start implementing the QSortFilterProxyModel does the sortfilter model take the place of my current model ? do I create a separate class for the proxymodel? I want to be able to sort my list that is created form my model.

    Qt Code:
    1. //---Column Headers for listView table---//
    2. Rectangle {
    3. id: eventMsg_panel
    4. height: 30
    5. radius: 8
    6. color: "#141414"
    7. anchors.right: parent.right
    8. anchors.rightMargin: 6
    9. anchors.left: parent.left
    10. anchors.leftMargin: 5
    11. anchors.top: parent.top
    12. anchors.topMargin: 50
    13. border.width: 5
    14. border.color: "#00000000"
    15. Rectangle{
    16. id: labels
    17. color: "#333131"
    18. radius: 8
    19. anchors.top: parent.top
    20. anchors.topMargin: 0
    21. border.color: "#00000000"
    22. border.width: 2
    23. anchors.bottom: parent.bottom
    24. anchors.right: parent.right
    25. anchors.left: parent.left
    26. Rectangle{
    27. id: id_col
    28. width: 100
    29. anchors.top: parent.top
    30. anchors.bottom: parent.bottom
    31. anchors.left: parent.left
    32. color: "#00000000"
    33. Text {
    34. color: "#ffffff"
    35. anchors.verticalCenter: parent.verticalCenter
    36. anchors.horizontalCenter: parent.horizontalCenter
    37. verticalAlignment: Text.AlignVCenter
    38. text: "ID";
    39. font.pixelSize: 20
    40. horizontalAlignment: Text.AlignHCenter
    41. font.bold: false
    42. }
    43. }
    44. Rectangle{
    45. id:divider1
    46. width:3
    47. anchors.top: parent.top
    48. anchors.bottom: parent.bottom
    49. anchors.left: id_col.right
    50. color: "#8a8a8a"
    51. }
    52. Rectangle{
    53. id: userName_col
    54. width: 150
    55. anchors.top: parent.top
    56. anchors.bottom: parent.bottom
    57. anchors.left: divider1.right
    58. color: "#00000000"
    59. Text {
    60. color: "#ffffff"
    61. anchors.verticalCenter: parent.verticalCenter
    62. anchors.horizontalCenter: parent.horizontalCenter
    63. verticalAlignment: Text.AlignVCenter
    64. text: "User Name";
    65. font.pixelSize: 20
    66. horizontalAlignment: Text.AlignHCenter
    67. font.bold: false
    68. }
    69. }
    70. Rectangle{
    71. id:divider2
    72. width:3
    73. anchors.top: parent.top
    74. anchors.bottom: parent.bottom
    75. anchors.left: userName_col.right
    76. color: "#8a8a8a"
    77. }
    78. Rectangle{
    79. id: eventMsg_col
    80. height: 25
    81. width: 200
    82. anchors.top: parent.top
    83. anchors.bottom: parent.bottom
    84. anchors.left: divider2.right
    85. anchors.right: parent.right
    86. color: "#00000000"
    87. anchors.rightMargin: 156
    88. Text {
    89. height: 20
    90. color: "#ffffff"
    91. verticalAlignment: Text.AlignVCenter
    92. text: "Event Message";
    93. anchors.horizontalCenterOffset: -50
    94. anchors.verticalCenter: parent.verticalCenter
    95. anchors.horizontalCenter: parent.horizontalCenter
    96. horizontalAlignment: Text.AlignHCenter
    97. font.pixelSize: 20
    98. font.bold: false
    99. }
    100. }
    101. Rectangle{
    102. id:divider3
    103. width:3
    104. anchors.top: parent.top
    105. anchors.bottom: parent.bottom
    106. anchors.left: eventMsg_col.right
    107. color: "#8a8a8a"
    108. }
    109. Rectangle{
    110. id: dateTime_col
    111. width: 150
    112. anchors.top: parent.top
    113. anchors.bottom: parent.bottom
    114. anchors.left: divider3.right
    115. color: "#00000000"
    116. Text {
    117. color: "#ffffff"
    118. anchors.verticalCenter: parent.verticalCenter
    119. anchors.horizontalCenter: parent.horizontalCenter
    120. verticalAlignment: Text.AlignVCenter
    121. text: "Date";
    122. font.pixelSize: 20
    123. horizontalAlignment: Text.AlignHCenter
    124. font.bold: false
    125. }
    126. }
    127. }
    128. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. ScrollView{
    2. id: userEvent_scrollView
    3. anchors.fill: parent
    4. anchors.bottomMargin: 5
    5. anchors.topMargin: 5
    6. anchors.rightMargin: 5
    7. anchors.leftMargin: 5
    8. //---Enables flickable Area---//
    9. flickableItem.interactive: true
    10. //---ScrollBar Component---//
    11. style: edit_scrollbar_style
    12. ListView {
    13. id: listView
    14. anchors.fill: parent
    15. model: UserEventLog
    16. delegate: msgDelegate
    17. keyNavigationWraps: true
    18. KeyNavigation.tab: userNameDropDown
    19. orientation : "Vertical"
    20. snapMode: ListView.SnapToItem
    21. boundsBehavior: Flickable.StopAtBounds
    22. clip: true;
    23. highlightMoveVelocity: 2000
    24. highlight: Rectangle{
    25. radius: 7;
    26. color: "red"
    27. }
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jfinn88; 30th September 2016 at 22:36.

  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: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    You just put the proxy model in between the actual model and the view.

    I.e. set the actual model on the proxy via QSortFilterProxyModel::setSourceModel() and then use the proxy as the view's model.

    Cheers,
    _

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

    Default Re: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    I just want to use the virtual sort() function of QSortFilterProxyModel class to sort my columns in my list view if the header/column title is clicked()...

    I add a UserEventModelProxy class that sub classes QSortFilterPRoxyModel class (I only create a construct cuase I just want to use a the virtual function sort() and this function should get called when the constructor dose...)

    I created an instance of my UserEventModelProxy class to use in my UserEventLog class
    I set the sourceModel and set the sortRole to UserEventLog::dateRole
    then set the sort to the date column (it seems this sort is ran when the model is going to populate and not on a signal I want to sort on signal I don’t want sort to happen before)

    I need to connect the column title/head to the sort() virtual function call...

    Qt Code:
    1. #include "usereventlog.h"
    2.  
    3.  
    4. //---UserEvent-SortModelProxy---//
    5. UserEventModelProxy::UserEventModelProxy(QObject *parent) : QSortFilterProxyModel(parent){
    6.  
    7. }
    8. //----------End QSortFilterProxy----------//
    9.  
    10.  
    11.  
    12. //------Begin UserEventLog Class/Model-------//
    13. UserEventLog::UserEventLog(QObject *parent):QAbstractListModel(parent){
    14.  
    15. //---allocates dynamic memory on heap | new instance of proxyModel class---//
    16. m_UserEventModelProxy = new UserEventModelProxy(this);
    17.  
    18. //---Sets the model the proxySort will use e.g. UserEventLog Model---//
    19. m_UserEventModelProxy->setSourceModel(this);
    20.  
    21. //---property holds the item role that is used to query the source model's data when sorting items---//
    22. m_UserEventModelProxy->setSortRole(UserEventLog::dateRole);
    23.  
    24. //---Sorts the model by column in the given order---//
    25. //---Date Column---//
    26. m_UserEventModelProxy->sort(4);
    27. }
    28.  
    29. UserEventLog::~UserEventLog(){
    30.  
    31. }
    32.  
    33. int UserEventLog::rowCount(const QModelIndex &parent) const{
    34. Q_UNUSED(parent);
    35. return m_userEventList.count();
    36. }
    37.  
    38. QHash<int, QByteArray> UserEventLog::roleNames() const{
    39. QHash<int, QByteArray> roleNames;
    40. roleNames.insert(idRole, "id");
    41. roleNames.insert(nameRole, "userName");
    42. roleNames.insert(msgRole, "eventMessage");
    43. roleNames.insert(dateRole, "dateTime");
    44. return roleNames;
    45. }
    46.  
    47. QVariant UserEventLog::data(const QModelIndex &index, int role) const{
    48. if (index.row() < 0 || index.row() >= m_userEventList.count()){
    49. return QVariant();
    50. }
    51.  
    52. QVariant text;
    53.  
    54. if(role == idRole){
    55. userEventLogMsg msg = m_userEventList.at(index.row());
    56. text = msg.id;
    57. }
    58. else if(role == nameRole){
    59. userEventLogMsg msg = m_userEventList.at(index.row());
    60. text = msg.username;
    61. }
    62. else if(role == msgRole){
    63. userEventLogMsg msg = m_userEventList.at(index.row());
    64. text = msg.eventmessage;
    65. }
    66. if(role == dateRole){
    67. userEventLogMsg msg = m_userEventList.at(index.row());
    68. text = msg.datetime;
    69. }
    70. return text;
    71. }
    72.  
    73. void UserEventLog::addEvent(const userEventLogMsg &msg){
    74. beginInsertRows(QModelIndex(), 0, 0);
    75. m_userEventList.insert(0, msg);
    76. endInsertRows();
    77. }
    78.  
    79. void UserEventLog::init(){
    80. //emit showBusy(true);
    81. dbConnect();
    82. archiveEvent();
    83. selectEvent();
    84. //emit showBusy(false);
    85. }
    86.  
    87. bool UserEventLog::dbConnect(){
    88. //---check if database is connected---//
    89. if(!m_selectDataBase.isValid()){
    90. qDebug() << "error in connecting to DB";
    91. m_selectDataBase = QSqlDatabase::addDatabase("QSQLITE", "conn2");
    92. m_selectDataBase.setDatabaseName(Paths::root() + "/userLog.db");
    93.  
    94. qDebug() << "database connect path: "+Paths::root()+"/userLog.db";
    95. m_selectDataBase.open();
    96. }
    97. else{
    98. qDebug() <<"connected to DB" ;
    99. m_selectDataBase.open();
    100. }
    101. return m_selectDataBase.isValid();
    102. }
    103. bool UserEventLog::selectEvent(){
    104. dbConnect();
    105. QDate beginDate = QDate::currentDate();
    106. QDate endDate = QDate::currentDate();
    107.  
    108. emit showBusy(true);
    109.  
    110. QSqlQuery selectQuery("SELECT * FROM userlogevents WHERE dateTime BETWEEN ? and ?", m_selectDataBase);
    111. selectQuery.addBindValue(beginDate);
    112. selectQuery.addBindValue(endDate);
    113. if(selectQuery.exec()){
    114. qDebug()<<"sql statement executed fine";
    115. }
    116. else{
    117. emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 1", "Error: sql select script..."+selectQuery.lastError().text());
    118. return selectQuery.exec();
    119. }
    120.  
    121. userEventLogMsg msg;
    122. beginResetModel();
    123. m_userEventList.clear();
    124. while (selectQuery.next()){
    125. msg.id = selectQuery.value(0).toString();
    126. msg.username = selectQuery.value(1).toString();
    127. msg.eventmessage = selectQuery.value(2).toString();
    128. msg.datetime = selectQuery.value(3).toString();
    129. addEvent(msg);
    130. }
    131. endResetModel();
    132. emit showBusy(false);
    133. m_selectDataBase.close();
    134. return selectQuery.exec();
    135. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class UserEventModelProxy : public QSortFilterProxyModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. UserEventModelProxy(QObject *parent = 0);
    7. };
    8.  
    9. //---Data struct for user event log---//
    10. struct userEventLogMsg{
    11. //---hold all values for a single list entry---//
    12. QString id;
    13. QString username;
    14. QString eventmessage;
    15. QString datetime;
    16. };
    17.  
    18. //---Class UserEventDailyLog | Subed classed: QAbstractTableModel---//
    19. class UserEventLog : public QAbstractListModel
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit UserEventLog(QObject *parent = 0);
    25.  
    26. ~UserEventLog();
    27.  
    28. enum userEventRoles {idRole= Qt::UserRole + 220, nameRole, msgRole, dateRole};
    29.  
    30. int rowCount(const QModelIndex & parent) const;
    31.  
    32. QHash<int, QByteArray> roleNames() const;
    33.  
    34. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
    35.  
    36. Q_INVOKABLE void addEvent(const userEventLogMsg &msg);
    37.  
    38. Q_INVOKABLE bool archiveEvent();
    39.  
    40. Q_INVOKABLE bool selectEvent();
    41.  
    42. Q_INVOKABLE bool dbConnect();
    43.  
    44. Q_INVOKABLE void init();
    45.  
    46. Q_INVOKABLE bool searchDate(QDate userDateText);
    47.  
    48. Q_INVOKABLE bool searchUserName(QString userNameText);
    49.  
    50. Q_INVOKABLE bool searchUserNameDateText(QString userNameText, QDate userDateText);
    51.  
    52. Q_INVOKABLE bool searchDateRange(QDate beginDate, QDate endDate);
    53.  
    54. Q_INVOKABLE bool searchDateRange(const QString &userName, QDate beginDate, QDate endDate);
    55.  
    56. signals:
    57. void showBusy(bool busy_state);
    58.  
    59. void alertDbMsg(int icon, QString title, QString msg_text);
    60.  
    61. private:
    62. QList<userEventLogMsg> m_userEventList;
    63. QSqlDatabase m_selectDataBase;
    64. QSqlQuery m_selectQuery;
    65.  
    66. //---Instance of proxy model class---//
    67. UserEventModelProxy *m_UserEventModelProxy;
    68. };
    69. //--------------------End of userLogEvent Class-----------------//
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //-------------Create instance of class | Set the context property to expose C++ to QML------------//
    2. mUserEventLogModel = new UserEventLog();
    3. m_QmlEngine->rootContext()->setContextProperty("UserEventLog", mUserEventLogModel);
    4.  
    5. mUserEventProxyModel = new UserEventModelProxy();
    6. m_QmlEngine->rootContext()->setContextProperty("UserEventProxyModel", mUserEventProxyModel);
    7. //--------------------------------------------------------------//
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:


    qml
    Qt Code:
    1. //---Column Headers for listView table---//
    2. Rectangle {
    3. id: eventMsg_panel
    4. height: 30
    5. radius: 8
    6. color: "#141414"
    7. anchors.right: parent.right
    8. anchors.rightMargin: 6
    9. anchors.left: parent.left
    10. anchors.leftMargin: 5
    11. anchors.top: parent.top
    12. anchors.topMargin: 50
    13. border.width: 5
    14. border.color: "#00000000"
    15. Rectangle{
    16. id: labels
    17. color: "#333131"
    18. radius: 8
    19. anchors.top: parent.top
    20. anchors.topMargin: 0
    21. border.color: "#00000000"
    22. border.width: 2
    23. anchors.bottom: parent.bottom
    24. anchors.right: parent.right
    25. anchors.left: parent.left
    26. Rectangle{
    27. id: id_col
    28. width: 100
    29. anchors.top: parent.top
    30. anchors.bottom: parent.bottom
    31. anchors.left: parent.left
    32. color: "#00000000"
    33. Text {
    34. color: "#ffffff"
    35. anchors.verticalCenter: parent.verticalCenter
    36. anchors.horizontalCenter: parent.horizontalCenter
    37. verticalAlignment: Text.AlignVCenter
    38. text: "ID";
    39. font.pixelSize: 20
    40. horizontalAlignment: Text.AlignHCenter
    41. font.bold: false
    42. }
    43. }
    44. Rectangle{
    45. id:divider1
    46. width:3
    47. anchors.top: parent.top
    48. anchors.bottom: parent.bottom
    49. anchors.left: id_col.right
    50. color: "#8a8a8a"
    51. }
    52. Rectangle{
    53. id: userName_col
    54. width: 150
    55. anchors.top: parent.top
    56. anchors.bottom: parent.bottom
    57. anchors.left: divider1.right
    58. color: "#00000000"
    59. Text {
    60. color: "#ffffff"
    61. anchors.verticalCenter: parent.verticalCenter
    62. anchors.horizontalCenter: parent.horizontalCenter
    63. verticalAlignment: Text.AlignVCenter
    64. text: "User Name";
    65. font.pixelSize: 20
    66. horizontalAlignment: Text.AlignHCenter
    67. font.bold: false
    68. }
    69. }
    70. Rectangle{
    71. id:divider2
    72. width:3
    73. anchors.top: parent.top
    74. anchors.bottom: parent.bottom
    75. anchors.left: userName_col.right
    76. color: "#8a8a8a"
    77. }
    78. Rectangle{
    79. id: eventMsg_col
    80. height: 25
    81. width: 200
    82. anchors.top: parent.top
    83. anchors.bottom: parent.bottom
    84. anchors.left: divider2.right
    85. anchors.right: parent.right
    86. color: "#00000000"
    87. anchors.rightMargin: 156
    88. Text {
    89. height: 20
    90. color: "#ffffff"
    91. verticalAlignment: Text.AlignVCenter
    92. text: "Event Message";
    93. anchors.horizontalCenterOffset: -50
    94. anchors.verticalCenter: parent.verticalCenter
    95. anchors.horizontalCenter: parent.horizontalCenter
    96. horizontalAlignment: Text.AlignHCenter
    97. font.pixelSize: 20
    98. font.bold: false
    99. }
    100. }
    101. Rectangle{
    102. id:divider3
    103. width:3
    104. anchors.top: parent.top
    105. anchors.bottom: parent.bottom
    106. anchors.left: eventMsg_col.right
    107. color: "#8a8a8a"
    108. }
    109. Rectangle{
    110. id: dateTime_col
    111. width: 150
    112. anchors.top: parent.top
    113. anchors.bottom: parent.bottom
    114. anchors.left: divider3.right
    115. color: "#00000000"
    116. Text {
    117. color: "#ffffff"
    118. anchors.verticalCenter: parent.verticalCenter
    119. anchors.horizontalCenter: parent.horizontalCenter
    120. verticalAlignment: Text.AlignVCenter
    121. text: "Date";
    122. font.pixelSize: 20
    123. horizontalAlignment: Text.AlignHCenter
    124. font.bold: false
    125. }
    126. }
    127. }
    128. }
    To copy to clipboard, switch view to plain text mode 

    qml continued

    Qt Code:
    1. //---Delegate Component for listView | controls key nav & highlighting---//
    2. Component {
    3. id: msgDelegate
    4. Item {
    5. id: active_eventMsg
    6. anchors.left: parent.left
    7. anchors.leftMargin: 5
    8. anchors.right: parent.right
    9. anchors.rightMargin:5
    10. height: 30
    11.  
    12. //---Key Navigation Work Around (listView focus issue)---//
    13. Keys.onDownPressed: listView.incrementCurrentIndex()
    14. Keys.onUpPressed: listView.decrementCurrentIndex()
    15. KeyNavigation.tab: userNameDropDown
    16. activeFocusOnTab: true
    17.  
    18. //---Highlight Functionality---//
    19. MouseArea {
    20. anchors.fill: parent
    21. onClicked: {
    22. listView.focus = true
    23. console.log("msgDelegate activeFocus: "+activeFocus)
    24. listView.currentIndex = index
    25. }
    26. }
    27. //---Rect display model data in listView---//
    28. Rectangle{
    29. id: id_rect
    30. anchors.top: parent.top
    31. anchors.left: parent.left
    32. anchors.leftMargin: 2
    33. width: id_col.width
    34. height: parent.height
    35. color: "#00000000"
    36. Text {
    37. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    38. anchors.fill: parent
    39. verticalAlignment: Text.AlignVCenter
    40. horizontalAlignment: Text.AlignLeft
    41. text: id;
    42. font.pixelSize: 18
    43. }
    44. }
    45. Rectangle{
    46. id: userName_rect
    47. anchors.top: parent.top
    48. anchors.left: id_rect.right
    49. anchors.leftMargin: 2
    50. width: userName_col.width
    51. height: parent.height
    52. color: "#00000000"
    53. Text {
    54. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    55. anchors.fill: parent
    56. anchors.leftMargin: 2
    57. verticalAlignment: Text.AlignVCenter
    58. horizontalAlignment: Text.AlignLeft
    59. text: userName;
    60. font.pixelSize: 18
    61. }
    62. }
    63. Rectangle{
    64. id: eventMsg_rect
    65. anchors.top: parent.top
    66. anchors.left: userName_rect.right
    67. anchors.leftMargin: 2
    68. width: eventMsg_col.width
    69. height: parent.height
    70. color: "#00000000"
    71. Text {
    72. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    73. anchors.fill: parent
    74. anchors.leftMargin: 2
    75. verticalAlignment: Text.AlignVCenter
    76. horizontalAlignment: Text.AlignLeft
    77. text: eventMessage;
    78. font.pixelSize: 18
    79. }
    80. }
    81. Rectangle{
    82. id: dateTime_rect
    83. anchors.top: parent.top
    84. anchors.left: eventMsg_rect.right
    85. anchors.leftMargin: 2
    86. width: dateTime_col.width
    87. anchors.right: parent.right
    88. height: parent.height
    89. color: "#00000000"
    90. Text {
    91. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    92. anchors.fill: parent
    93. anchors.leftMargin: 2
    94. verticalAlignment: Text.AlignVCenter
    95. horizontalAlignment: Text.AlignLeft
    96. text: dateTime;
    97. font.pixelSize: 18
    98. }
    99. }
    100. }
    101. }
    102.  
    103. //---Rect for listView---//
    104. Rectangle {
    105. id: listView_rect
    106. radius: 8
    107. anchors.top: eventMsg_panel.bottom
    108. anchors.topMargin: 2
    109. anchors.bottom: parent.bottom
    110. anchors.bottomMargin: 65
    111. anchors.left:parent.left
    112. anchors.leftMargin: 5
    113. anchors.right:parent.right
    114. anchors.rightMargin: 6
    115. border{
    116. color: "black"
    117. width: 3
    118. }
    119. //---Adds ScrollBars---//
    120. ScrollView{
    121. id: userEvent_scrollView
    122. anchors.fill: parent
    123. anchors.bottomMargin: 5
    124. anchors.topMargin: 5
    125. anchors.rightMargin: 5
    126. anchors.leftMargin: 5
    127. //---Enables flickable Area---//
    128. flickableItem.interactive: true
    129. //---ScrollBar Component---//
    130. style: edit_scrollbar_style
    131. ListView {
    132. id: listView
    133. anchors.fill: parent
    134. model: UserEventLog
    135. delegate: msgDelegate
    136. keyNavigationWraps: true
    137. KeyNavigation.tab: userNameDropDown
    138. orientation : "Vertical"
    139. snapMode: ListView.SnapToItem
    140. boundsBehavior: Flickable.StopAtBounds
    141. clip: true;
    142. highlightMoveVelocity: 2000
    143. highlight: Rectangle{
    144. radius: 7;
    145. color: "red"
    146. }
    147. }
    148. }
    149. }
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:


    "To make your data sortable, you can either implement sort() in your model, " -doc

    I want to reimplement sort but not sure how and also don't want it sort anything when it first displays the model


    Added after 39 minutes:


    update: override the sort() method

    added sortByColumn() method that call the sort function

    need to connect it now...

    Qt Code:
    1. void UserEventModelProxy::sortByColumn(int column, Qt::SortOrder order)
    2. {
    3. sort(column, order);
    4. }
    To copy to clipboard, switch view to plain text mode 


    Added after 23 minutes:


    can yo set the sort indicator for a listView?
    Last edited by jfinn88; 3rd October 2016 at 19:56.

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

    Default Re: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    I set my model of my listView to my proxy model but nothing displays in the listView using the proxyModel


    Added after 1 46 minutes:


    One thing i realized is I only have one column with the way my model works ...
    Last edited by jfinn88; 3rd October 2016 at 22:53.

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

    Default Re: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    update:
    using lessThan custom function to determine which data get placed below when sorting

    using proxyFilter instance in my UserEventLog class constructor

    setRootContext to expose proxySortFilter to qml

    Set the listView's model to the proxySortFilter model using the rootContext set

    created a mouse area in qml with an onClicked call -> on this click I want to sort based of what column they click(since its a list view each row of data only technically has one column I just talking about when they click on the visual display for the column heading in qml)

    Issue 1: I don’t want data sorted when just displaying model I only want to sort if the column heading is clicked (not sure if listView best way but I really like how It visually looks so far and want to keep the listView style)

    Issue 2: calling sort from qml onClicked signal


    custom lessThan()
    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. qDebug() << "made it inside lessThan";
    4.  
    5. //---Create boolean values from the modelIndex passed as parameters---//
    6. bool entry1 = sourceModel()->data(one, UserEventLog::dateRole).toBool();
    7. bool entry2 = sourceModel()->data(two, UserEventLog::dateRole).toBool();
    8.  
    9. //---Check the boolean values for data---//
    10. if(entry1 && entry2){
    11. return QSortFilterProxyModel::lessThan(one, two);
    12. }
    13. if(entry1 && !entry2){
    14. return false;
    15. }
    16. if(!entry1 && entry2){
    17. return true;
    18. }
    19.  
    20. //---Sort the ListView model based off the role type passed/selected---//
    21. if(sortRole() == UserEventLog::idRole){
    22. QVariant dataOne = sourceModel()->data(one, UserEventLog::idRole);
    23. QVariant dataTwo = sourceModel()->data(two, UserEventLog::idRole);
    24.  
    25. QDateTime dateOne = QDateTime::fromString(dataOne.toString());
    26. QDateTime dateTwo = QDateTime::fromString(dataTwo.toString());
    27. if (dateOne != dateTwo) {
    28. return dateOne < dateTwo;
    29. }
    30. else {
    31. QVariant nameOne = sourceModel()->data(one, UserEventLog::idRole);
    32. QVariant nameTwo = sourceModel()->data(two, UserEventLog::idRole);
    33. return (nameOne.toString() < nameTwo.toString());
    34. }
    35. }
    36. else if(sortRole() == UserEventLog::nameRole){
    37. QVariant dataOne = sourceModel()->data(one, UserEventLog::nameRole);
    38. QVariant dataTwo = sourceModel()->data(two, UserEventLog::nameRole);
    39.  
    40. QDateTime dateOne = QDateTime::fromString(dataOne.toString());
    41. QDateTime dateTwo = QDateTime::fromString(dataTwo.toString());
    42. if (dateOne != dateTwo) {
    43. return dateOne < dateTwo;
    44. }
    45. else {
    46. QVariant nameOne = sourceModel()->data(one, UserEventLog::nameRole);
    47. QVariant nameTwo = sourceModel()->data(two, UserEventLog::nameRole);
    48. return (nameOne.toString() < nameTwo.toString());
    49. }
    50. }
    51. else if(sortRole() == UserEventLog::msgRole){
    52. QVariant dataOne = sourceModel()->data(one, UserEventLog::msgRole);
    53. QVariant dataTwo = sourceModel()->data(two, UserEventLog::msgRole);
    54.  
    55. QDateTime dateOne = QDateTime::fromString(dataOne.toString());
    56. QDateTime dateTwo = QDateTime::fromString(dataTwo.toString());
    57. if (dateOne != dateTwo) {
    58. return dateOne < dateTwo;
    59. }
    60. else {
    61. QVariant nameOne = sourceModel()->data(one, UserEventLog::msgRole);
    62. QVariant nameTwo = sourceModel()->data(two, UserEventLog::msgRole);
    63. return (nameOne.toString() < nameTwo.toString());
    64. }
    65. }
    66. else if(sortRole() == UserEventLog::dateRole){
    67. QVariant dataOne = sourceModel()->data(one, UserEventLog::dateRole);
    68. QVariant dataTwo = sourceModel()->data(two, UserEventLog::dateRole);
    69.  
    70. QDateTime dateOne = QDateTime::fromString(dataOne.toString(),"M/d/yyyy hh:mm AP");
    71. QDateTime dateTwo = QDateTime::fromString(dataTwo.toString(), "M/d/yyyy hh:mm AP");
    72. if (dateOne != dateTwo) {
    73. return dateOne < dateTwo;
    74. }
    75. else {
    76. QVariant nameOne = sourceModel()->data(one, UserEventLog::dateRole);
    77. QVariant nameTwo = sourceModel()->data(two, UserEventLog::dateRole);
    78. return (nameOne.toString() < nameTwo.toString());
    79. }
    80. }
    81. else{
    82. return QSortFilterProxyModel::lessThan(one, two);
    83. }
    84. }
    To copy to clipboard, switch view to plain text mode 

    userEventLog constructor (may need to change this since I want sort fcn call on qml onclicked event
    Qt Code:
    1. //------Begin UserEventLog Class/Model-------//
    2. UserEventLog::UserEventLog(QObject *parent):QAbstractListModel(parent){
    3.  
    4. //---allocates dynamic memory on heap | Instance of proxyModel class---//
    5. m_UserEventModelProxy = new UserEventModelProxy(this);
    6.  
    7. //---property holds the source model for proxy model---//
    8. m_UserEventModelProxy->setSourceModel(this);
    9.  
    10. //---property holds the case sensitivity setting used for comparing strings when sorting---//
    11. m_UserEventModelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
    12.  
    13. //---property holds the item role that is used to query the source model's data when sorting items---//
    14. m_UserEventModelProxy->setSortRole(UserEventLog::nameRole);
    15.  
    16. //m_UserEventModelProxy->setSortRole(UserEventLog::nameRole);
    17. //m_UserEventModelProxy->setSortRole(UserEventLog::msgRole);
    18. //m_UserEventModelProxy->setSortRole(UserEventLog::dateRole);
    19.  
    20. //---Sorts the model by column in the given order | this calls lessThan() ---//
    21. m_UserEventModelProxy->sort(0);
    22. }
    To copy to clipboard, switch view to plain text mode 


    qml (visual headers for listView)
    Qt Code:
    1. //---Column Headers for listView table---//
    2. Rectangle {
    3. id: eventMsg_panel
    4. height: 30
    5. radius: 8
    6. color: "#141414"
    7. anchors.right: parent.right
    8. anchors.rightMargin: 6
    9. anchors.left: parent.left
    10. anchors.leftMargin: 5
    11. anchors.top: parent.top
    12. anchors.topMargin: 50
    13. border.width: 5
    14. border.color: "#00000000"
    15. Rectangle{
    16. id: labels
    17. color: "#333131"
    18. radius: 8
    19. anchors.top: parent.top
    20. anchors.topMargin: 0
    21. border.color: "#00000000"
    22. border.width: 2
    23. anchors.bottom: parent.bottom
    24. anchors.right: parent.right
    25. anchors.left: parent.left
    26. Rectangle{
    27. id: id_col
    28. width: 100
    29. anchors.top: parent.top
    30. anchors.bottom: parent.bottom
    31. anchors.left: parent.left
    32. color: "#00000000"
    33. Text {
    34. color: "#ffffff"
    35. anchors.verticalCenter: parent.verticalCenter
    36. anchors.horizontalCenter: parent.horizontalCenter
    37. verticalAlignment: Text.AlignVCenter
    38. text: "ID";
    39. font.pixelSize: 20
    40. horizontalAlignment: Text.AlignHCenter
    41. font.bold: false
    42. }
    43. MouseArea{
    44. id: id_col_mouseArea
    45. anchors.fill: parent
    46. onClicked: {
    47. UserEventModelProxy.sortRole(idRole);
    48. }
    49. }
    50. }
    51. Rectangle{
    52. id:divider1
    53. width:3
    54. anchors.top: parent.top
    55. anchors.bottom: parent.bottom
    56. anchors.left: id_col.right
    57. color: "#8a8a8a"
    58. }
    59. Rectangle{
    60. id: userName_col
    61. width: 150
    62. anchors.top: parent.top
    63. anchors.bottom: parent.bottom
    64. anchors.left: divider1.right
    65. color: "#00000000"
    66. Text {
    67. color: "#ffffff"
    68. anchors.verticalCenter: parent.verticalCenter
    69. anchors.horizontalCenter: parent.horizontalCenter
    70. verticalAlignment: Text.AlignVCenter
    71. text: "User Name";
    72. font.pixelSize: 20
    73. horizontalAlignment: Text.AlignHCenter
    74. font.bold: false
    75. }
    76. MouseArea{
    77. id: userName_col_mouseArea
    78. anchors.fill: parent
    79. onClicked: {
    80. UserEventModelProxy.sortRole(nameRole);
    81. }
    82. }
    83. }
    84. Rectangle{
    85. id:divider2
    86. width:3
    87. anchors.top: parent.top
    88. anchors.bottom: parent.bottom
    89. anchors.left: userName_col.right
    90. color: "#8a8a8a"
    91. }
    To copy to clipboard, switch view to plain text mode 

    qml listView model
    Qt Code:
    1. //---Delegate Component for listView | controls key nav & highlighting---//
    2. Component {
    3. id: msgDelegate
    4. Item {
    5. id: active_eventMsg
    6. anchors.left: parent.left
    7. anchors.leftMargin: 5
    8. anchors.right: parent.right
    9. anchors.rightMargin:5
    10. height: 30
    11.  
    12. //---Key Navigation Work Around (listView focus issue)---//
    13. Keys.onDownPressed: listView.incrementCurrentIndex()
    14. Keys.onUpPressed: listView.decrementCurrentIndex()
    15. KeyNavigation.tab: userNameDropDown
    16. activeFocusOnTab: true
    17.  
    18. //---Highlight Functionality---//
    19. MouseArea {
    20. anchors.fill: parent
    21. onClicked: {
    22. listView.focus = true
    23. console.log("msgDelegate activeFocus: "+activeFocus)
    24. listView.currentIndex = index
    25. }
    26. }
    27. //---Rect display model data in listView---//
    28. Rectangle{
    29. id: id_rect
    30. anchors.top: parent.top
    31. anchors.left: parent.left
    32. anchors.leftMargin: 2
    33. width: id_col.width
    34. height: parent.height
    35. color: "#00000000"
    36. Text {
    37. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    38. anchors.fill: parent
    39. verticalAlignment: Text.AlignVCenter
    40. horizontalAlignment: Text.AlignLeft
    41. text: id;
    42. font.pixelSize: 18
    43. }
    44. }
    45. Rectangle{
    46. id: userName_rect
    47. anchors.top: parent.top
    48. anchors.left: id_rect.right
    49. anchors.leftMargin: 2
    50. width: userName_col.width
    51. height: parent.height
    52. color: "#00000000"
    53. Text {
    54. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    55. anchors.fill: parent
    56. anchors.leftMargin: 2
    57. verticalAlignment: Text.AlignVCenter
    58. horizontalAlignment: Text.AlignLeft
    59. text: userName;
    60. font.pixelSize: 18
    61. }
    62. }
    63. Rectangle{
    64. id: eventMsg_rect
    65. anchors.top: parent.top
    66. anchors.left: userName_rect.right
    67. anchors.leftMargin: 2
    68. width: eventMsg_col.width
    69. height: parent.height
    70. color: "#00000000"
    71. Text {
    72. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    73. anchors.fill: parent
    74. anchors.leftMargin: 2
    75. verticalAlignment: Text.AlignVCenter
    76. horizontalAlignment: Text.AlignLeft
    77. text: eventMessage;
    78. font.pixelSize: 18
    79. }
    80. }
    81. Rectangle{
    82. id: dateTime_rect
    83. anchors.top: parent.top
    84. anchors.left: eventMsg_rect.right
    85. anchors.leftMargin: 2
    86. width: dateTime_col.width
    87. anchors.right: parent.right
    88. height: parent.height
    89. color: "#00000000"
    90. Text {
    91. color: active_eventMsg.ListView.isCurrentItem ? "white" : "black"
    92. anchors.fill: parent
    93. anchors.leftMargin: 2
    94. verticalAlignment: Text.AlignVCenter
    95. horizontalAlignment: Text.AlignLeft
    96. text: dateTime;
    97. font.pixelSize: 18
    98. }
    99. }
    100. }
    101. }
    102.  
    103. //---Rect for listView---//
    104. Rectangle {
    105. id: listView_rect
    106. radius: 8
    107. anchors.top: eventMsg_panel.bottom
    108. anchors.topMargin: 2
    109. anchors.bottom: parent.bottom
    110. anchors.bottomMargin: 65
    111. anchors.left:parent.left
    112. anchors.leftMargin: 5
    113. anchors.right:parent.right
    114. anchors.rightMargin: 6
    115. border{
    116. color: "black"
    117. width: 3
    118. }
    119. //---Adds ScrollBars---//
    120. ScrollView{
    121. id: userEvent_scrollView
    122. anchors.fill: parent
    123. anchors.bottomMargin: 5
    124. anchors.topMargin: 5
    125. anchors.rightMargin: 5
    126. anchors.leftMargin: 5
    127. //---Enables flickable Area---//
    128. flickableItem.interactive: true
    129. //---ScrollBar Component---//
    130. style: edit_scrollbar_style
    131. ListView {
    132. id: listView
    133. anchors.fill: parent
    134. model: UserEventModelProxy
    135. delegate: msgDelegate
    136. keyNavigationWraps: true
    137. KeyNavigation.tab: userNameDropDown
    138. orientation : "Vertical"
    139. snapMode: ListView.SnapToItem
    140. boundsBehavior: Flickable.StopAtBounds
    141. clip: true;
    142. highlightMoveVelocity: 2000
    143. highlight: Rectangle{
    144. radius: 7;
    145. color: "red"
    146. }
    147. }
    148. }
    149. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure what or how to call the proxyModel lessThan() from qml onClicked

    I'm not sure if calling sort() form UserEventLog constructor is right way to do it since I don’t want the data sorted at first....

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

    Default Re: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    update: made some good improvements but still having issues implementing proxysortfilter

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

  7. #7
    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: QProxyFilterSort sort listView column headers by alaph and by ass. dec.

    I have not looked at all the code, but are you sure the model you expose through the root context is the model you need?

    Some of your code seems to indicate that you create a useless sort filter proxy model inside the main model and the expose another, empty, proxy model to QML.

    Cheers,
    _

Similar Threads

  1. QTableWidgte, sort by CheckBox column
    By Asting in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2014, 11:10
  2. QTableView column-headers too short ...
    By kerim in forum Newbie
    Replies: 2
    Last Post: 20th April 2011, 10:09
  3. QTableView + column span of headers
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 11th November 2009, 15:29
  4. How does the QTableWidget sort a column?
    By codemonkey in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 14:21
  5. Replies: 2
    Last Post: 20th August 2008, 16:55

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.