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. Updating QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

Updating QAbstractTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 474 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.
  • A Offline
    A Offline
    adaptine
    wrote on last edited by
    #1

    Hello.

    I've implemented a class which inherits from QAbstractTableModel. I'm getting data to a tableview in QML and everything, but I'm struggling updating the table on certain events.

    The table data is retrieved from a SQL-Lite database, and I'm simply emitting dataChanged() when QFileSystemWatcher emits fileChanged() on the database-file (after the data is renewed from DB). This works fine, and the table-content is updated.

    So there is two issues I have:

    1: I want to change the delegate (rectangle) color depending on 5 different states retrieved from the database.

    2: There might be fewer or more rows retrieved from the database on an update meaning I have to refresh the table in order to remove rows not relevant any more. I'm fine with updating the entire table since there is not many rows.

    How can this be solved?

    My TableView in qml:

    TableView {
            id: tableView
    
            property var columnWidths: [150, 150, 760, 180]
            columnWidthProvider: function (column) { return columnWidths[column]; }
            rowHeightProvider: function (column) { return 40; }
            rowSpacing: 5
    
            anchors.fill: parent
            anchors.topMargin: 100
            anchors.leftMargin: 20
            topMargin: columnsHeader.implicitHeight
            clip: true
            flickableDirection: Flickable.VerticalFlick
    
            model: alarmmodel
            delegate: Rectangle {
                    color: Style.current.componentBG2
                    Text {
                        text: alarmdata
                        anchors.fill: parent
                        anchors.margins: 10
    
                        color: Style.current.colorFont
                        font.pixelSize: 15
                        verticalAlignment: Text.AlignVCenter
                       
                        }
                    }
                }
    
            Row {
                id: columnsHeader
                y: tableView.contentY
                z: 2
                Repeater {
                    model: tableView.columns > 0 ? tableView.columns : 1
                    Label {
                        width: tableView.columnWidthProvider(modelData)
                        height: 35
                        text: alarmmodel.headerData(modelData, Qt.Horizontal)
                        color: '#888888'
                        font.pixelSize: 15
                        font.bold: true
                        padding: 10
                        verticalAlignment: Text.AlignVCenter
    
                        background: Rectangle { color: Style.current.componentBG }
                    }
                }
            }
    
            ScrollIndicator.vertical: ScrollIndicator { }
        }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @adaptine said in Updating QAbstractTableModel:

      simply emitting dataChanged()

      instead call beginResetModel() before fetching the new data and endResetModel() after the update is done

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • A Offline
        A Offline
        adaptine
        wrote on last edited by
        #3

        Bingo!!!

        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