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. How can I put a column from a SQLite table into the options of a Combobox?
Forum Updated to NodeBB v4.3 + New Features

How can I put a column from a SQLite table into the options of a Combobox?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 623 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.
  • M Offline
    M Offline
    MaximBozek
    wrote on last edited by
    #1

    I thought exposing the data as a QStringListModel to QML would work.
    I currently have this:
    main.qml:

            ComboBox {
                id: country_loaded
                width: 200
                height: 50
                model: plateModel
    
                background: Rectangle {
                    color: mecs_white
                    border.width: 3
                    border.color: mecs_white
                    radius: 10
                }
            }
    

    main.py:

    class DataManager(QObject):
        def __init__(self):
            super().__init__()
            self.model = QStringListModel()
    
        def load_plate_data(self):
            # Connect to the SQLite database
            conn = sqlite3.connect('rfid_gate_system.db')
            cursor = conn.cursor()
    
            # Query the plates
            cursor.execute("SELECT plate FROM plates")
            plates = [row[0] for row in cursor.fetchall()]
    
            # Update the model with the plate data
            self.model.setStringList(plates)
    
            # Close the connection
            conn.close()
    
    if __name__ == "__main__":
        app = QGuiApplication(sys.argv)
    
        engine = QQmlApplicationEngine()
    
        data_manager = DataManager()
        data_manager.load_plate_data()
        engine.rootContext().setContextProperty("plateModel", data_manager.model)
    
        qml_file = Path(__file__).resolve().parent / "main.qml"
    
        engine.load(qml_file)
    
        if not engine.rootObjects():
            sys.exit(-1)
    
        sys.exit(app.exec())
    

    getting this error:

    qrc:/qt-project.org/imports/QtQuick/Controls/Windows/ComboBox.qml:65:9: Unable to assign QQmlDMAbstractItemModelData to QString
    
    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      When using a QAbstractItemModel subclass with multiple roles as the model of a ComboBox, you need to set its textRole (and valueRole).
      In your case you should add textRole: "display" to your ComboBox .

      M 2 Replies Last reply
      1
      • M MaximBozek has marked this topic as solved on
      • M Offline
        M Offline
        MaximBozek
        wrote on last edited by
        #3

        Hell yea, it works

        1 Reply Last reply
        0
        • GrecKoG GrecKo

          When using a QAbstractItemModel subclass with multiple roles as the model of a ComboBox, you need to set its textRole (and valueRole).
          In your case you should add textRole: "display" to your ComboBox .

          M Offline
          M Offline
          MaximBozek
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • GrecKoG GrecKo

            When using a QAbstractItemModel subclass with multiple roles as the model of a ComboBox, you need to set its textRole (and valueRole).
            In your case you should add textRole: "display" to your ComboBox .

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

            @GrecKo Do you maybe also know how i can style the text in my dropdown?

            GrecKoG 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              See https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qcombobox
              In the QComboBox::drop-down section you can style text as you would do in a label

              "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

              M 1 Reply Last reply
              1
              • VRoninV VRonin

                See https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qcombobox
                In the QComboBox::drop-down section you can style text as you would do in a label

                M Offline
                M Offline
                MaximBozek
                wrote on last edited by
                #7

                @VRonin I am programming in qml.

                1 Reply Last reply
                0
                • M MaximBozek

                  @GrecKo Do you maybe also know how i can style the text in my dropdown?

                  GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on last edited by
                  #8

                  @MaximBozek If you just want to change the text you can do so by setting an explicit displayText property. If you want to change how it is displayed you can do so by changing the contentItem.

                  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