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. Construct a QML object with parameters
Forum Update on Monday, May 27th 2025

Construct a QML object with parameters

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.2k 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.
  • R Offline
    R Offline
    Richard Lee
    wrote on last edited by
    #1

    I derived a sub-class of QSqlTableModel, let's say SqlTableModel.

    In the C++ code, I can create an instance of the SqlTableModel by SqlTableModel *myModel = SqlTableModel(0, db), where the db is a QSqlDatabase instance.

    Then I register this class to QML by qmlRegisterType<SqlTableModel>("QmlSql", 1,0, "SqlTableModel);

    My question is, in the QML code, how can I passed the db parameter to the SqlTableModel object?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! See Exposing Attributes of C++ Types to QML, it shows how to expose properties and methods.

      R 1 Reply Last reply
      1
      • ? A Former User

        Hi! See Exposing Attributes of C++ Types to QML, it shows how to expose properties and methods.

        R Offline
        R Offline
        Richard Lee
        wrote on last edited by
        #3

        @Wieland I have read through this reference, but it does not allow to expose a QSqlDatabase property to QML by declaring:
        Q_PROPERTY(QSqlDatabase db READ db WRITE setDb).

        I think this is because there is no such type in the QML type system, so the QML does recognize it.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Custom types for QML have to inherit from QObject.

          R 1 Reply Last reply
          3
          • ? A Former User

            Custom types for QML have to inherit from QObject.

            R Offline
            R Offline
            Richard Lee
            wrote on last edited by
            #5

            @Wieland Ah ha, yeah, I just forgot this point. Thank you so much for your help!

            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