Role in setData has no effect



  • Hi,
    I have the following in my .h file:

      bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) ;  
    

    in .cpp:
    I have the function:

    bool FixDb::setData(const QModelIndex &index, const QVariant &value, int role )
    {
        if(role == Qt::EditRole)
            {
                qDebug()     << "Entered setData bool.";
                int rowFix = index.row ();
                int colFix = index.column ();
    
                qDebug() << "Setdata row: " << rowFix;
                qDebug() << "Setdata col: " << colFix;
            }
        return true;
    }
    

    bool setData is called from an other function:

    void FixDb::on_tableView_Fix_doubleClicked(const QModelIndex &index)
    {
        int row = index.row ();
        int column = index.column ();
        qDebug() << "Fix Index: " << "(" << row <<"," << column << ")";
    
        QModelIndex fixIndex(index.model()->index(index.row(), 0, index.parent()));
        qDebug() << fixIndex.data(Qt::DisplayRole);
        QVariant v(fixIndex.data(Qt::DisplayRole));
        fixID = v.toString ();
        qDebug () << "Fix ID: " << fixID;
    
        QModelIndex fixItemIndex(index.model ()->index (row,column, index.parent()));
        qDebug() << fixItemIndex.data (Qt::DisplayRole);
        QVariant fixV;
        fixV = fixItemIndex.data (Qt::DisplayRole);
        itemToFix = fixV.toString ();
    
        qDebug() << "Item to fix: " << itemToFix;
    
        QSqlQuery query_getImage;
        query_getImage.prepare ("SELECT Pic FROM Items WHERE ID = :FriendID");
        query_getImage.bindValue (":FriendID",FriendID);
    
        setData(index, value, role);
    

    In the .h the editRole is set, but clearly it doesn't transfer to the actual bool function in the cpp file as processing does not enter

    if(role == Qt::EditRole)
    

    It jumps directly to the return true. Why don't I have the editRole set in the actual function? Thank you.


  • Moderators

    @gabor53 You are passing some role here:

    setData(index, value, role);
    

    Try without passing so that it takes the default value.

    setData(index, value);
    


  • @p3c0
    Thank you.

    setData(index, value);
    

    worked.


Log in to reply
 

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