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. QTableView/QAbstractTableModel Text disappears after double click
QtWS25 Last Chance

QTableView/QAbstractTableModel Text disappears after double click

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 6.6k 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.
  • J Offline
    J Offline
    jc-denton
    wrote on 21 Mar 2012, 16:07 last edited by
    #1

    I need a table editor for a custom model. As explained in the Qt tutorial I enabled the Qt::ItemIsEditable flag and implemented setData() in the model. Editing works fine so far, but there is one little detail I don't like about it. After double clicking an element the text in the cell is removed (it comes back again if I press escape) and I can enter new text. I would like a more MS Excel like editing behavior where the text is not removed and the cursor is placed at the point where the user double clicked.

    I have been reading the docs about the classes involved a bit and QAbstractItemDelegate caught my attention. The docs show an example where the string in the cell is replaced with a progress bar. I wonder in order to get the desired editing behavior do I need to implement my own ItemDelegate or is there a simple way to do this?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jc-denton
      wrote on 22 Mar 2012, 07:37 last edited by
      #2

      Just read that data() is also called when editing, so if I just also return text if the role is Qt::EditRole the text does not disappear after double clicking! However the whole text is selected (I would like to have just the cursor at the right place) and it does not seem to open a multi-line on tables with multiple cells.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 22 Mar 2012, 07:47 last edited by
        #3

        You will need to create your own delegate to do the cursor positioning. That should not be too hard. I don't understand what you mean by your last phrase. What do you expect to happen exactly?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jc-denton
          wrote on 22 Mar 2012, 08:07 last edited by
          #4

          Thx for your quick replay, I am new to Qt and therefore need to learn a few concepts (like delegates) first..

          bq. it does not seem to open a multi-line on tables with multiple cells.

          Ooops I mean a multi-line editor of course. The QTableView displays text with multiple lines, but when editing I can only edit one line. Also pasting a multi-line string from a text editor ends up on one line.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 22 Mar 2012, 08:10 last edited by
            #5

            The problem is, that the default delegate will be a QLineEdit, which is a single-line editor widget. You will need to make sure that a QTextEdit or QPlainTextEdit is used instead. One way to do that is by creating your own delegate.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jc-denton
              wrote on 22 Mar 2012, 09:45 last edited by
              #6

              My solution so far:

              @// views is a of type QTableView*
              QAbstractItemDelegate *itemDelegate = view->itemDelegate();

              // Not sure why this is QStyledItemDelegate and not just QItemDelegate.
              // But QStyledItemDelegate also offers setItemEditorFactory(..) so this
              // is fine.
              assert(typeid(QStyledItemDelegate) == typeid(*itemDelegate));

              QStyledItemDelegate *styledItemDelegate
              = dynamic_cast<QStyledItemDelegate *>(itemDelegate);

              QItemEditorFactory *editorFactory = new QItemEditorFactory;

              // "plainText" is the name of the property of the QPlainTextEdit which
              // contains the editing data.
              QItemEditorCreatorBase *creator =
              new QItemEditorCreator<QPlainTextEdit>("plainText");

              editorFactory->registerEditor(QVariant::String, creator);

              styledItemDelegate->setItemEditorFactory(editorFactory);@

              Now I also need to figure out how to place the cursor correctly, right now it is always before the beginning of the string.

              1 Reply Last reply
              0

              2/6

              22 Mar 2012, 07:37

              4 unread
              • Login

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