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. Insert row into QSQLTableModel
Forum Updated to NodeBB v4.3 + New Features

Insert row into QSQLTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.4k Views 2 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.
  • M Offline
    M Offline
    moravas
    wrote on last edited by
    #1

    Hi Folks,

    I would like to insert a new row into QSQLTableModel instance via a subclass of QStyledItemDelegate:

    void EditorDelegate::insertRow(QAbstractItemModel* model, const QModelIndex& index) const {
    model->insertRow(index.row() + 1);
    model->setData(model->index(index.row() + 1, 0), QUuid::createUuid());
    }
    

    In the second line would like to set the primary key.
    The problem is that after the insertion a new row will be displayed without the generated UUID and the row isn't clickable so this delegate isn't draw itself on the top of the new cell.

    Can anybody help me? Or please sign me if more information required.

    Regards,
    Norbert

    kshegunovK 1 Reply Last reply
    0
    • M moravas

      Hi Folks,

      I would like to insert a new row into QSQLTableModel instance via a subclass of QStyledItemDelegate:

      void EditorDelegate::insertRow(QAbstractItemModel* model, const QModelIndex& index) const {
      model->insertRow(index.row() + 1);
      model->setData(model->index(index.row() + 1, 0), QUuid::createUuid());
      }
      

      In the second line would like to set the primary key.
      The problem is that after the insertion a new row will be displayed without the generated UUID and the row isn't clickable so this delegate isn't draw itself on the top of the new cell.

      Can anybody help me? Or please sign me if more information required.

      Regards,
      Norbert

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @moravas
      Hello,
      It this UUID supposed to be visible in the column? If so you might want to set its role. Something like this:

      QUuid id = QUuid::createUuid();
      model->setData(model->index(index.row() + 1, 0), id);
      model->setData(model->index(index.row() + 1, 0), id.toString(), Qt::DisplayRole);
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moravas
        wrote on last edited by
        #3

        Hi,

        thank you: I checked your solution without success :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Following Qt's documentation, insertRow insert a single row before the row given. So unless I'm mistaken, you are calling setData on the wrong index (off by one)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            asanka424
            wrote on last edited by
            #5

            How do you call "EditorDelegate::insertRow" ? Do you pass the QModelIndex of last row in the model?

            Also you are setting the data in wrong index as mentioned my SGaist

            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