Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView and QSortFilterProxyModel

QTableView and QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 573 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I am using QTableView, I was originally using just QStnadardItemModel, now I've implemented QSortFilterProxyModel.

    mpsfpTmodel = new QSortFilterProxyModel(this);
    mpsiTmodel = new QStandardItemModel(this);
    mpsfpTmodel->setSourceModel(mpsiTmodel);
    mptvTrecs = new QTableView(this);
    mptvTrecs->setSelectionBehavior(QAbstractItemView::SelectRows);
    mptvTrecs->setMinimumWidth(cintThreatsTableWidth);
    mptvTrecs->setModel(mpsfpTmodel);
    mptvTrecs->setSortingEnabled(true);
    

    The problem I'm having is that the columns contain numbers and these do not sort correctly.
    ddd75ce3-8117-47ac-ba85-a2e0a986b7d4-image.png
    How do I fix this?

    Kind Regards,
    Sy

    raven-worxR 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #6

      @SPlatten
      for this you have to subclass.

      The cleaner solution would be to ensure the source data is already in the correct type.

      You could also add a custom ItemRole to your source model and call QSortFilterProxyModel::setSortRole() with that new custom role. In the source model then return the data in the correct type for that role.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #2

        What's the source of your data in your model and how is your model created? It looks like they are being added to your model as text/strings instead of numbers.

        SPlattenS 1 Reply Last reply
        0
        • M mchinand

          What's the source of your data in your model and how is your model created? It looks like they are being added to your model as text/strings instead of numbers.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #3

          @mchinand , the data is read from MariaDB then in the application I process the query and append each record to the model mpsiTmodel. How do I add to the model as numbers?

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • SPlattenS SPlatten

            I am using QTableView, I was originally using just QStnadardItemModel, now I've implemented QSortFilterProxyModel.

            mpsfpTmodel = new QSortFilterProxyModel(this);
            mpsiTmodel = new QStandardItemModel(this);
            mpsfpTmodel->setSourceModel(mpsiTmodel);
            mptvTrecs = new QTableView(this);
            mptvTrecs->setSelectionBehavior(QAbstractItemView::SelectRows);
            mptvTrecs->setMinimumWidth(cintThreatsTableWidth);
            mptvTrecs->setModel(mpsfpTmodel);
            mptvTrecs->setSortingEnabled(true);
            

            The problem I'm having is that the columns contain numbers and these do not sort correctly.
            ddd75ce3-8117-47ac-ba85-a2e0a986b7d4-image.png
            How do I fix this?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #4

            @SPlatten said in QTableView and QSortFilterProxyModel:

            How do I fix this?

            probably they are stored as strings and not integers?!

            Override QSortFilterProxyModel::lessThan():

            bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
            {
                 if( source_left.column() == MY_COLUMN && source_right.column() == MY_COLUMN )
                     return source_left.data().toInt() < source_right.data().toInt();
            
                return QSortFilterProxyModel::lessThan(source_left,source_right);
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            SPlattenS 1 Reply Last reply
            2
            • raven-worxR raven-worx

              @SPlatten said in QTableView and QSortFilterProxyModel:

              How do I fix this?

              probably they are stored as strings and not integers?!

              Override QSortFilterProxyModel::lessThan():

              bool MySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
              {
                   if( source_left.column() == MY_COLUMN && source_right.column() == MY_COLUMN )
                       return source_left.data().toInt() < source_right.data().toInt();
              
                  return QSortFilterProxyModel::lessThan(source_left,source_right);
              }
              
              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              @raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?

              Kind Regards,
              Sy

              raven-worxR 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @raven-worx , thank you, is it possible to implement that function in a parent class or is the only way to sub-class QSortFilterProxyModel ?

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #6

                @SPlatten
                for this you have to subclass.

                The cleaner solution would be to ensure the source data is already in the correct type.

                You could also add a custom ItemRole to your source model and call QSortFilterProxyModel::setSortRole() with that new custom role. In the source model then return the data in the correct type for that role.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi,

                  Out of curiosity, how are you filling your model ?
                  If these values are stored as numerical value in your database, you should retrieve and store them in a similar fashion so no need for any kind of subclassing.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  SPlattenS 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Out of curiosity, how are you filling your model ?
                    If these values are stored as numerical value in your database, you should retrieve and store them in a similar fashion so no need for any kind of subclassing.

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #8

                    @SGaist , I've sorted out this problem now, I'm not in front of the laptop I was using, so I will post an update tomorrow when I'm back in front of it.

                    Kind Regards,
                    Sy

                    Seb TurS 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @SGaist , I've sorted out this problem now, I'm not in front of the laptop I was using, so I will post an update tomorrow when I'm back in front of it.

                      Seb TurS Offline
                      Seb TurS Offline
                      Seb Tur
                      wrote on last edited by
                      #9

                      @SPlatten
                      What I do is I make sure to cast to numbers and no nulls in the query
                      eg. SELECT CAST(IFNULL(kompletacja,0) AS DECIMAL(10,5)) as 'kompletacja' FROM TABLE

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved