Qt Tutorial

CSIE 梁祐承@2017醒獅團

Application with GUI ?

  • GTK (C)
  • Javafx (Java)
  • Qt (C++)
  • ...

About Qt

  • cross platform
  • open source
  • support multi language
    • PyQt
  • it's cute(?)

"Qt"怎麼唸

\Qt/  \Pyhton/  \我的C/ \fufu/

不是這樣

我不是希派 我只是個南推

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

ハラショー

Qt不唸"Q"+"t"
正確唸法是 "cute"

我都唸costco, 可是有人唸costco
所以還是唸Qt吧

Qt Creator

Qt Creator

  • help you design GUI

 

  • elements drag and drop

 

  • instant preview

Installation

  • https://www.qt.io/
  • Qt 5.7 recommand (for Program Design II)
  • you'll need to specific the compiler you want yo use

When you have problems

Getting start - Pushbutton

Open Forms -> mainwindow.ui

and add a pushbutton

Right click the pushbutton, and select "Go to slot..."

and choose "clicked()"

Write some code in the method(e.g. "cout"), and run the program to see what happend !

How to interact with other GUI elements?

Add another element

Object name

Use button to modify it

Click

Signals and Slots

  • Not standard C++ function
  • Use Macros
  • To use signals and slots, your object must be a derived class of "QObject" and use the "Q_OBJECT" macro.

降り注ぐ シグナルを躰で感じて

Signals

  • connect to slots
  • when "emit" a signal, all connected slots will be called.
  • is a function prototype

Slots

  • connect to signals
  • can be called by signals, or use like a normal function.
  • return value only available when use as a normal function.

Prepare

  • 繼承 QOBject
  • 加入Q_OBJECT macro

加入自訂slot

  • 在public slots宣告method
  • 需要定義
  • 可當作一般method使用
  • 可以有return值,但只有當作一般method使用時才有意義

 

testsignalslot.h
testsignalslot.cpp

和MainWindow中的pushbutton做連結

加入自訂signal

  • 在signals宣告
  • 是function prototype
  • 不需要定義

 

  • 使用方式:emit signal
testsignalslot.h

和MainWindow中的slot做連結

Try It!

Parameters

  • Signals and slots can have parameters too.

 

 

 

 

  • Notice that the compiler will not check whether the signal/slot exist, if you use not exist ones, will cause runtime error.
connect(testClass, SIGNAL(counter_changed(int)), this, SLOT(testClassChanged(int)));

Advanced

(optional)

"connect" support many different syntaxs, if you're interested, you can visit official Qt documentation.

About delay

what is the result if you use "sleep()" in your program?

Ans:

Your GUI will freeze.

《Inferno》

冰   炎   地   獄

Solution?

QTimer

  • Use signals and slots
  • Will not block your program
  • See Qt doc

Practice

  • Add a QTimer to your program
  • Connect the signal "timeout()" to your slot
  • Start the timer with interval(in ms)

I use some Qt Objects

(e.g. QString), but "cout"

cannot print it out !

QDebug

  • can print out Qt Classes use operator<<
  • #include <QDebug>
  • usage: qDebug() << something
    • will print newline automatically

Thank you !

  • Bullet
  • Bullet

Qt Tutorial

By Liang Yu-Cheng

Qt Tutorial

  • 1,432