So You Can’t Get Your Qt Models to Work With QML?

This particular rant was started by my looking to divert my mind by answering a question on StackOverflow. Never ever ever go to StackOverflow. Kids today just don’t know squat and there is nothing you can do to help them. Let’s start with my initial response an a slightly improved image.

The gist of the issue

QML is just a hand polished turd. There is no polite way to put it. Qt made a desperate ploy to attract script kiddies in the iDiot phone market with QML. The problem is, by and large, script kiddies don’t know anything. They tend to be “self-taught” with far more emphasis on “self” than “taught.” No grounding in the fundamentals of application design and software development. They just want to hack.

They like scripting languages because you can “just hack” without rules. If you want to get an idea for just how much disinformation exists in the script kiddie world, check out the claims about Python being type safe.

Hopefully you will all click the link and look at the original question and code. Please do so now!

My Initial Response

This image may not be 100% accurate but it is how one must visualize things in their mind. You have three distinct lanes an object lives in when you introduce QML: C++, QML Engine, and JavaScript engine. Each and every one of those lanes believes beyond a shadow of a doubt that they control the life and death of the object.

When you are just passing integers around that are passed by value, this is no issue. When you are passing QStrings around, because of the copy-on-write charter of Qt, this is only a minor issue. When you are passing real objects around, worse yet, complex containers of real objects, you have to understand this completely. The answer that “fixed” your problem really only masked it. You will find there is a lot of QML and JavaScript code out there that exists only to mask an application not respecting the lanes.

Had this application used an actual database there would be a fourth lane. Yes, SQLite provides an SQL interface and allows many things, but an actual database has an external engine providing shared access and controlling the life of cursors. SQLite files tend to be single user. Yes, multiple threads within your application can access it, but while your application is running you cannot open a terminal window and use command line tools to examine the database. Think of it more as a really nice indexed file system without sharing.

So, you create an object in C++ and then expose it to QML. The QML engine now believes beyond a doubt that it controls the life and death of that object despite not having its own copy.

QML is really feeble. It can’t actually do much, so it has to hand any significant object off to JavaScript. The JavaScript engine now believes beyond a shadow of a doubt that it now controls the life and death of that object.

You need to also envision these lanes as independent threads. There will most likely be many threads within each, but, in general, any signal or communication between these will go on the event loop of the target as a queued event. That means it will only be processed when it finally bubbles to the top of the queue for that event loop.

This, btw, is why you can never use Smart Pointers when also using QML/JavaScript. Especially the kind that do reference counting and delete the object when there are no more references. There is no way for the C++ lane to know that QML or JavaScript are still using the object.

The answer telling you to check for undefined property is masking the problem that your code is drunk driving across all lanes. Eventually, on a faster (or sometimes slower) processor garbage collection for one of the lanes will run at a most inopportune moment and you will be greeted with a stack dump. (The drunk driving code will hit a tree that does not yield.)

Correct Solution #1: Never use QML or JavaScript. Just use C++ and Widgets. Stay entirely within the C++ lane. If that is a route open to you it’s a good way to go. There is an awful lot of production code out there doing just that. You can obtain a copy of this book (or just download the source code from the page) and muddle through building it.

Correct Solution #2: Never actually do anything in QML or JavaScript. This is an all together different solution than #1. You can use QML for UI only, leaving all logic in C++. Your code is failing because you are trying to actually do something. I haven’t built or tried your code. I simply saw

function deleteRowFromDatabase(row)

which shouldn’t exist at all. C++ holds your model. You emit a signal from your QML/JavaScript lanes when user action requires deletion. This signal becomes a queued event in the C++ lane. When it is processed the row will be deleted and the model updated. If you have properly exposed your model to QML it will emit some form of “model changed” signal and the UI will update accordingly. One of the main points of MVC (Model-View-Controler) is communication to/from the model. When the data changes it notifies the view(s).

Correct Solution #3: Never use C++. Have your C++ be a “Hello World!” shell that just launches your QML. Never create and share an object between C++ and the other two lanes. Do everything inside of JavaScript and QML.

Binding loop errors, in general, happen when code drunk drives across all three lanes. They also happen when code doesn’t view each lane as at least one distinct thread with its own event loop. The C++ lane hasn’t finished (perhaps not even started) the delete operation but your code is already trying to use the result.

The Final Solution

Lots of back and forth happened. When someone has locked themselves into a failed architecture they are generally the last ones to see the failure of their plan. I know. I’ve been on the other side of that. You have to be on the other side of that a few times to learn how to avoid it in the future.

First problem is they chose to use QML. That locked them into using a model. You will find out why that was bad in a bit.

