Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [QtQuick] ItemSelectionModel for custom QAstractTableModel

[QtQuick] ItemSelectionModel for custom QAstractTableModel

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
4 Posts 2 Posters 510 Views 1 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.
  • L Offline
    L Offline
    lumbo7332
    wrote on last edited by lumbo7332
    #1

    I have this main.py with a custom QAbstractTableModel:

    import sys
    import signal
    
    
    from PyQt6.QtGui import QGuiApplication
    from PyQt6.QtQml import QQmlApplicationEngine, qmlRegisterSingletonInstance
    from PyQt6.QtCore import Qt, QAbstractTableModel
    
    
    class InstalledPkgsModel(QAbstractTableModel):
        def __init__(self, data):
            super(InstalledPkgsModel, self).__init__()
            self._data = data
    
        def data(self, index, role):
            if role == Qt.ItemDataRole.DisplayRole:
                value = self._data[index.row()][index.column()]
    
                return value
    
        def rowCount(self, index):
            return len(self._data)
    
        def columnCount(self, index):
            try:
                return len(self._data[0])
            # If there are no installed mods in the prefix
            except IndexError:
                return 1
    
    
    # Make app respond to Ctrl-C
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.quit.connect(app.quit)  # type: ignore
    
    # Populate manage table view installed packages
    data = [
        ["git", "", "common", "1.0"],
        ["distutils", "", "common", "1.0"],
        ["bsa", "", "common", "0.4"],
        ["nexus", "", "common", "1.0"],
        ["fallout_4", "", "common", "0.1"],
    ]
    installed_pkgs_model = InstalledPkgsModel(data)
    qmlRegisterSingletonInstance(
        "installed_pkgs_model", 1, 0, "InstalledPkgsModel", installed_pkgs_model
    )
    
    engine.load("main.qml")
    
    sys.exit(app.exec())
    

    I'd like to set something up where I can select rows in the TableView that's using this model. I currently have this main.qml:

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    import installed_pkgs_model 1.0
    
    ApplicationWindow {
        visible: true
        width: 1000
        height: 700
        title: "Portmod"
    
        TableView {
            id: installedPkgsTable
    
            width: 1000
            height: 700
            columnSpacing: 1
            rowSpacing: 1
            clip: true
            model: InstalledPkgsModel
    
            selectionModel: ItemSelectionModel {
                model: installedPkgsTable.model
            }
    
            delegate: Rectangle {
                required property bool selected
    
                implicitWidth: 300
                implicitHeight: 50
                color: selected ? "blue" : "lightgray"
    
                Text {
                    text: display
                }
    
            }
    
        }
    
    }
    

    The problem is, it doesn't work. Clicking on a row does nothing.

    1 Reply Last reply
    0
    • CristianMaureiraC Offline
      CristianMaureiraC Offline
      CristianMaureira
      wrote on last edited by
      #2

      Hey,
      can you share how you are exposing the QAbstractTableModel (in Python) to your QML file? (better if you can share the full code :P )

      L 2 Replies Last reply
      0
      • CristianMaureiraC CristianMaureira

        Hey,
        can you share how you are exposing the QAbstractTableModel (in Python) to your QML file? (better if you can share the full code :P )

        L Offline
        L Offline
        lumbo7332
        wrote on last edited by
        #3

        @CristianMaureira Sure! I've updated the main post to be a MRE.

        1 Reply Last reply
        0
        • CristianMaureiraC CristianMaureira

          Hey,
          can you share how you are exposing the QAbstractTableModel (in Python) to your QML file? (better if you can share the full code :P )

          L Offline
          L Offline
          lumbo7332
          wrote on last edited by
          #4

          @CristianMaureira Was the MRE helpful?

          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