Partially select text of a QTableView cell



  • Hello!

    I have a QTableView with a custom model. The data() method of my model shows money amounts with a currency sign in all the sells (i.e. "100.40 EUR"). Now I want the following: When the user double clicks a cell to edit the money amound, I only want the number to be selected, but NOT the currency. So in this example: When the user double clicks the cell, I only want the "100.40" to be selected (blue), but not the " EUR" part.

    Currencly my data() method looks like this:
    @
    QVariant OpenPositionSet::data(const QModelIndex &index, int role) const {

    if( !index.isValid() || index.row() >= mPositions.size() )
        return QVariant();
    
    if(role == Qt::EditRole) {
        return ??
    }
    
    if (role == Qt::DisplayRole) 
            return mPositions[index.row()].get(index.column()) + " EUR"; // Returns i.e. "100.40 EUR"
      
    return QVariant();
    

    }@

    I thought I can do this with the EditRole. But when I return the same as in DisplayRole, the whole text is selected.
    When I just return "100.40", then the "EUR" is not visible at all. I want it visible, but just not selected.

    Anyone knows how I can make it so that editing the cell shows "100.40 EUR", but only "100.40" is selected?


  • Moderators

    I would use a delegate for that.

    Have a look at "the spin box example":http://qt-project.org/doc/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html

    Hope it helps


Log in to reply
 

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