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. Partially select text of a QTableView cell

Partially select text of a QTableView cell

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    compyler
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      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

      Qt Certified Specialist
      www.edalsolutions.be

      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