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



  • 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



  • @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/


  • Moderators

    @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);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.