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. Unable to pass QModelIndex to a function due to some C++ constrait
Forum Update on Monday, May 27th 2025

Unable to pass QModelIndex to a function due to some C++ constrait

Scheduled Pinned Locked Moved Solved General and Desktop
3 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.
  • S Offline
    S Offline
    scotryder
    wrote on last edited by
    #1

    Hi there,

    I have override filterAcceptRow but unable to pass index0 to a custom function and get the following error

    error: reference to type 'const QModelIndex' could not bind to an rvalue of type 'const QModelIndex *' CanBeFilter(&index0, &canReturn);
    

    Here is my code so far

    bool FilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
    
    
    
         const QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    
    
         
         bool canReturn = true;
     
     
     
         CanBeFilter(&index0, &canReturn);
    
         return canReturn;
    
    }
    
    //CanBeFilter Signature is
    void FilterProxyModel::CanBeFilter(QModelIndex &index, bool &canReturn) const
    
    

    I know its a C++ issue but im unable to solve it.

    Thanks

    Taz742T jsulmJ 2 Replies Last reply
    0
    • S scotryder

      Hi there,

      I have override filterAcceptRow but unable to pass index0 to a custom function and get the following error

      error: reference to type 'const QModelIndex' could not bind to an rvalue of type 'const QModelIndex *' CanBeFilter(&index0, &canReturn);
      

      Here is my code so far

      bool FilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
      {
      
      
      
           const QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
      
      
           
           bool canReturn = true;
       
       
       
           CanBeFilter(&index0, &canReturn);
      
           return canReturn;
      
      }
      
      //CanBeFilter Signature is
      void FilterProxyModel::CanBeFilter(QModelIndex &index, bool &canReturn) const
      
      

      I know its a C++ issue but im unable to solve it.

      Thanks

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @scotryder
      Because index0 is const and in your function

      void FilterProxyModel::CanBeFilter(QModelIndex &index, bool &canReturn) {
          //index is reference index0 and index is not const.
          //const QModelIndex index0; It means that it should not change. But in this function you give it the right to change it.
      }
      

      You can add const QModelIndex &index or remove 'const' 'QModelIndex index0'.

      http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

      Do what you want.

      1 Reply Last reply
      3
      • S scotryder

        Hi there,

        I have override filterAcceptRow but unable to pass index0 to a custom function and get the following error

        error: reference to type 'const QModelIndex' could not bind to an rvalue of type 'const QModelIndex *' CanBeFilter(&index0, &canReturn);
        

        Here is my code so far

        bool FilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
        {
        
        
        
             const QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
        
        
             
             bool canReturn = true;
         
         
         
             CanBeFilter(&index0, &canReturn);
        
             return canReturn;
        
        }
        
        //CanBeFilter Signature is
        void FilterProxyModel::CanBeFilter(QModelIndex &index, bool &canReturn) const
        
        

        I know its a C++ issue but im unable to solve it.

        Thanks

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @scotryder said in Unable to pass QModelIndex to a function due to some C++ constrait:

        CanBeFilter(&index0, &canReturn);

        You're trying to pass pointers where references are expected. Change it to:

        CanBeFilter(index0, canReturn);
        

        And as @Taz742 pointed out make first parameter const.
        In addition CanBeFilter should return the result instead of modifying a parameter:

        bool FilterProxyModel::CanBeFilter(const QModelIndex &index) const;
        
        bool canReturn = CanBeFilter(index0);
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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