Second problem was drunk driving across all of the lanes. When you stay in the green lane, life is good. Theoretically, when you stay in the red lane life is just as good, you simply can’t do anything real. If you choose to exist only in the yellow lane, then you don’t need Qt at all.

Third problem was the fact there was absolutely no reason for a proxy. You are supposed to use a proxy when you need to transform data for display, like formatting a date or populating a combo box of reference table values. Some also use them to ensure a read-only data source cannot accidentally receive a write request.

Fourth problem was not really knowing anything about a relational database. I see this a lot in the self-taught universe. I even see it with recent college graduates because they can pursue some kind of Web/Game development degree and nobody thinks to include a course on how to care for and feed a relational database.

The Models

QML Turd

There is just no polite way to describe QML. This forcing of models upon the world would be fine if it was properly architected. Hell, suitable debugging and error information would go a long way.

QVariant data(const QModelIndex &index, int role) const;

That one line causes more problems than anything else. If it doesn’t exactly match that in your concrete implementation, you will get no data. The documentation and the IDE will lead many to believe it should have a different signature.

“Oh, but if it’s not right it will just give me an error, right?”


If you are using this model in a table the model will return the row count and your table will be all NULL data. If QML can’t find this, it won’t tell you. Adding insult to injury there is not a flag or debugging option you can set to force QML to identify which data() method it used.

So, if you have 60 rows in your table the model will tell the UI there are 60 rows and you will have 60 NULL rows and, most likely a scrollbar.


This is where OOP jumped the shark. You can get my latest book and read about MVVM. I won’t repeat that essay. What I will tell you is that MVVM was created by people who don’t know how to use a relational database and are physically incapable of understanding data, without a relation, is useless.

If someone walks into a room and says “25” then leaves you have no idea WTF they are talking about. If someone walks into a room and says “John Smith is 25” then leaves, you may or may not know who John Smith is, but you now know he is 25. The 25 now has meaning because it has a relation. With the possible exception of NULL, there is no meaning without relation.

You need to know the previous information because it provides a frame of reference. People who don’t know how to properly use relational databases create the models. When they create automated tests for them these tests will be worse than non-existent.

void SQLiteModel::deleteRow(int rowNo)
     qDebug() << "removeRow called for " << rowNo << "\n";
     bool rslt = removeRow(rowNo);
     qDebug() << "result of removeRow() " << rslt << "\n";
     qDebug() << "Error: " << lastError().text() << "\n";
     qDebug() << "query: " << query().lastQuery() << "\n";

When the above executes you see the following.

removeRow called for  3
 result of removeRow()  false
 Error:  "near \"WHERE\": syntax error Unable to execute statement"
 query:  "select * from questions;" 

It didn’t matter if the table was created with this


or with this


It appears this has been broken for over a year, if it ever worked at all.

Lack of Formal Training

Formal training. Actually attending a good school for computer science cannot be overvalued. Sadly there are a lot of run-for-profit shit schools along with just plain shit schools.

Formal training. That’s what teaches you to generate large test data sets. Testing with only ten or fewer records won’t help you find anything. Even for some hokey little program like this you need to initially test with at least enough records to force a scrollbar onto the UI. At least 60 would be good. 500 would be better. There is an ipsum generator if you really hate typing that much.

Formal training. That’s what teaches you the first step to solving a database table issue of any kind is verifying the creation and population of the table from the command line. If you read through the original code you will see it was just stuffing records into the proxy.

Formal training. That’s what teaches you the “default debugger” and moving onto other I/O operations like deleting rows. Other things that can be verified from the command line.

Formal training. That is what teaches you proper software development technique. The language and tools you use don’t matter. Proper technique is what matters.

You can solve any programming problem with the proper technique formal training provides. That and a formal logic class that teaches no programming language is what one needs to succeed in IT. Tools come and go. Those two things are constants.

The Code

QT += quick sql
 CONFIG += c++11
 This is important
 CONFIG += qmltypes
 You can make your code fail to compile if it uses deprecated APIs.
 In order to do so, uncomment the following line.
 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
         main.cpp \
 RESOURCES += qml.qrc
 Additional import path used to resolve QML modules in Qt Creator's code model
 Additional import path used to resolve QML modules just for Qt Quick Designer
 Default rules for deployment.
 qnx: target.path = /tmp/$${TARGET}/bin
 else: unix:!android: target.path = /opt/$${TARGET}/bin
 !isEmpty(target.path): INSTALLS += target
 class SQLiteModel : public QSqlTableModel
     explicit SQLiteModel(QObject *parent = NULL);
 bool setQuery(const QString &query); void defaultQuery(); virtual QHash<int, QByteArray> roleNames() const; QVariant data(const QModelIndex &index, int role) const; void declareQML(); Q_INVOKABLE QString getQuestion( int rowNo);
 public slots:
     void deleteRow( int rowNo);
     void copyRow( int rowNo);
     void updateRow( int rowNo, QString txt);
     bool openDb();
     void addStarterData();
 QSqlDatabase db; int m_recordCount;
