QTableView sort column data by UserRole












1















I have a QStandardItemModel piped into a QTableView. One of the columns in my model contains dates which have user-friendly displayData and computer-friendly userData. So for example one QStandardItem might display a string like 22 Nov 2018 but the userdata would look like 324586 (seconds since the epoch). However when I sort the column it of course sorts by the displayData. How can I force the table to sort by userData instead?










share|improve this question





























    1















    I have a QStandardItemModel piped into a QTableView. One of the columns in my model contains dates which have user-friendly displayData and computer-friendly userData. So for example one QStandardItem might display a string like 22 Nov 2018 but the userdata would look like 324586 (seconds since the epoch). However when I sort the column it of course sorts by the displayData. How can I force the table to sort by userData instead?










    share|improve this question



























      1












      1








      1








      I have a QStandardItemModel piped into a QTableView. One of the columns in my model contains dates which have user-friendly displayData and computer-friendly userData. So for example one QStandardItem might display a string like 22 Nov 2018 but the userdata would look like 324586 (seconds since the epoch). However when I sort the column it of course sorts by the displayData. How can I force the table to sort by userData instead?










      share|improve this question
















      I have a QStandardItemModel piped into a QTableView. One of the columns in my model contains dates which have user-friendly displayData and computer-friendly userData. So for example one QStandardItem might display a string like 22 Nov 2018 but the userdata would look like 324586 (seconds since the epoch). However when I sort the column it of course sorts by the displayData. How can I force the table to sort by userData instead?







      python pyqt pyqt5 qtableview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 2:53









      eyllanesc

      79.2k103257




      79.2k103257










      asked Nov 27 '18 at 1:58









      SpencerSpencer

      509318




      509318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to use setSortRole():



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          text = t.toString("dd MMM yyyy")
          it = QtGui.QStandardItem(text)
          it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.setSortRole(QtCore.Qt.UserRole)
          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here



          Although I prefer to save the QDateTime directly and use a delegate to show the data with the format you want.



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class DateDelegate(QtWidgets.QStyledItemDelegate):
          def displayText(self, value, locale):
          return locale.toString(value, "dd MMM yyyy")

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)
          delegate = DateDelegate(self._tableView)
          self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          it = QtGui.QStandardItem()
          it.setData(t, QtCore.Qt.DisplayRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer


























          • Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

            – Spencer
            Nov 27 '18 at 2:42











          • @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

            – eyllanesc
            Nov 27 '18 at 2:45











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491693%2fqtableview-sort-column-data-by-userrole%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to use setSortRole():



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          text = t.toString("dd MMM yyyy")
          it = QtGui.QStandardItem(text)
          it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.setSortRole(QtCore.Qt.UserRole)
          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here



          Although I prefer to save the QDateTime directly and use a delegate to show the data with the format you want.



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class DateDelegate(QtWidgets.QStyledItemDelegate):
          def displayText(self, value, locale):
          return locale.toString(value, "dd MMM yyyy")

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)
          delegate = DateDelegate(self._tableView)
          self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          it = QtGui.QStandardItem()
          it.setData(t, QtCore.Qt.DisplayRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer


























          • Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

            – Spencer
            Nov 27 '18 at 2:42











          • @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

            – eyllanesc
            Nov 27 '18 at 2:45
















          1














          You have to use setSortRole():



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          text = t.toString("dd MMM yyyy")
          it = QtGui.QStandardItem(text)
          it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.setSortRole(QtCore.Qt.UserRole)
          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here



          Although I prefer to save the QDateTime directly and use a delegate to show the data with the format you want.



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class DateDelegate(QtWidgets.QStyledItemDelegate):
          def displayText(self, value, locale):
          return locale.toString(value, "dd MMM yyyy")

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)
          delegate = DateDelegate(self._tableView)
          self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          it = QtGui.QStandardItem()
          it.setData(t, QtCore.Qt.DisplayRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer


























          • Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

            – Spencer
            Nov 27 '18 at 2:42











          • @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

            – eyllanesc
            Nov 27 '18 at 2:45














          1












          1








          1







          You have to use setSortRole():



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          text = t.toString("dd MMM yyyy")
          it = QtGui.QStandardItem(text)
          it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.setSortRole(QtCore.Qt.UserRole)
          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here



          Although I prefer to save the QDateTime directly and use a delegate to show the data with the format you want.



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class DateDelegate(QtWidgets.QStyledItemDelegate):
          def displayText(self, value, locale):
          return locale.toString(value, "dd MMM yyyy")

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)
          delegate = DateDelegate(self._tableView)
          self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          it = QtGui.QStandardItem()
          it.setData(t, QtCore.Qt.DisplayRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer















          You have to use setSortRole():



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          text = t.toString("dd MMM yyyy")
          it = QtGui.QStandardItem(text)
          it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.setSortRole(QtCore.Qt.UserRole)
          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here



          Although I prefer to save the QDateTime directly and use a delegate to show the data with the format you want.



          from PyQt5 import QtCore, QtGui, QtWidgets
          import random

          DATECOLUMN = 1

          class DateDelegate(QtWidgets.QStyledItemDelegate):
          def displayText(self, value, locale):
          return locale.toString(value, "dd MMM yyyy")

          class MainWindow(QtWidgets.QMainWindow):
          def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self._tableView = QtWidgets.QTableView()
          self.setCentralWidget(self._tableView)

          self._model = QtGui.QStandardItemModel(10, 4)
          self._tableView.setModel(self._model)
          delegate = DateDelegate(self._tableView)
          self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

          now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
          for i in range(self._model.rowCount()):
          for j in range(self._model.columnCount()):
          if j == DATECOLUMN:
          t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
          it = QtGui.QStandardItem()
          it.setData(t, QtCore.Qt.DisplayRole)
          else:
          it = QtGui.QStandardItem("{}-{}".format(i, j))
          self._model.setItem(i, j, it)

          self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = MainWindow()
          w.show()
          sys.exit(app.exec_())


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 '18 at 2:38

























          answered Nov 27 '18 at 2:14









          eyllanesceyllanesc

          79.2k103257




          79.2k103257













          • Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

            – Spencer
            Nov 27 '18 at 2:42











          • @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

            – eyllanesc
            Nov 27 '18 at 2:45



















          • Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

            – Spencer
            Nov 27 '18 at 2:42











          • @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

            – eyllanesc
            Nov 27 '18 at 2:45

















          Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

          – Spencer
          Nov 27 '18 at 2:42





          Ah, so I need to set it on the model, not the view! Still getting used to where everything lives in model/view framework... Thanks for the very complete answer!

          – Spencer
          Nov 27 '18 at 2:42













          @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

          – eyllanesc
          Nov 27 '18 at 2:45





          @Spencer I recommend you read: doc.qt.io/qt-5/model-view-programming.html so you understand something about MVC in Qt. On the other hand I suggest that for future questions where you point out that something is failing you provide a Minimal, Complete, and Verifiable example

          – eyllanesc
          Nov 27 '18 at 2:45




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491693%2fqtableview-sort-column-data-by-userrole%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks