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. Filter a column with QSortFilterProxyModel
QtWS25 Last Chance

Filter a column with QSortFilterProxyModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.0k 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.
  • J Offline
    J Offline
    JBosco
    wrote on last edited by JBosco
    #1

    Sorry because I don't speak English very well

    To speed up my project, I decided to postpone my problem here. I use the setFilterRegExp function a method of the QSortFilterProxyModel object to filter a column. The concern is that I want to filter the empty cell with it. Can someone give me an idea to do it?
    (google translate)

    JonBJ 1 Reply Last reply
    0
    • J JBosco

      Sorry because I don't speak English very well

      To speed up my project, I decided to postpone my problem here. I use the setFilterRegExp function a method of the QSortFilterProxyModel object to filter a column. The concern is that I want to filter the empty cell with it. Can someone give me an idea to do it?
      (google translate)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @JBosco
      I see your problem because of https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterRegularExpression-prop

      If no QRegularExpression or an empty string is set, everything in the source model will be accepted.

      (BTW, use setFilterRegExpression now instead of setFilterRegExp, I think despite the docs).

      You could try:

      • setFilterRegExpression("^$")
      • setFilterFixedString("")

      but I don't know if either of those two will work.

      Otherwise, I think you will need to override https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterAcceptsRow and code there for your empty (or NULL) value. That provides an alternative way to filter instead of using the regular expressions/fixed strings.

      P.S.
      Do you want the filter to accept/include only empty column, as I have assumed, or do you want it to reject/exclude empty column? If you want to see only non-empty, setFilterRegExpression(".+") should do it.

      J 1 Reply Last reply
      2
      • JonBJ JonB

        @JBosco
        I see your problem because of https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterRegularExpression-prop

        If no QRegularExpression or an empty string is set, everything in the source model will be accepted.

        (BTW, use setFilterRegExpression now instead of setFilterRegExp, I think despite the docs).

        You could try:

        • setFilterRegExpression("^$")
        • setFilterFixedString("")

        but I don't know if either of those two will work.

        Otherwise, I think you will need to override https://doc.qt.io/qt-5/qsortfilterproxymodel.html#filterAcceptsRow and code there for your empty (or NULL) value. That provides an alternative way to filter instead of using the regular expressions/fixed strings.

        P.S.
        Do you want the filter to accept/include only empty column, as I have assumed, or do you want it to reject/exclude empty column? If you want to see only non-empty, setFilterRegExpression(".+") should do it.

        J Offline
        J Offline
        JBosco
        wrote on last edited by
        #3

        Thank you, setFilterRegExpression("^$") works for me

        JonBJ 1 Reply Last reply
        1
        • J JBosco

          Thank you, setFilterRegExpression("^$") works for me

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @JBosco
          OK, good, I didn't know setFilterRegExpression() expects a "whole" regular expression, I thought it already implies it had its own ^...$ around whatever you specify. Qt docs are not always clear on this in the various places regular expressions are used.

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

            @JonB Qt doesn't modify your regular expression unless you use a function that explicitly does that like QRegularExpression::anchoredPattern.

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

            JonBJ 1 Reply Last reply
            0
            • SGaistS SGaist

              @JonB Qt doesn't modify your regular expression unless you use a function that explicitly does that like QRegularExpression::anchoredPattern.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @SGaist
              That was not what I meant. I was thinking of somewhere, maybe widget validators, where it was not clear to me whether the pattern you were giving was treated as if it had to match the whole input, i.e. as though it were enclosed in ^$. And in that case specifying ^$ would be two literal characters, and not match empty.

              EDIT Ah, https://doc.qt.io/qt-5/qregexpvalidator.html#details

              When QRegExpValidator determines whether a string is Acceptable or not, the regexp is treated as if it begins with the start of string assertion (^) and ends with the end of string assertion ($); the match is against the entire input string, or from the given position if a start position greater than zero is given.

              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