Programming

Latest posts — page 2

Comparing Two Floating-Point Numbers

The mathematician Leopold Kronecker is believed to have said: God made the integers, all else is the work of man. And Kronecker didn't even know the floating-point numbers "made" for computers. Comparing two numbers of type

QCOMPARE with Custom Qt Types

When you write unit tests, you will have to compare the actual value and the expected value. A simplified example with QStrings would look like this. void MyTest::testQCompare() { auto actualStr = QString{"abba"}; auto expectedStr = QString{"juhu&

Printing Custom Qt Types with qDebug()

When you define your own C++ types for a Qt application, you want to print their values with qDebug(), qWarning() or qCritical() eventually. auto frame1 = QCanBusFrame{0x18ef0201U, QByteArray::fromHex("018A010000000000")}; qDebug() << "frame1 =" <<

Speaking CAN: Write Buffer Overflow

In the first post of the Speaking CAN series, we learned how the terminal sends a read-parameter request to the ECU and how the ECU sends a response with the value of the parameter back to the terminal. This works

Tremendous Speed-Up with SQL Transactions

The function CustomerDatabase::importCustomers reads 500 customers from the list c_customers, creates an SQL query cmd for each customer and inserts each customer into the Customers table of the SQL database db. void CustomerDatabase::importCustomers() { auto db = QSqlDatabase::database(

Monitoring Sys Files with QFileSystemWatcher

When the driver turns off the ignition in a vehicle, display computers like terminals in harvesters or infotainment systems in cars should be notified. The display computers should save important data and shut down orderly. When the display computer runs

QML Engine Deletes C++ Objects Still In Use

Correction: In the original post, I stated that ownership is transferred from C++ to QML by READ functions of Q_PROPERTYs. This is wrong. Ownership is only transferred by Q_INVOKABLE functions and slots, which return a QObject pointer. I

Converting a QByteArray into an Unsigned Integer

My program received messages from a network as an array of bytes. The sender of the messages encoded integers in Big Endian format. So, the hex number 0x1540 arrived in the sequence [0x15, 0x40]. I wrote the following function, which

New in Qt 5.10: Dynamic Language Change in QML

My favourite new feature in Qt 5.10 is the inconspicuous function QmlEngine::retranslate(). Finally, seven years after QML's birth, there is a Qt way to change the language of your application at runtime. There is no need