Post

Learning Qt and QML

In this post, I would be talking about Qt topics that I’ve found useful and tricky to understand. Also, I would like to write about some C++ tricks, as well.

Qt starting recipe

  1. Include the QApplication or QGuiApplication
  2. Second, We need a class inheriting from QWidget. (Alternatively you could use a mainwindow, so include a QMainWindow)
  3. Third, you can use old practice of working with this QWidget, or you can create and work with a .ui file.(and play with this in the Qt Designer). To create a .ui file, do:
    1. create a new file named something, e.g. foo.ui
    2. in your .h file of QWidget file, add these two:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      
        namespace Ui{
        class mainWindow;
        }; 
        class mainWindow: QWdiget /*or QMainwindow*/{
            mainWindow(QWidget *parent = nullptr)
            ...
            private:
                Ui::mainWindow ui;
        }
      
    3. In the corresponding implementation cpp file:
      1
      2
      3
      4
      5
      
       #include "./ui_foo.h"
       ...
       mainWindow::mainWindow(QWidget *parent) : QWidget(parent){
           ui.setupUi(this);
       }
      

Including .ui file:

  • first read this getting started
  • You have basically two options:
    1. adding testingUI.ui file directly into your main.cpp file To do this, you need to write:
      1
      2
      3
      4
      5
      6
      7
      8
      
        #include "ui_testingUI.h" // not an actual header file
        int main(){
         ...
         Ui::testingUI ui;
         ui.setupUi(new Qwidget());
         ui.show();
         ...
        }
      
    2. Adding some code to the .ui file, (i.e. making it intercatible) and then add it to the main.cpp file!

A critical Error in QT-creator

  • When you initialize **any project** in QT, there will be some errors. What you need to do is just unsetting CMAKE_PROJECT_INCLUDE_BEFORE. WEIRD!!!

SQL databases in Qt

  • Firstly read this: https://doc.qt.io/qt-6/sql-programming.html
  • add sql qt package to cmakelists.txt
  • for using QSQLITE, use this:
    1
    2
    3
    
     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation).append("/generatedData.db");
     db.open();
    
  • for executing query on it, do this:
    1
    2
    
      QSqlQuery query;
      query.exec("SQL_COMMANDS");
    
  • For modifying records in a table (e.g. ContainerJobs), you can use 2 options:
    • Through query:
      • To use a binding variable in the query, use this command:
        1
        2
        3
        4
        
         query.prepare("insert into ContainerJobs (QC, indx, verticalLoc, Type) values (:QC, :indx, :verticalLoc, :Type)");
         query.bindValue(":QC", "m1");
         ...
         query.exec();
        
    • Through QSqlTableModel sqlmod (I like this more!)
      • To do this: You first need a QTableView in your QWdiget or ui code/file for adding the QSqlTableModel to it as its model (setmodel)
      • then you need a QSqlTableModel m = QSqlTableModel(this, QSqlDatabase), remember, this should a QObject, hence, QWidgets are ok with it.
      • Then you need a QSqlRecord rec for adding records to m
      • Then for adding fields (or columns), you need to use QSqlField and for adding a specific type, equivalent to what is defined in the table of the original of the data base, you need to add the following code:
        1
        
        rec.append(QSqlField("QC", QMetaType::fromType<QString>())); 
        
      • finally, after adding all the columns to the record, you need to setValue(0,"m1");, first argument denoting as the n-th field.
      • then you just need to sqlmod.insertRecord(1,rec);, first argument denoting the n-th record on the table.
      • Fianlly, you need to do sqlmod.submitAll(); and sqlmod.transaction(); and sqlmod.commit(); and QTableView::setModel(sqlMod);

        Singleton design and “correct” way of using of QSqlDatabases

  • read this very important topic on qt forum (link)

Debugging tips:

  • Remember to check the Locals in the Views option of the debugger to view the variables and user defined objects.
  • Be careful about #include guards. Remember to add #pragma once in the beginning of the .h files to avoid double inclusion.
  • Alternatively, you can do this:
    1
    2
    3
    4
    
       #ifndef BBDEFS_H
       #define BBDEFS_H
       ...
       #endif // BBDEFS_H
    

Running Qt project outside QtCreator

To do so, you have two options (according to this stackoverflow):

  1. copy all the dlls into your build directory (tedious!)
  2. just add the dlls to the PATH environment variable. (they are often located in bin)
  3. Or, for non visual studio projects, you use the windeployment of the Qt.

C++ Tips

  • For copying: one object or resource into another (possibly with different types), use move semantics which is related to rvalue refrences for performance optimization.
This post is licensed under CC BY 4.0 by the author.