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. How to provide an empty row in QTableView
Forum Updated to NodeBB v4.3 + New Features

How to provide an empty row in QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 499 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
    Mark81
    wrote on last edited by
    #1

    In my Qt 6.8.0 application I display a SQlite table:

    // ...open db and create tables
    QSqlTableModel *_model;
    
    QSqlDatabase db = QSqlDatabase::database("mydb");
    _model = new QSqlTableModel(this, db);
    _model->setTable("mytable");
    _model->setEditStrategy(QSqlTableModel::OnRowChange);
    _model->select();
    

    I want to provide an empty row at the end of the table to let the user to add a new record without using a QPushButton.
    Something like:

    _model->insertRows(_model->rowCount(), 1);
    

    that leads to:

    1b6e0ee2-1a24-4bb6-a4f4-6304c9173ddc-image.png

    Of course this does not work as expected since the record is already in edit mode. Until I insert a valid data I cannot edit other rows.

    Please note that my tables have not null keys, so I can't just add and save an empty record.

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

      Hi,

      One possible way is to use a proxy model that returns the number of rows + 1 and when editing that row, calls whatever is necessary to create the row before adding data to it.

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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        One possible way is to use a proxy model that returns the number of rows + 1 and when editing that row, calls whatever is necessary to create the row before adding data to it.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @SGaist actually, I already have a proxy model:

        QSqlTableModel *_model;
        QSortFilterProxyModel *_proxyModel;
        
        QSqlDatabase db = QSqlDatabase::database("mydb");
        _model = new QSqlTableModel(this, db);
        _model->setTable("mytable");
        _model->setEditStrategy(QSqlTableModel::OnRowChange);
        _model->select();
        
        _proxyModel = new QSortFilterProxyModel(this);
        _proxyModel->setSourceModel(_model);
        _proxyModel->sort(1);
        
        ui->table->setModel(_proxyModel);
        

        is there a way to use this proxy to do what you suggested or I have to create a new subclass of QSortFilterProxyModel which in turn inherits QAbstractProxyModel?

        JonBJ 1 Reply Last reply
        0
        • M Mark81

          @SGaist actually, I already have a proxy model:

          QSqlTableModel *_model;
          QSortFilterProxyModel *_proxyModel;
          
          QSqlDatabase db = QSqlDatabase::database("mydb");
          _model = new QSqlTableModel(this, db);
          _model->setTable("mytable");
          _model->setEditStrategy(QSqlTableModel::OnRowChange);
          _model->select();
          
          _proxyModel = new QSortFilterProxyModel(this);
          _proxyModel->setSourceModel(_model);
          _proxyModel->sort(1);
          
          ui->table->setModel(_proxyModel);
          

          is there a way to use this proxy to do what you suggested or I have to create a new subclass of QSortFilterProxyModel which in turn inherits QAbstractProxyModel?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Mark81
          You could subclass, so _proxyModel = new SubClassedSortFilterProxyModel(this);, and implement the extra row there. But I think I'd leave that as-is because it's useful, and interpose a new QIdentityModel-subclass on top of it to just provide the dummy row for editing when required.

          M 1 Reply Last reply
          2
          • JonBJ JonB

            @Mark81
            You could subclass, so _proxyModel = new SubClassedSortFilterProxyModel(this);, and implement the extra row there. But I think I'd leave that as-is because it's useful, and interpose a new QIdentityModel-subclass on top of it to just provide the dummy row for editing when required.

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @JonB yeah, it works. Basically I did the following:

            class AddIdentityProxyModel : public QIdentityProxyModel
            {
            public:
                AddIdentityProxyModel(QObject *parent = 0): QIdentityProxyModel(parent)
                {
                }
            
                int rowCount(const QModelIndex &parent = QModelIndex()) const
                {
                    return sourceModel()->rowCount() + 1;
                }
            };
            

            then

            QSqlTableModel *_model;
            QSortFilterProxyModel *_proxyModel;
            AddIdentityProxyModel *_addProxy;
            
            QSqlDatabase db = QSqlDatabase::database("mydb");
            _model = new QSqlTableModel(this, db);
            _model->setTable("mytable");
            _model->setEditStrategy(QSqlTableModel::OnRowChange);
            _model->select();
            
            _proxyModel = new QSortFilterProxyModel(this);
            _proxyModel->setSourceModel(_model);
            _proxyModel->sort(1);
            
            _addProxy = new AddIdentityProxyModel(this);
            _addProxy->setSourceModel(_proxyModel);
            
            ui->table->setModel(_addProxy);
            

            it needs some tweaks in order to be fully functional, but I think it could work!

            1 Reply Last reply
            1
            • M Mark81 referenced this topic on

            • Login

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