Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Using a registered type when using a class from a c++ backend for a qml frontend
rizoritis last edited by
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?