Review:
1. QT Compilation
Qmake
*. Pro File Format
2. QT tools
Help documentation
Configuration tools
Uidesigner
3. Use of QT classes
I. Signal and slot
1. Background:
Process-oriented
Low coupling design between modules (High Cohesion ).
Function call:
Direct call
Callback call
Object-oriented
Low coupling design between modules (High Cohesion ).
Object call
Direct call
Interface call
Qt:
Signal and slot solutions:
Object member variable call?
Call of member functions of an object?
Case:
Form, which has a text box.
Thread, change the text box every second.
Problem:
It is troublesome to access the form component of the thread class and must be passed.
In QT, use the thread Program It may not work.
2. Use signals and slots
The biggest benefit is that objects are called directly without passing parameters.
And transfer data between objects
2. 1. The header and implementation must be separated.
2. qobject must be inherited
. The called function becomes a slot
. Call the function to become a signal
. A macro in qobject must be introduced into the class
. The implementation is exactly the same as that of C ++.
. The return values of signals and slots must be void
. The associated signal must be consistent with the slot prototype, and the name can be different.
View code
//Log. h# Ifndef log_h# DefineLog_h# Include<Qobject>ClassLog:PublicQobject//The definition slot function must inherit the qobject{Q_objectPublicSlots:VoidLog ();//Define a slot function. The slot function is the called function.};# Endif
View code
//Log. cpp# Include <log. h># Include<Iostream>Using NamespaceSTD;VoidLog: log ()//Implementation of Slot Functions{Cout<"Call log!"<Endl ;}
View code
//Biz. h# Ifndef biz_h# DefineBiz_hClassBiz:PublicQobject {q_objectPublic:VoidBiz ();//Use this function to call the siglog function. The siglog function is a signal function.Public: Signals:VoidSiglog ();//Define the signal function. The signal function is the function that calls the slot function.}# Endif
View code
//Biz. cpp# Include <iostream># Include<Biz. h>Using NamespaceSTD;VoidBiz: Biz ()//Implement the biz function and call the signal function in the function{Sleep (1); Cout<"Tackle biziness!"<Endl; emit siglog ();//Call of Signal Functions}
View code
// main. CPP # include
#include
int
main () {log; biz biz; qobject: connect (
//
connection signal and slot &
biz, signal (siglog ()), &
log, slot (log () biz. biz ();
///
call biz }
Thoughts:
What are the advantages of object coupling between signals and slots?
3. QT visualization components (controls)
How is the signal sent?
The signal is automatically sent.
Case:
Use the button signal.
Send a signal when a button event occurs.
Event> signal> slot
Signal and slot solve the following problem: how to call a user's function when an event occurs?
New class:
Qmessagebox provides a set of static functions dialog box
Steps:
1. Main. cpp
2. *. Pro
3. Dialog Box class
4. Implement the corresponding slot function of the button clicked Signal
The slot function must be the same as the clicked signal,
In which class is the slot function defined? Slot functions are placed into the class with the most access members
5. Bind signals and slots
View code
// Mydialog. h # Ifndef my_dialog_h # Define My_dialog_h # Include <Qdialog> # Include <Qpushbutton> Class Mydialog: Public Qdialog {q_object Private : Qpushbutton * BTN; Public : Mydialog (qwidget * Parent =Null ); ~ Mydialog (); Public Slots: Void Showbox ();}; # Endif View code
// Mydialog. cpp # Include " Mydialog. h " # Include <Iostream> # Include <Qmessagebox> Using STD: cout; mydialog: mydialog (qwidget * Parent) {resize ( 400 , 300 ); BTN = New Qpushbutton ( This ); BTN -> Resize ( 100 , 30 ); BTN -> Move ( 150 ,150 ); BTN -> Settext ( " OK " ); Connect (BTN, signal (clicked ()), This , Slot (Showbox ();} mydialog :: ~ Mydialog () {Delete BTN ;} Void Mydialog: Showbox () {qmessagebox: Information ( This , " Information " , " This is maomaochong! " );} View code
//Main. cpp# Include <qapplication># Include"Mydialog. h"IntMain (IntS,Char**V) {qapplication app (S, V); mydialog DLG; DLG. setvisible (True);ReturnApp.exe C ();}
Case 2:
Adder
1. design the interface (*. UI, *. h)
2. Main. cpp
3. Pro
4. Dialog Box
5. processing signals and slots
After the interface is designed, save it as a frmdlg. UI file. Then, in the folder corresponding to the terminal
UIC frmdlg. UI-O frmdlg. h
Generate the header file of the interface.
View code
// Dlgjfq. h # Ifndef dlg_jfq_h # Define Dlg_jfq_h # Include <Qdialog> # Include " Frmjfg. h " Class Dlgjfq: Public Qdialog {q_object Private : Ui_dlgjfq * Ui; Public : Dlgjfq (qwidget * Parent = Null ); ~ Dlgjfq (); Public Slots: Void Add ();}; # Endif View code
// Dlgjfq. cpp # Include " Dlgjfq. h " Dlgjfq: dlgjfq (qwidget * Parent) {UI = New Ui_dlgjfq; UI -> Setupui ( This ); Connect (ui -> Btnadd, signal (clicked ()), This , Slot (add ();} dlgjfq :: ~ Dlgjfq () {Delete UI ;} Void Dlgjfq: add (){ // String Qstring stradded = UI-> txtadded-> Text (); qstring stradd = UI-> txtadd-> Text (); // Convert to integer Int IA = Stradded. toint (); Int IB = Stradd. toint (); // Computing and Int Ic = Ia + Ib; // Display and convert to text in the label box UI-> lblresult-> Settext (qstring: Number (IC ));} View code
//Main. cpp# Include <qapplication># Include"Dlgjfq. h"IntMain (IntARGs,Char**Argv) {qapplication app (ARGs, argv); dlgjfq DLG; DLG. Show ();ReturnApp.exe C ();}
View code
//Main. ProTemplate =Appsources=Main. cpp \ dlgjfq. cpp Headers=Frmjfg. h \ dlgjfq. h config=Release qtqt=Core guitarget= Main
Ii. Inheritance structure of QT classes
Qt class
Core
Data Set
Qstring
Ry
Qpoint
Qsize
Qrectangle
System Type
Qcolor
Qfont
Qimage
Qicon
Qcursor
Qbitmap
...
Gui
Form class
|-Framework form qmainwindow (SDI/MDI) [menu/toolbar/status bar/with document and view]
|-Dialog Box form qdialog [mode dialog box/non-mode]
Components
Display Components
Input component
Button
Container
Menu
Layout Components
Advanced data components
Table/tree
Iii. Structure of form classes
Qobject qpaintdevice
| ------ | ------- |
Qwidget
|
| -------------------- | ------------------ |
Qmainwindow qdialog Control
Qobject:
Non-static
Connect
Disconnect
Static
Connect
Disconnect
Tr
Maintain the relationship between sub-objects
Timer event
Signal:
Destroy.
Qpaintdevice (all visualization controls are plotting devices)
Visual component height () width ();
Screen Resolution and other related information
Qwidget
General Attributes
Geometric Attributes:
Icon
Cursor
Title
Basic events (drawing/mouse events/Keyboard Events)
Job:
1. Read the random number program.
Note: thread Encapsulation
Signal and slot
2. Write the exchange rate calculator
3. Thinking:
Features and advantages of signal and slot.