Table of Contents

Get Start on C++ Qt Combo Developemnt

Problems and Solutions

Problem: QtCreator debug works but release not work

Problem: QtCreator generated executable binary won't run on its own

Problem: QtCreator build release error HostX86\x64\cl.exe“ is used by qmake but HostX64\x64\cl.exe” is configured in the kit.

Get Start on Coding with C++ and Qt

1. Simple Start with QtCreator

  1. Create a new empty project, and delete everything in project list and only keep
    • ProjectName.pro : the project config file
    • Sources/main.cpp : the main program
  2. simple code
    main.cpp
    #include "QApplication.h" // note: in for Qt stuff, you can also use #include <QApplication> format
    #include "QLabel.h"
     
    //#include <QtWidgets> // note: use this in Qt5, <QtGui> in Qt4, with this, 
    //with above line, you don't need include each single element, just like in Python
     
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QLabel *info_label = new QLabel("Hello Qt!");
        info_label->show();
        return app.exec();
    }
  3. change Left panel : Build : into Release mode; that way, your output Executable binary should be only 10kb instead of 500kb big size. since Debug build mode will create bigger output.
  4. after output binary, if you want to pass to your friend, you need put extra library next to it to run it standalone. (as mentioned in above “Problem and Solution” part)

1.1 Concept of Static and Dynamic linking

2. Work like PyQt/PySide

Python Example vs C++ Example

MyBox.py
from PyQt4 import QtGui, QtCore
import sys
 
class MyBox(QtGui.QMainWindow):
 
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
 
        main_widget = QtGui.QWidget()
        self.setCentralWidget(main_widget)
        self.setWindowTitle("C++ Welcome!")
 
        main_layout = QtGui.QHBoxLayout()
        main_widget.setLayout(main_layout)
 
        info_label = QtGui.QLabel("Hello Qt!")
        quit_btn = QtGui.QPushButton("Click Me!")
        cust_btn = QtGui.QPushButton("Custom Print!")
        main_layout.addWidget(info_label)
        main_layout.addWidget(quit_btn)
        main_layout.addWidget(cust_btn)
 
        # connection
        QtCore.QObject.connect( quit_btn, QtCore.SIGNAL("clicked()"), self.close )
        QtCore.QObject.connect( cust_btn, QtCore.SIGNAL("clicked()"), self.cust_action )
 
    def cust_action(self):
        print("Hello C++ from Python")
 
def main():
    app = QtGui.QApplication(sys.argv)
    mybox = MyBox()
    mybox.show()
    sys.exit(app.exec_())
 
if __name__ == "__main__":
    main()
mybox.cpp
#include <QtWidgets>
#include "mybox.h"
 
MyBox::MyBox(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget *main_widget = new QWidget;
    this->setCentralWidget(main_widget);
    this->setWindowTitle("Python Welcome!");
 
    QHBoxLayout *main_layout = new QHBoxLayout;
    main_widget->setLayout(main_layout);
 
    QLabel *info_label = new QLabel("Hello Qt!");
    QPushButton *quit_btn = new QPushButton("Click Me!");
    QPushButton *cust_btn = new QPushButton("Custom Print!");
    main_layout->addWidget(info_label);
    main_layout->addWidget(quit_btn);
    main_layout->addWidget(cust_btn);
 
    // connection
    QObject::connect(quit_btn, SIGNAL(clicked()), this, SLOT(close()));
    QObject::connect(cust_btn, SIGNAL(clicked()), this, SLOT(cust_action()));
 
}
 
MyBox::~MyBox()
{
 
}
 
void MyBox::cust_action(){
    qDebug() << "Hello C++";
}


main.cpp
#include "mybox.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyBox mybox;
    mybox.show();
 
    return app.exec();
}


mybox.h
#ifndef MYBOX_H
#define MYBOX_H
 
#include <QMainWindow>
 
class MyBox : public QMainWindow
{
    Q_OBJECT
 
public:
    MyBox(QWidget *parent = 0);
    ~MyBox();
 
public slots:
    void cust_action();
};
 
#endif // MYBOX_H

3. Manual and Portable Workflow with only Qt C++ Library and C++ Compiler

Intro to Portable

Tools You Need

Files You need

Environment setting You Need

Check and Test Build result in release folder