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. Filtering the QAbstractTableModel is slow.
Forum Updated to NodeBB v4.3 + New Features

Filtering the QAbstractTableModel is slow.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.5k 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.
  • I Offline
    I Offline
    ikuris
    wrote on last edited by
    #1

    Hello, I am using custom model that inherits QAbstractTableModel, QTableView, and custom class inherits QSortFilterProxyModel. I am using beginInsertRows and endInsertRows in my custom model.

    When I apply filtering, GUI freeze for a short time. I override filterAcceptRows in my custom class. When I change the filter It freeze some time based on how many rows I have (+10.000). What can I do about it ?

    Thanks a lot.

    Christian EhrlicherC 1 Reply Last reply
    0
    • I ikuris

      Hello, I am using custom model that inherits QAbstractTableModel, QTableView, and custom class inherits QSortFilterProxyModel. I am using beginInsertRows and endInsertRows in my custom model.

      When I apply filtering, GUI freeze for a short time. I override filterAcceptRows in my custom class. When I change the filter It freeze some time based on how many rows I have (+10.000). What can I do about it ?

      Thanks a lot.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ikuris said in Filtering the QAbstractTableModel is slow.:

      What can I do about it ?

      speed up your filterAcceptsRow() function would be a good start.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • I Offline
        I Offline
        ikuris
        wrote on last edited by
        #3

        Hello,
        Any advice on what I should look out for in my filterAcceptsRow() function?
        I'm checking every column in my custom filter class to see if this is what I want. I'm doing the search for each column in the filterAcceptRow function.

        Christian EhrlicherC 1 Reply Last reply
        0
        • I ikuris

          Hello,
          Any advice on what I should look out for in my filterAcceptsRow() function?
          I'm checking every column in my custom filter class to see if this is what I want. I'm doing the search for each column in the filterAcceptRow function.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ikuris said in Filtering the QAbstractTableModel is slow.:

          Any advice on what I should look out for in my filterAcceptsRow() function?

          Without code?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ikuris
            wrote on last edited by
            #5

            bool MyCustomClass::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
            {
            QModelIndex index_0 = sourceModel()->index(source_row,0,source_parent);
            QModelIndex index_1 = sourceModel()->index(source_row,1,source_parent);
            QModelIndex index_2 = sourceModel()->index(source_row,2,source_parent);
            QModelIndex index_3 = sourceModel()->index(source_row,3,source_parent);

            bool flag1 = sourceModel()->data(index_0).toString().contains("filter1");
            
            bool flag2 = sourceModel()->data(index_1).toString().contains("filter2");
            
            // COMPARECOLUMN3&4 BASICALLY CHECKS FOR FLAGS (I HAVE IN MY CUSTOM CLASS THEN RETURNIN BOOLEAN  
            
            bool flag3 = compareColum3(sourceModel()->data(index_2).toString().toUtf8().data());
            
            bool flag4 = compareColumn4(sourceModel()->data(index_3).toString().toUtf8().data());
            
            return flag1 && flag2 && flag3 && flag4;
            

            }

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @ikuris said in Filtering the QAbstractTableModel is slow.:

              compareColum3

              What does this function do ? Why does it need a plain char*
              I would add a function to retrieve the whole data structure from the source model instead going through data()

              auto sm = static_cast<MyModel*>(sourceModel());
              auto myStruct = sm->getCompleteData(source_row);
              bool flag1 = myStruct->whatever.contains(QLatin1String("filter1"));
              ...
              

              This avoids a lot of to/fromQVariant conversions.
              And you should do an early exit instead evaluating all 4 booleans.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ikuris
                wrote on last edited by
                #7

                It is comparing strings based on the flags that I got. I got different flags for compareColumn3 and and compareColumn4 for their personal filtering settings. Basically functions makes able to find data between x and y, or applying other opperation != , <, >, >= ,<=).

                Thanks for your suggestion I will try ASAP. Any other suggestions ?

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ikuris said in Filtering the QAbstractTableModel is slow.:

                  Any other suggestions ?

                  show compareColumn3() and 4 function.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ikuris
                    wrote on last edited by
                    #9

                    bool MyCustomClass::compareColumn3(const std::string& filter)
                    {
                    switch(myCompareDataFlag)
                    {
                    case flagBigger: return filter < mFilter;
                    case flagSmalller: return filter > mFilter;
                    ...
                    ...
                    }
                    return true;

                    }

                    Could converting QStriing (toUtf().data() ) be the main cause of slowing down my GUI? If It is I pass QString as parameter. For compareColumn4 I am parsing QString with regex to get usefull parts in that section.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Conversion is always slow, es. when you convert it three times for unknown reason.
                      Pass a const qSTring and make mFilter as QString too.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • I Offline
                        I Offline
                        ikuris
                        wrote on last edited by
                        #11

                        Thank you very much, I will try what you said and get back to you as soon as possible.

                        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