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. Using a registered type when using a class from a c++ backend for a qml frontend
QtWS25 Last Chance

Using a registered type when using a class from a c++ backend for a qml frontend

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 598 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
    rizoritis
    wrote on last edited by
    #1

    Hello,

    I have a Database class in my c++ backend in which I manage all my SQLite databases. I am using this backend and have created an API for my QML frontend to see. In order to do that I had to register the class in my main.cpp as follows:

    @qmlRegisterType<Database>("Backend", 1, 0, "Database");@

    In doing so I realized that within my c++ backend I was unable to use any member functions from Database because I have not made an instance of it. To solve this problem I did the obvious:

    @Database *database = new Database;@

    Now I am able to use the instance, database, to access member functions within my c++ code. My issue is that it seems to call the constructor twice and therefore tries to create the database twice. The solution may be obvious but my mind seems to be going blank. How can I register my class so that QML can see it while at the same time create an instance of it so that I can use it in my c++ code? What I am doing seems to be redundant and I get a bunch of warnings such as the following:

    "QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed."

    Obviously this is done because the database is trying to connect twice. Any ideas on the write way to register a class while making an instance to use within the backend?

    Thanks!

    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