Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. TableView how to delete a row

TableView how to delete a row

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.3k 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.
  • morteasherahM Offline
    morteasherahM Offline
    morteasherah
    wrote on last edited by morteasherah
    #1

    Hello to all. I have been using Qt/QtQuick for a project recently and I am stuck on deleting rows from a TableView. Below is the offending code

     TableView {
                    anchors.top: aBar.bottom
                    anchors.topMargin: 3
                    sortIndicatorVisible: true
                    frameVisible: false
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    onSortIndicatorColumnChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
                    onSortIndicatorOrderChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder)
                    id: dataView
    
                    TableViewColumn {
                        role: "Nome"
                        title: "Nome"
                        width: 200
                    }
    
                    TableViewColumn {
                        role: "Residenza"
                        title: "Residenza"
                        width: 200
                    }
    
                    TableViewColumn {
                        role: "Assicurazione"
                        title: "Assicurazione"
                        width: 200
                    }
    
                    TableViewColumn {
                        width: 128
                        resizable: false
    
                        delegate: RowLayout {
                                anchors.fill: parent
                                clip: true
    
                                IconButton {
                                    iconName: "content/create"
                                    onClicked: console.log(styleData.row)
                                }
    
                                IconButton {
                                    iconName: "action/delete"
                                    onClicked: {
                                        console.log(styleData.row)
                                        sqlSortedData.eliminaCliente(styleData.row)
                                    }
                                }
                        }
                    }
                    model: sqlSortedData
                }
    
     Q_INVOKABLE void eliminaCliente(int row) {
            QSqlTableModel *sm = (QSqlTableModel*)sourceModel();
            sm->removeRow(row);
    }
    

    Ok, so where is the problem? The code correctly removes the columns that are part of the model, but this isn't the case for the last column, the one where I use the delegate to show the edit and delete buttons.

    I cannot figure out why it works this way.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vladstelmahovsky
      wrote on last edited by
      #2

      because they are not the part of the model?

      1 Reply Last reply
      0
      • morteasherahM Offline
        morteasherahM Offline
        morteasherah
        wrote on last edited by
        #3

        I thought the same. This implies that TableView redraws everything each time the model changes, right?

        I think I don't have a clear view of how TableView instantiates the row objects. If a row is not present in the model during the redraw operation, why the corresponding custom column is instantiated?

        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