include "sqlitemodel.h"
 void SQLiteModel::declareQML()
     qmlRegisterSingletonInstance("Fred", 1, 0, "MyModel", this);
 SQLiteModel::SQLiteModel(QObject *parent)
 bool SQLiteModel::openDb()
     const QString DB_NAME = "test.db";
     QDir hDir = QDir::home();
     bool retVal = false;
 db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(hDir.absoluteFilePath(DB_NAME)); retVal =; if (retVal) {     QSqlQuery query(db);     query.exec("CREATE TABLE IF NOT EXISTS questions (Q_NO INTEGER PRIMARY KEY AUTOINCREMENT, Q_TEXT TEXT NOT NULL);");     query.exec("SELECT COUNT(*) FROM questions;");     while (     {         QSqlRecord rec = query.record();         int recordCount = rec.field(0).value().toInt();         if (recordCount < 10)         {             addStarterData();         }     } } return retVal;
 bool SQLiteModel::setQuery(const QString &query)
 if (this->query().record().isEmpty()) {     qWarning() << "SQLiteModel::setQuery() -" << this->query().lastError();     return false; } m_recordCount = record().count(); return true;
 void SQLiteModel::defaultQuery()
     setQuery("select * from questions;");
 QHash SQLiteModel::roleNames() const
     QHash roles;
 for( int i = 0; i < record().count(); i++) {     roles[Qt::UserRole + i + 1] = record().fieldName(i).toLatin1(); } qDebug() << "Roles: " << roles << "\n"; return roles;
 QVariant SQLiteModel::data(const QModelIndex &index, int role) const
     QVariant value = QSqlQueryModel::data(index, role);
     if(role < Qt::UserRole)     {         value = QSqlQueryModel::data(index, role);     }     else     {         int columnIdx = role - Qt::UserRole - 1;         QModelIndex modelIndex = this->index(index.row(), columnIdx);
         value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
 return value;
 void SQLiteModel::addStarterData()
     QSqlQuery preparedQuery(db);
     QStringList lst;
 lst << "Why is the sky blue"     << "What color is the sky on your planet"     << "What makes water wet"     << "Do you drink tea"     << "Is left the opposite of right"     << "If Fred is a boy, what is the name of his dog"     << "Who are you"     << "What do you want"     << "Which way is up"     << "Who knows what evil lurks in the hearts of men"     << "Two wrongs don't make a right, but do three lefts"     << "If boiling has a point, what is it"; preparedQuery.prepare("INSERT INTO questions (Q_TEXT) VALUES(:txt);"); for (QString txt : lst) {     preparedQuery.bindValue(":txt", txt);     preparedQuery.exec(); }
 QString SQLiteModel::getQuestion(int rowNo)
     QSqlRecord rec = record(rowNo);
     return rec.field("Q_TEXT").value().toString();
 /* Qt's view of the world really kind of sucks when it comes to
 relational databases. They want you to do everything within the model.
 A real programmer wants to do everything directly at the database
 and let some watchdog re-run selects as necessary. Admittedly that
 approach is really sucky for the user when a table has a billion rows
 and the query is SELECT * FROM. That approach, however, understands
 that it is not a single user world. In the real world hundreds of
 users will be adding/deleting/modifying records. Each situation has to
 decide how they want to monitor and update the model.
 There are various rules in the documentation as to when Qt will
 update the database. Call submitAll() for each change and remove
 all doubt.
 I put a huge block of comments here because Qt models, at least from
 what I've seen well and truly suck when the primary key is an
 auto-increment integer. You physically have to write to the database
 to get the next key value.
 The ultimate insult here is that
 This is a constant problem with Qt's design and its developers. When you
 work on real computers with massive distributed databases, you understand
 one query could take as long as half an hour, especially with a "BigData"
 database where many spindles are spun down. This is why banks and other
 institutions make you submit a query when you want something from outside
 the tiny amount they keep online. Later in the day you get a message
 saying your report is ready.
 Adding further insult to injury, the models are busted.
 removeRow called for  3
 result of removeRow()  false
 Error:  "near \"WHERE\": syntax error Unable to execute statement"
 query:  "select * from questions;"
 Every table modification fails with that exact error and you can't dump the
 query to see just where the problem really is.
 void SQLiteModel::deleteRow(int rowNo)
 if 0
 qDebug() << "removeRow called for " << rowNo << "\n"; bool rslt = removeRow(rowNo); qDebug() << "result of removeRow() " << rslt << "\n"; qDebug() << "Error: " << lastError().text() << "\n"; qDebug() << "query: " << query().lastQuery() << "\n"; submitAll();
 QSqlRecord rec = record(rowNo); QSqlQuery query(db); query.prepare("DELETE FROM questions WHERE Q_NO = :rowNo ;"); query.bindValue(":rowNo", rec.field("Q_NO").value().toInt()); bool rslt = query.exec(); qDebug() << "Result of removing row: " << rslt << "\n"; defaultQuery();
 void SQLiteModel::copyRow(int rowNo)
 if 0
 QSqlRecord rec = record(rowNo); QSqlRecord rec2 = record(); rec2.setValue("Q_TEXT", rec.field("Q_TEXT").value().toString()); bool rslt = insertRecord(rowCount(), rec2);  // negative appends to end qDebug() << "result of inserting row: " << rslt << "\n"; qDebug() << "Error: " << lastError().text() << "\n"; qDebug() << "query: " << query().lastQuery() << "\n"; if (rslt) {     qDebug() << "submitting changes\n";     submitAll(); }
 qDebug() << "copyRow called for " << rowNo << "\n"; QSqlRecord rec = record(rowNo); QSqlQuery query(db); query.prepare("INSERT INTO questions (Q_TEXT) VALUES(:txt);"); query.bindValue(":txt", rec.field("Q_TEXT").value().toString()); bool rslt = query.exec(); qDebug() << "Result of copying row: " << rslt << "\n"; defaultQuery();
 void SQLiteModel::updateRow(int rowNo, QString txt)
     qDebug() << "updateRow called for " << rowNo << "  with: " << txt << "\n";
 if 0
 QSqlRecord rec = record(rowNo); rec.field("Q_TEXT").setValue(txt); bool rslt = setRecord(rowNo, rec); qDebug() << "result of update: " << rslt << "\n"; qDebug() << "Error: " << lastError().text() << "\n"; qDebug() << "query: " << query().lastQuery() << "\n"; if (rslt) {     submitAll(); }
 QSqlRecord rec = record(rowNo); QSqlQuery query(db); query.prepare("UPDATE questions SET Q_TEXT = :txt WHERE Q_NO = :rowNo ;"); query.bindValue(":txt", txt); query.bindValue(":rowNo", rec.field("Q_NO").value().toInt()); bool rslt = query.exec(); qDebug() << "Result of removing row: " << rslt << "\n"; defaultQuery();
 p, li { white-space: pre-wrap; } 
 #include <QGuiApplication>
 #include <QQmlApplicationEngine>
 #include <QQmlContext>


 #include "sqlitemodel.h"

 int main(int argc, char *argv[])

     QGuiApplication app(argc, argv);

     QScopedPointer<SQLiteModel> sqmodel( new SQLiteModel(nullptr));

     QQmlApplicationEngine engine;
     const QUrl url(QStringLiteral("qrc:/main.qml"));
     QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                      &app, [url](QObject *obj, const QUrl &objUrl) {
         if (!obj && url == objUrl)
     }, Qt::QueuedConnection);



     QList<QObject *> objs = engine.rootObjects();

     if (!objs.empty())
         QObject *root = objs[0];
         QObject::connect( root, SIGNAL(deleteRow(int)), sqmodel.get(), SLOT(deleteRow(int)));
         QObject::connect( root, SIGNAL(copyRow(int)), sqmodel.get(), SLOT(copyRow(int)));
         QObject::connect( root, SIGNAL(updateRow(int, QString)), sqmodel.get(), SLOT(updateRow(int, QString)));

     return app.exec();
import QtQuick 2.15
 import QtQuick.Window 2.15
 import QtQuick.Controls 1.4
 import QtQuick.Controls 2.15
 import QtQuick.Controls.Material 2.15
 import Qt.labs.qmlmodels 1.0
 import Fred 1.0
     id: root
     title: qsTr("SQL Demo")
     width: 640
     height: 480
     visible: true
 minimumWidth: 640 minimumHeight: 480 signal deleteRow(int rowNo) signal copyRow(int rowNo) signal updateRow(int rowNo, string txt) TableView {     id: myView     anchors.fill: parent     TableViewColumn{ role: "Q_NO"  ; title: "Primary Key" ; width: 80 }     TableViewColumn{ role: "Q_TEXT"  ; title: "Question" ; width: 400 }     model: MyModel     Menu     {         id: contextMenu         property int rowNo: 0         property string rowTxt         MenuItem         {             text: qsTr('Delete')             onClicked:             {                 root.deleteRow(contextMenu.rowNo)             }         }         MenuItem         {             text: qsTr('Edit')             onClicked:             {                 console.log("Edit clicked for row " + contextMenu.rowNo)                 promptQuestion.rowNo = contextMenu.rowNo                 promptQuestion.rowTxt = contextMenu.rowTxt                 promptQuestion.visible = true             }         }         MenuItem         {             text: qsTr('Copy')             onClicked:             {                 console.log("Copy clicked for row " + contextMenu.rowNo)                 root.copyRow(contextMenu.rowNo)             }         }         MenuItem         {             text: qsTr('Cancel')             onClicked:             {                 console.log("Cancel clicked for row " + contextMenu.rowNo)             }         }     }     rowDelegate: Item     {         Rectangle         {             anchors             {                 left: parent.left                 right: parent.right                 verticalCenter: parent.verticalCenter             }             height: parent.height             color: styleData.selected ? 'gray' : styleData.row % 2 ? 'white' : 'cornsilk'             MouseArea             {                 anchors.fill: parent                 acceptedButtons: Qt.RightButton                 onClicked:                 {                     mouse.accepted = false                     if (mouse.button == Qt.RightButton)                     {                         console.log("caught right button")                         mouse.accepted = true                         contextMenu.rowNo = model.row                         contextMenu.rowTxt = MyModel.getQuestion(model.row )                         contextMenu.popup()                     }                 }             }         }     } } Dialog {   id: promptQuestion   title: "Edit Question"   standardButtons: Dialog.Ok | Dialog.Cancel   modal: true   property int rowNo: 0   property string rowTxt   TextField   {       id: userTxt       text: promptQuestion.rowTxt   }   onAccepted:   {       console.log("Accepted")       root.updateRow(rowNo, userTxt.text )       promptQuestion.close()   }   onRejected:   {       promptQuestion.close()   } }


Too many of you will take this code and paste it into your own projects without even looking at it. You really need to read the comments found in sqlitemodel.cpp at lines 150 through 194.

I didn’t know the models were this busted because I never use them. I only use QML at gunpoint and even then I have to think about it.

The models are a failed architecture because they must run in the main event loop. Never do significant I/O in the main event loop. Yeah, this works nice with a few dozen records. Probably work nice with a few thousand on a fast machine that has plenty of RAM for disk caching and no other users needing to do I/O.

If your database is accross the Internet where you could have upwards of 20 second network timeouts your UI is locked up for that entire time. If your source data is stored on something like an IBM Data Cell;

Or its modern day replacement, the tape library

Tape library robot in action

your UI could be locked up for over half an hour waiting its turn in the I/O request queue. You don’t know the definition of “Big Data” until you are writing applications designed to use that amount of data. Despite what you may think, there are a lot of these still around. Chapel Hill didn’t retire Roberta until 2019.

Roberta at Chapel Hill

The “Data Appliance” which replaced Roberta isn’t going to be oceans faster. Faster, yes. Oceans faster, not really. Has to do with the compressing and de-duplicating.

Here is a tape robot library that just got installed in 2019.

The extra capacity needed to be at least 60 petabytes (PB) – the equivalent of storing more than 10 million DVDs – with the potential to increase again to meet future data demands.

From the above link
The Minimum You Need to Know About Qt and Databases
Roland Hughes started his IT career in the early 1980s. He quickly became a consultant and president of Logikal Solutions, a software consulting firm specializing in OpenVMS application and C++/Qt touchscreen/embedded Linux development. Early in his career he became involved in what is now called cross platform development. Given the dearth of useful books on the subject he ventured into the world of professional author in 1995 writing the first of the "Zinc It!" book series for John Gordon Burke Publisher, Inc. A decade later he released a massive (nearly 800 pages) tome "The Minimum You Need to Know to Be an OpenVMS Application Developer" which tried to encapsulate the essential skills gained over what was nearly a 20 year career at that point. From there "The Minimum You Need to Know" book series was born. Three years later he wrote his first novel "Infinite Exposure" which got much notice from people involved in the banking and financial security worlds. Some of the attacks predicted in that book have since come to pass. While it was not originally intended to be a trilogy, it became the first book of "The Earth That Was" trilogy: Infinite Exposure Lesedi - The Greatest Lie Ever Told John Smith - Last Known Survivor of the Microsoft Wars When he is not consulting Roland Hughes posts about technology and sometimes politics on his blog. He also has regularly scheduled Sunday posts appearing on the Interesting Authors blog.

Leave a Reply