3
رــي ض  ت  إعداـــــ ـ  ب ل د ـ  ح أ / وـــــــــــــــ كاـــــــس أسا ــ ي غ  ــ  ل ـىـقودس ل د ـ  /

Arabic Basics of Qt

Embed Size (px)

Citation preview

Qt / /

() () () () ()

1

( ) Cross-Platform ( ) windows ( ) Mac OSX ( ) Linux ( ) Symbian ()Windows Mobile ( .) Embedded Linux ++ ( ) Compiling . . : .

++

2

. . . : C++ Object Oriented Programming ( Qt Nokia SDK Software ) ................................................................................

3

2 8 9 12 14 17 20 23 24 27 28 30 32 34 36 39 41 43 44 50 52 53 56 58 60 62 65

.............................................................................................................. ............................................................................................. ............... ) QtCore Module ( ............................................................................................ QDebug ........................................................................................ QBitArray ..................................................................................... QByteArray ............................................................................................. QString ...................................................... ) Container Classes ( ..................................................................................... QList ................................................................................ QVector ................................................................................ QQueue .................................................................................. QStack .......................................................................... QStringList ...................................................................................... QSet .................................................................................... QMap .......................................................................... QMultiMap .............................................................. ) Iterator Classes ( .......................................................................... Java Style Iterator .......................................................................... STL Style Iterator ........................................................................... foreach Keyword ................................................................................................. QDir .......................................................................................... QFileInfo ................................................................................................. QFile .................................................................................... QTextStream ................................................................................... QDataStream .......................................................................................... QVariant ........................................................................................... QObject

4

38 68 301 401 801 311 121 131 831 141 441 941 251 351 451 161 961 271 371 771 781 591 502 712

( ........... ) QtGui Module ........................................................................................... QWidget ........................................................................................... QPainter ............................................................................................ QDialog ............................................................................... QMainWindow ( ..................................................... ) Layout Managment ( ...................................................... ) Drag and Drop - ( .................................................... ) Model / View .......................................................................... ................................................................. .......................................................... ( ....................................... ) Graphics View Framework .................................................................. QGraphicsScene ................................................................... QGraphicsView .................................................................... QGraphicsItem ( ............................................. ) Animation Gui ( ......................... ) QNetwork Module ................................................................................. QHostAddress ........................................................................................ QHostInfo ...................................................................................... QTcpServer ...................................................................................... QTcpSocket .................................................................................... QUdpSocket (................. ) QtSql Module ( ............................ ) Multithreaded Programming ................................................................................

5

822 922 132 532 632

( 1 ) ( .................................... ) Memory Managment ( 2 ) ++......................................................... C ( 3 ) .................................................................... ( 4 ) ................................ QtCreator ( 5 ) ................................................... QObject ................................................................................

6

QDebug Example QBitArray Example QByteArray Example QString Example QList Example QVector Example QQueue Example QStack Example QStringList Example QSet Example QMap Example QMultiMap Example Java_Iterator Example STL_Iterator Example foreach Example QFileInfo Example QFile Example QTextStream Example QDataStream Example QVariant Example QObject Example QWidget Example Car GUI Example QPainter Example QDialog Example QMainWindow Example

Examples Name

Page EX:No Examples Name13 15 18 21 25 27 29 31 33 35 37 40 46 48 50 54 57 58 60 62 73 87 94 101 105 109 EX_1 EX_2 EX_3 EX_4 EX_5 EX_6 EX_7 EX_8 EX_9 EX_10 EX_11 EX_12 EX_13 EX_14 EX_15 EX_16 EX_17 EX_18 EX_19 EX_20 EX_21 EX_22 EX_23 EX_24 EX_25 EX_26

Layout Example Drag_Drop Example ListView Example TableView Example TreeView Example Graphics Example Animation GUI Example QHostInfo Example QHostAddress Example QTcpServer Example QTcpSocket Example QUdpSender Example QUdpReceiver Example Databse Example QThread Example

Page EX:No116 125 138 141 144 155 164 174 174 179 189 196 200 211 222 EX_27 EX_28 EX_29 EX_30 EX_31 EX_32 EX_33 EX_34 EX_34 EX_35 EX_36 EX_37 EX_38 EX_39 EX_40

7

( ) Modules ( ) Classes . ( ) Modules :

. . GUI .MultiMedia . .OpenGL .OpenVG .Qt Scripts .DataBases .SVG .Web content .XML . .Windows ActiveX Control Server .Windows ActiveX

QtCore QtGui QtMultimedia QtNetwork QtOpenGL QtOpenVG QtScript QtSql QtSvg QtWebKit QtXml Phonon

Module

Windows OS UNIX OSQtDBus QAxContainer QAxServer

.Inter-Process Communication

: : .QtNetwork Module , QtSql Module , QtGui Module

8

QtCore Module

9

QtCore Module

>#include QtCore> int main)( { qDebug) Hi Qt Developers ! ( ;

QDebug Example

EX

Am NO ple 1

qDebug)( >> Hi Qt Developers !! again ; qDebug)( >> 10+20 = >> 10+20 ; }

: ) Console (

qDebug) Hi Qt Developers ! (;

) Console ( Hi Qt Developers ! . Hi Qt Developers !! again . 30 = 10+20

qDebug)( >> Hi Qt Developers !! again; qDebug)( >> 10+20 = >> 10+20;

................................................................................

13

QtCore Module

: : ( ) Bits 10 .True or False ( : ) Declaration :

QBitArray Class

;(8)QBitArray x x 8 ( ) Bits 0 False 1 True : ;(QBitArray x)8,true : x .true or false (at)int x true . ()isNull true 0. ()isEmpty x .true (setBit)int x ( setBit)int x, bool t x t t .true or false

;QBitArray x .x.isNull)( ---> will return true .x.isEmpty)( ---> will return true

isNull , isEmpty :

;(0)QBitArray x .x.isNull)( ---> will return false .x.isEmpty)( ---> will return true QtCore Module

41

#include >QtCore> int main)( { QBitArray x) 8 (; QBitArray y) 8 , true (; QBitArray r) 8 (; x.setBit) 0 (; x.setBit) 1 (; x.setBit) 2 (; x.setBit) 3 (; x.setBit) 4 (; y.setBit) 0 , false (; y.setBit) 1 , false (; y.setBit) 2 , false (; r = x & y; r = x | y; r = x ^ y; r = ~x; r = y;

QBitArray ExampleEX Am NO ple 2

}

: QBitArray x) 8(; QBitArray y) 8 , true (; QBitArray r) 8 (; x.setBit) 0 (; x.setBit) 1 (;0|0|0|0|0|0|0|0 1|1|1|1|1|1|1|1 0|0|0|0|0|0|0|0 1|0|0|0|0|0|0|0 1|1|0|0|0|0|0|0

8 x 8 y 8 r True x 0 True x 1

15

QtCore Module

;( 2 )x.setBit0|0|0|0|0|1|1|1

2 x True 3 x True 4 x True 0 y False 1 y False 2 y False x , y: 1|1|1|1|1|0|0|0 X0|0|0|1|1|0|0|0 1|1|1|1|1|1|1|1 1|1|1|0|0|1|1|1 1|1|1|0|0|0|0|0 1|1|1|1|1|0|0|0

;( 3 )x.setBit ;( 4 )x.setBit ;( y.setBit) 0 , false ;( y.setBit) 1 , false

0|0|0|0|1|1|1|1

0|0|0|1|1|1|1|1

1|1|1|1|1|1|1|0

1|1|1|1|1|1|0|0

;( y.setBit) 2 , false

1|1|1|1|1|0|0|0

1|1|1|1|1|0|0|0

r = x & y; >-- AND Operator r = x | y; >-- OR Operator r = x ^ y; >-- XOR Operator ;r = ~x ;r = y >-- NOT Operator >-- Equal Operator

Y

= r = r = r = r = r

QtCore Module

61

: : ( ) Bytes 0 652 . ( : ) Declaration : ;( QBitArray x) Test x 4 . T,E,S,T ;( QBitArray x) 5 , T x 5 . T,T,T,T,T : .x x . x . L .P . L . L . (at)int x (left)int x (right)int x (mid)int P , int L ()trimmed (chop)int L (truncate)int L

QByteArray Class

................................................................................

71

QtCore Module

#include >QtCore> int main)( { QByteArray ar) Hello (; QByteArray br) 5 , Q (; qDebug)( >> ar.trimmed)(; ar = ar.trimmed)( + br; qDebug)( >> ar[ 0 ]; qDebug)( >> ar.at) 1 (; qDebug)( >> ar.left) 2 (; qDebug)( >> ar.right) 7 (; qDebug)( >> ar.mid) 3 , 4 (; ar.chop) 7 (; ar.truncate) 2 (; }

QByteArray ExampleEX Am NO ple 3

QByteArray ar) Hello (; QByteArray br) 5 , Q (;

: 01 ar 5 br Hello H|e|l|l|o|Q |Q |Q |Q |Q

H|e|l|l|o| | | | |

Q|Q|Q|Q|Q

qDebug)( >> ar.trimmed)(; ar = ar.trimmed)( + br; qDebug)( >> ar[ 0 ];

= ar

. 0 H

QtCore Module

18

;( 1 )qDebug)( >> ar.at ;( 2 )qDebug)( >> ar.left ;( 7 )qDebug)( >> ar.right

e 1 . He 2 . loQQQQQ 7 .

;( 4 , 3 )qDebug)( >> ar.mid loQQ 4 3 . ;( 7 )ar.chop ;( 2 )ar.truncate 7 . 2 .H|e|l

= ar = ar

H|e

................................................................................

91

QtCore Module

: : ( ) Strings 61 ( ) UniCode . ( : ) Declaration

QString Class

;( QString str) string ;QString str ;str = string

: . . . str . (append)str ( insert)int p , str str .p ( remove)int P ,int L L .p (2 replace)str1,str 2 str 1.str True .str (contains)str True .str (startsWith)str True .str (endsWith)str ( setNum)number number .

QString . QtCore Module

02

#include

QString ExampleEX Am NO ple 4

int main() { QString str( Hello Qt ); str += Developer; str.append( 2010 ); str.insert( 0 , Hello !! ); str.remove( 6 , 3 ); str.replace( 2010 , 2011 ); qDebug() > str.endsWith) 11 (; qDebug)( >> str.setNum) 2010.11 (;

.Hello Hello Qt Developer 2011 str .Dev str True .Hello str True .Hi str False .10 str False .11 str True . 11.0102

qDebug)( >> QString::number) 10 , 2 (; .)Binary 0101 01 2 ( qDebug)( >> QString::number) 10 , 16 (; .)Hexadecimal 01 61 ( a QString strarg) Hi %1 %2 (; qDebug)( >> strarg.arg) Qt , Developer (; Developer Qt 1% , 2% Hi Qt Developer .Function Arguments QtCore Module

22

. ( ) ( )Iterator Classes . : T >QList>T ( )Pointers ( ) Data . ( ) Data >QVector>T ( ) Pointers . QList .FIFO >QQueue>T QVector .LIFO >QStack>T >QSet>T . ( )Key (. )T >QMap>Key,T ( )key,T . > QMultiMap>Key,T QMap ( )Key (.)T > QHash>Key,T QMap QHash . > QMultiHash>Key,T QHash .

(:)Container Classes

................................................................................

32

QtCore Module

: : ( ) Container Classes ( ). ( : ) Declaration :

QList Class

;QList name ;EX: QList strlist ( ) QString strlist ( ). (>>). ;2EX: strlist 7 >> 6; .)int( mylist No 0 | 1 | 2 | 3 | 4 | 5 | 6 . Value 0 | 1 | 2 | 3 | 5 | 7 | 6 mylist.append) 8 (; No 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 . 8 Value 0 | 1 | 2 | 3 | 5 | 7 | 6 | 8

:

mylist.insert) 4 , 4 (;No 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 Value 0 | 1 | 2 | 3 | 4 | 5 | 7 | 6 | 8

.4 4 .6 7

mylist.swap) 7 , 6 (;No 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 Value 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

25

QtCore Module

;( 8 )mylist.removeAt7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 No 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 Value

8.

;()mylist.pop_back ;()mylist.pop_front ;( 7 )mylist.push_back

6 | 5 | 4 | 3 | 2 | 1 | 0 No 6 | 5 | 4 | 3 | 2 | 1 | 0 Value 5 | 4 | 3 | 2 | 1 | 0 No 6 | 5 | 4 | 3 | 2 | 1 Value 6 | 5 | 4 | 3 | 2 | 1 | 0 No 7 | 6 | 5 | 4 | 3 | 2 | 1 Value

. .

7 .

;( 1 )qDebug)( >> mylist.indexOf 0 1 0. ;( 3 )qDebug)( >> mylist.value ;( 5 , 1 )mylist.move6 | 5 | 4 | 3 | 2 | 1 | 0 No 7 | 2 | 6 | 5 | 4 | 3 | 1 Value

4 3. 1 5.

;( 0 )mylist.prepend7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 No 7 | 2 | 6 | 5 | 4 | 3 | 1 | 0 Value

0 .

;()qDebug)( >> mylist.size ;( 0 )qDebug)( >> mylist.count

8 . 1 0 . QtCore Module

62

: : QList . ( 52 ). ( : ) Declaration : ;QVector name ;EX: QVector strlist strlist ( ) QString ( ) (>>). ;2EX: strlist > mystack.pop)(; mystack.push)Mahmoud(;

:

.QString mystack .Ahmed , Osama , sami . sami .Ahmed , Osama . Osama . Ahmed . Mahmoud .Ahmed , Mahmoud

31

QtCore Module

: QList : QList . : >QList>). ;mylist Samier ; .QStringList ) QString( mylist .Osama , Mohamed , Ali , Samier mylist.sort)(; .Ali,Mohamed,Osama,Samier : QStringList mylist2; QString str)Mahmoud,Ali,Said,Sami,Mohamed(; . )QString( mylist2 .QString str mylist2 = str.split) , (; . , str = mylist2.join) - (; .- . Mahmoud-Ali-Said-Sami-Mohamed: str

:

33

QtCore Module

: : . ( : ) Declaration ;QSet name ;EX : QSet myset (>>). ;4 > 5; 1 myset . myset QSet>int> myset2; myset2 >> 5 >> 7 >> 2 >> 9 >> 8; .myset = )1,2,3,5( and myset2 = )5,7,2,8,9( qDebug)( >> myset.intersect) myset2 (; .myset2 myset myset .myset = )2,5( and myset2 = )5,7,2,8,9( qDebug)( >> myset2.subtract) myset (; .myset2 myset myset2 myset = )2,5( and myset2 = )7,8,9( qDebug)( >> myset.unite) myset2 (; .myset2 myset myset2 myset = )2,5789( and myset2 = )7,8,9(

:

35

QtCore Module

: : ()pairs ( ) ( )Key , Value . QMultiMap ( : ) Declaration ;QMap name ;EX : QMap mymap mymap : ( ) ( ). : : (key)value v (keys)value v ()keys (value)key k ()values (take)key k ()count

QMap Class

;(1,mymap.insert)good ;0 = ]mymap[bad

.v .v . .k . k .k .

................................................................................

QtCore Module

63

#include int main() { QMap mymap; mymap[hadi] = 2; mymap[sami] = 2; mymap.insert( ali , 1 ); mymap.insert( hadi , 1 ); qDebug() mymap.keys ;()qDebug)( >> mymap.keys ;( qDebug)( >> mymap.value) sami ;()qDebug)( >> mymap.values

ali 1. ali , hadi 1. .ali , hadi , sami 2 .sami 211.

;( qDebug)( >> mymap.take) ali 1 ali :hadi sami

Key

Value1 2

................................................................................

QtCore Module

83

: QMap : QMap . ( : ) Declaration ;QMultiMap name ;EX : QMultiMap mymap mymap : ( ) ( ). : : (values)key k (count)key k

QMultiMap Class

;( 1 , mymap.insert) good ;0 = ]mymap[bad

. QMap .k .k

................................................................................

93

QtCore Module

#include int main() { QMultiMap myMmap,myMmap1,myMmap2; myMmap1.insert( ali , 1 ); myMmap1.insert( sami , 1 ); myMmap1.insert( mohamed , 1 ); myMmap2.insert( sami , 2 ); myMmap2.insert( ali , 3 ); myMmap2.insert( sami , 1 ); myMmap2.insert( ali , 4 ); myMmap = myMmap1 + myMmap2; }

QMultiMap Example

EX Am NO ple 12

QMultiMap >QString , int > myMmap,myMmap1,myMmap2; . mymap,mymap1,mymap2 myMmap1.insert)ali , 1 (; myMmap1.insert)sami , 1 (; myMmap1.insert)mohamed , 1 (; myMmap2.insert)sami myMmap2.insert)ali myMmap2.insert)sami myMmap2.insert)ali , 2 (; , 3 (; , 1 (; , 4 (;Key ali mohamed sami Key ali ali sami sami

:

maymap1

Value 1 1 1 Value 4 3 1 2

maymap2

myMmap = myMmap1 + myMmap2;

Key Value ali 4 ali 3 ali 1 mohamed 1 sami 1 sami 2 sami 1 QtCore Module

mymap

40

.) STL - Style -Iterators (, ) Java-Style- Iterators ( : :)Java Style Iterator( .STL :)Iterator Classes( Containers QList>T>, QQueue>T> QLinkedList QVector>T>, QStack>T> QSet>T> QMap>Key, T>, QMultiMap>Key, T> QHash>Key, T>, QMultiHash>Key, T> Read-only iterator QListIterator QLinkedListIterator>T> QVectorIterator>T> QSetIterator>T> QMapIterator>Key, T> QHashIterator>Key, T> Read-write iterator QMutableListIterator QMutableLinkedListIterator QMutableVectorIterator>T> QMutableSetIterator>T> QMutableMapIterator>Key, T> QMutableHashIterator>Key, T>

:)Iterator Classes(

:)STL Style Iterator( 0.2 . :)Iterator Classes( Containers QList>T>, QQueue>T> QLinkedList QVector>T>, QStack>T> QSet>T> QMap>Key, T>, QMultiMap>Key, T> QHash>Key, T>, QMultiHash>Key, T> Read-only iterator QList>T>::const_iterator QLinkedList>T>::constiterator QVector>T>::const_iterator QSet>T>::const_iterator QMap>Key, T>::const_iterator QHash>Key, T>::const_iterator Read-write iterator QList>T>::iterator QLinkedList>T>::iterator QVector>T>::iterator QSet>T>::iterator QMap>Key, T>::iterator QHash>Key, T>::iterator

QList . ................................................................................ 41QtCore Module

: : .Java - Style - Iterator ( : ) Declaration ;(QListIterator name)QList :EX ;QList mylist ;(QListIterator iter)mylist mylist .QString iter .mylist : . .STL

QListIterator Class

A

B

C

D

E

( .) next

A

B

C

D

E

( )next (.)previous QtCore Module

24

A

B

C

D

E

(.)previous true . true . . . . . . . () hasNext () hasPrevious () next () previous () peekNext () peekPrevious () toBack () toFront

................................................................................

34

QtCore Module

STL Style Iterator;Container class::iterator name :EX ;QList mylist ;QList::iterator iter mylist .QString iter .mylist : . . ( : ) Declaration

A

B

C

D

)(end

(++).

A

B

C

D

)(end

(++) (--). QtCore Module

44

A

B

C

D

)(end

(--) ( () ) end . . ().end . . . n . n . iterator .j *iterator () end () begin ++ iterator -- iterator iterator += n iterator -= n iterator - j

................................................................................

54

QtCore Module

#include int main() { QList mylist; mylist Mohsen >> Sami; . mylist Ali Mohamed

:

mylist

Mohsen

Sami

QListIterator>QString> itr)mylist(; . mylist itr Ali Mohamed

mylist

Mohsen

Sami

while)itr.hasNext)(( qDebug)( >> itr.next)(;

. Mohamed

Ali

mylist

Mohsen

Sami

QtCore Module

46

(()while)itr.hasPrevious ;()qDebug)( >> itr.previous .Ali Mohamed

mylist

Mohsen

Sami

;()qDebug)( >> itr.peekNext ( )Ali .Ali Mohamed

mylist

Mohsen

Sami

;()itr.toBackmylist

.Ali Mohamed Mohsen Sami

;()qDebug)( >> itr.previous ( )Sami .Ali Mohamed

mylist

Mohsen

Sami

................................................................................

74

QtCore Module

#include int main() { QList mylist; mylist > Sami; . mylist Ali Mohamed Mohsen

:

mylist

Sami

end)(

QList>QString>::iterator itr; . 0 mylist itr Ali Mohamed Mohsen

mylist

Sami

end)(

for)itr = mylist.begin)( ; itr != mylist.end)( ; itr++( qDebug)( >> *itr; .) end )( ( Ali Mohamed Mohsen

mylist

Sami

end)(

QtCore Module

48

--itr;Ali Mohamed Mohsen

mylist

. Sami end)(

qDebug)( >> *)itr(; qDebug)( >> *)--itr(;Ali Mohamed

.) Sami ( .) Mohsen ( Sami end)(

Mohsen

mylist

qDebug)( >> *)--itr(;Ali Mohamed

.) Mohamed ( Mohsen

mylist

Sami

end)(

qDebug)( >> *)++itr(;Ali Mohamed

Mohsen

mylist

.) Mohsen ( Sami end)(

*)itr( = Modified;

.) Modified ( Mohamed Modified

Ali

mylist

Sami

end)(

................................................................................

49

QtCore Module

: foreach switch while for ( ) . ... : foreach )variable , container(#include int main() { QList mylist; mylist > Ali >> Omar; . QString mylist foreach ) QString liststr , mylist ( qDebug)( >> liststr;

:

.QString liststr . mylist liststr foreach QtCore Module

50

;QMap>QString , int> mymap ;1 = ]mymap[ahmed ;2 = ]mymap[mohamed ;3 = ]mymap[osama mymap QString .int ( ()foreach ) QString mapstr , mymap.keys ;qDebug)( >> mapstr

mapstr . QString foreach mapstr mymap .

( ()foreach ) int mapint , mymap.values ;qDebug)( >> mapint

mapint .int foreach mapint mymap .

forever : {forever ... } } { . ................................................................................

15

QtCore Module

: : ( / ) ( \ ) . . .1: QDir)/home/user/Documents( use in mac or linux .2: QDir)C:/Documents and Settings( use in windwos ( / ) ( \ ) .C:\Documents and Settings ;(QDir name)dir name ;(EX: QDir mydir)C:\\windows ( : ) Declaration

QDir Class

: .dir name .dir name .dir path . .attribute (cd)dir name (mkdir)dir name (mkpath)dir path ()absolutePath (entryInfoList)atrribute

................................................................................

QtCore Module

25

: : . QFileInfoList QFileInfo >.QList>QFileInfo ;(QFileInfo name)filepath/filename ;(EX: QDir mydir)c:\\mypath\\myfile.txt ( : ) Declaration

QFileInfo Class

: .dir name .dir name .dir path . .attribute (cd)dir name (mkdir)dir name (mkpath)dir path ()absolutePath (entryInfoList)atrribute

. QDir, QFileInfo , QFileInfoList . ................................................................................

35

QtCore Module

#include

QDir , QFileInfo ExampleEX Am NO ple 16

int main() { QDir mydir; QFileInfoList flist; mydir.cd(mydir.currentPath()); qDebug() > d } .

55

QtCore Module

: QIODevice : . ;(QFile myfile)myfile.txt ( : ) Declaration : ( option ). length . length. . data . (open)option (read)length (seek)length ()readLine (write)data

QFile Class

................................................................................

QtCore Module

65

#include int main() { QFile File(test.txt);

QFile ExampleEX Am NO ple 17

File.open(QIODevice::ReadWrite | QIODevice::Text); File.write( Hi Qt Developer !!\n ); File.seek( 0 ); qDebug() > File.readLine)(; File.close)(; . Hi Qt Developer !! . 0 . .

57

QtCore Module

: : QIODevice ) Text ( QTextStream . QString QByteArray . QIODevice QFile : ) Declaration ( QTextStream mystream)QIODevice(; QTextStream mystream)QByteArray(; QTextStream mystream)QString(;

QTextStream Class

: . (>>) (>>) QTextStream ExampleEX Am NO ple 18

#include int main() { QFile File(test.txt); File.open(QIODevice::ReadWrite | QIODevice::Text); QTextStream fstr( &File ); fstr fstr ;( 0 )fstr.seek ;()qDebug)( >> fstr.readLine ;()qDebug)( >> fstr.readLine fstr .File !! Hi Qt Developer . 56 . 0 .

:

................................................................................

readLine .

95

QtCore Module

: : QIODevice QDataStream QByteArray . QIODevice QFile : ) Declaration ( QDataStream mystream)QIODevice(; QDataStream mystream)QByteArray(;

QDataStream Class

: . (>>) (>>) QDataStream ExampleEX Am NO ple 19

#include int main() { QFile File(test.txt); File.open(QIODevice::ReadWrite); QDataStream fdata(&File); fdata > str >> a ; qDebug() QString)Hi Qt Developer ;56 >> fstr

;( 0 )fdata.device)(->seek 0 . () fdata.device QFile . seek ;QString str ;int a ; fdata >> str >> a ; qDebug)( >> str >> a (>>) .

................................................................................

16

QtCore Module

: : . : ) Declaration ( QVariant myvar)any data type(; : . QVariant ExampleEX Am NO ple 20

QVariant Class

#include int main() { QVariant v( 100 ); int x = v.toInt() + 10; QString str = v.toString(); double z = v.toDouble() * 0.2234; qDebug() -- Q_PROPERTY :public --> ;(... ,constructor)QObject *parent --> ;()setter functions --> ;()getter functions --> :private ;variables ..... :protected ... :Q_SLOTS ;slots functions :Q_SIGNALS ;signals function } --> --> setter .getter --> >-- Q_SLOTS --> >-- Q_SIGNALS -->

QObject Class

07

. Q_OBJECT ( ) meta object compiler QObject ( .) moc ( ; ) : (Q_CLASSINFO)Programmer , Ali (Q_CLASSINFO)about , drawing : ( ) Setter Function ( .) Getter Function Q_PROPERTY .Setter , Getter : ( Q_PROPERTY) Var_datatype propertyName READ Getter WRITE Setter

Q_CLASSINFO

Q_PROPERTY

class classname : QObject { Q_OBJECT (Q_PROPERTY)int num_pro READ number WRITE setNumber :public ()int number ;(void setNumber)int :private ;int num ;}

( setProperty) propertyName , Variable Value public , private , protected .

Q_SLOTS & Q_SIGNALS

Q_OBJECT . 17QObject Class

Q_SIGNALS and Q_SLOTS : ( ) Signal Function ( .) Slot Function ( ) Signal Function ( .) Signal Function : .Q_SIGNALS ( .Q_EMIT ) Signal Function : .Q_SLOTS : ;((connect) obj1 , SIGNAL)signal function( , obj2 , SLOT)slot function1 ObjectFunction )(doSomething Q_SIGNALS )(sendThing Function )(doSomething { ....... ;))(Q_EMIT(sendThing }

;) ) )(connect ( Object1 , SIGNAL ( sendThing() ) , Object2 , SLOT ( accessRecv

2 ObjectQ_SLOTS )(accessRecv

: Container Watcher .QObject Class

27

#include >QObject> #include >QStringList> class container : public QObject { Q_OBJECT public: explicit container)QObject *parent = 0(; void addItem)QString(; Q_SIGNALS: void ItemAdded)QString(; public Q_SLOTS: private: QStringList strlist; }; #include container.h container::container)QObject *parent( : QObject)parent( { }

container.h>-->-->-->-->-->-->-->-->--container.cpp

.QObject container EX Am NO ple 21

QObject container Q_OBJECT

strlist Q_SIGNALS QString Q_SLOTS strlist

>---

void container::addItem)QString str( >--- strlist { this->strlist >> str; >--- strlist str Q_EMIT)ItemAdded)str((; >--- Q_EMIT } str ItemAdded Q_EMIT .addItem . ItemAdded

73

QObject Class

#include >QObject> class watcher : public QObject { Q_OBJECT public: explicit watcher)QObject *parent = 0(; Q_SIGNALS: public Q_SLOTS: void printstr)QString(; };

watcher.h

.QObject watcher

>-->---

Q_SLOTS

Q_SLOTS printstr .connect ) signal function ( watcher.cpp

#include watcher.h #include >QDebug> watcher::watcher)QObject *parent( : QObject)parent( { }

void watcher::printstr)QString str( { qDebug)( >> new Item was added to Container : >> str; } : printstr . printstr str : . QString : QObject Class

74

#include >QtCore/QCoreApplication> #include container.h #include watcher.h

main.cpp

.QObject watcher

int main)int argc, char *argv[]( { QCoreApplication a)argc, argv(; container *cont = new container; >--- cont container watcher *watch = new watcher; >--- watch watcher QObject::connect)cont , SIGNAL)ItemAdded)QString(( , watch , SLOT)printstr)QString(((; cont->addItem)Mohamed 1(; cont->addItem)Mohamed 2(; cont->addItem)Mohamed 3(; return a.exec)(;

}

connect) cont , SIGNAL) ItemAdded)QString( ( , watch , SLOT) printstr)QString( ( (; cont ItemAdded .watch printstr : .strlist cont addItem Q_EMIT ItemAdded . watch printstr connect .) ( : new Item was added to Container : Mohamed 1 new Item was added to Container : Mohamed 2 new Item was added to Container : Mohamed 3

75

QObject Class

:signals and slots ( ) signal ( ) slot ( ) ( ) . : --> (signal function : setdata)int 1 int 1 slot function : execdata)int( >--- int . : . : . : .(setdata)int (setdata)int,QString (setdata)int,QString ()setdata (setdata)int (setdata)int,int (setdata)int ()setdata

Signals

Slots (execdata)int (execdata)QString,int (execdata)int,QString ()execdata ()execdata(setdata)int (setdata)int,int

(execdata)int

connect

.QObject : . . .QObject Class

67

Inherits car Macros , Constructors and Destructors ClasName

QObject

VariablesLevel private private private private private

Level NON NON NON NON NON public public

Type Macro Macro Macro Macro Macro Contructor Contructor Type QString QString int int bool

Q_OBJECT Q_CLASSINFO(Author , QT-Developer) Q_PROPERTY(QString carname READ name WRITE setName); Q_PROPERTY(QString carmodel READ model WRITE setModel) Q_PROPERTY(int carspeed READ speed WRITE setSpeed) explicit car(QObject *parent = 0); explicit car(QString name, QObject *parent = 0); car_name; car_model; car_speed; car_maxspeed; car_enginestatus; Name Info

Function member

Setters and Getter Function membersLevel public public public public public Setters Functions Type Function void setName(QString); void setModel(QString); void setMaxSpeed(int); void StartEngine(); void StopEngine(); Level public public public public public public

Getters Functions Type Function QString name(); QString model(); int speed(); int maxSpeed(); bool isEngineOn(); QString EngineStatus(); Q_SIGNALS Type Function void SpeedChanged(int); void Enginetoggled(bool); void MaxSpeed(int); :

Slots and Signals Level Q_SLOTS

Q_SLOTS Type Function void setSpeed(int);

Level Q_SIGNALS Q_SIGNALS Q_SIGNALS

77

QObject Class

#include >QObject> class car : public QObject { Q_OBJECT Q_CLASSINFO)Author , QT-Developer( Q_PROPERTY)QString carname READ name WRITE setName( Q_PROPERTY)QString carmodel READ model WRITE setModel( Q_PROPERTY)int carspeed READ speed WRITE setSpeed( public: explicit car)QObject *parent = 0(; explicit car)QString name, QObject *parent = 0(; void setName)QString(; void setModel)QString(; void setMaxSpeed)int(; Setters void StartEngine)(; void StopEngine)(; QString name)(; QString model)(; int speed)(; Getters int maxSpeed)(; bool isEngineOn)(; QString EngineStatus)(; Q_SIGNALS: void SpeedChanged)int(; void Enginetoggled)bool(; void MaxSpeed)int(; public Q_SLOTS: void setSpeed)int(; private: QString car_name; QString car_model; int car_maxspeed; int car_speed; bool car_enginestatus; };

:) car.h (

}}

}

}

}}

}

QObject Class

78

Q_OBJECT Q_CLASSINFO

: . Q_OBJECT moc .QObject

: (Q_CLASSINFO)Author , QT-Developer (Q_CLASSINFO)Web , www.carclass.net Q_PROPERTY ( ) Variable Type ( ) Getter Function ( .) Setter Function : (Q_PROPERTY)DataType PropertyName READ GetterFunction WRITE SetterFunction . Q_PROPERTY : (Q_PROPERTY)QString carname READ name WRITE setName (Q_PROPERTY)QString carmodel READ model WRITE setModel (Q_PROPERTY)int carspeed READ speed WRITE setSpeed Q_SIGNALS Q_SIGNALS void . classname.cpp . Q_EMIT Q_SLOTS Q_SLOTS .Q_SIGNALS

................................................................................

97

QObject Class

#include car.h car::car)QObject *parent( : QObject)parent( { this->car_speed = 0; this->car_enginestatus = false; }

car ) car.cpp (

car::car)QString name, QObject *parent(:QObject)parent( { this->car_speed = 0; this->car_enginestatus = false; this->car_name = name; } void car::setName)QString name( { this->car_name = name; } void car::setModel)QString model( { this->car_model = model; } void car::setMaxSpeed)int max( { this->car_maxspeed = max; Q_EMIT MaxSpeed)max(; }

}

} } }

}

: Setter

: Setter

: Setter max

>------------ MaxSpeed Q_EMIT

QObject Class

80

void car::setSpeed)int speed( { if)speed > 0(speed = 0; if)speed > this->car_maxspeed( speed = this->car_maxspeed; : Setter if)this->car_speed == speed(return; if)!this->isEngineOn)((speed=0; this->car_speed = speed; Q_EMIT Q_EMIT SpeedChanged)speed(; >----------------------------- SpeedChanged speed } void car::StartEngine)( { if)this->car_enginestatus == true( return; this->car_enginestatus = true; Q_EMIT Enginetoggled)this->car_enginestatus(; } void car::StopEngine)( { if)this->car_enginestatus == false( return; else{ this->setSpeed)0(; this->car_enginestatus = false; Q_EMIT Enginetoggled)this->car_enginestatus(; } } QString car::name)( { return this->car_name; } QString car::model)( { return this->car_model; }

}

}

: Setter

81

QObject Class

} }

}

>------ Enginetoggled car_enginestatus

Q_EMIT

: Setter

Q_EMIT >------ Enginetoggled car_enginestatus

: Getter

: Getter

()int car::maxSpeed { ;return this->car_maxspeed } ()int car::speed { ;return this->car_speed } ()bool car::isEngineOn { ;return this->car_enginestatus }

} } }

: Getter

: Getter

: Getter bool : true , false

signals and slots QObject slots .Q_SLOTS : ;(>void on__)>signal parameters ;()EX : void on_PushBotton_clicked connectSlotsByName . 28 QObject Class

()QString car::EngineStatus { ;QString status ;if)this->isEngineOn)(( status = Engine On ;else status = Engine Off ;return status }

}

: Getter QString

QtGui Module

38

QtGui Module

>#include -------> mywidget mywidget

QWidget Class

;( QWidget mywidget2) &mywidget 2 mywidget mywidget . mywidget parent children . QtGui Module

68

#include >QtGui/QApplication> #include >QWidget> #include >QLabel> #include >QLineEdit> #include >QCheckBox> int main)int argc, char *argv[]( { QApplication a)argc, argv(; QWidget wid; QLabel lbl; QLineEdit line; QCheckBox box; wid.show)(; lbl.show)(; line.show)(; box.show)(; return a.exec)(; }

main.cpp

EX Am NO ple 22

. : QWidget wid QLabel lbl

QCheckBox box QLineEdit line

87

QtGui Module

>#include >QtGui/QApplication >#include >QWidget >#include >QLabel >#include >QLineEdit >#include >QCheckBox (][int main)int argc, char *argv { ;(QApplication a)argc, argv ;QWidget wid ;(QLabel lbl)&wid ;(QLineEdit line)&wid ;(QCheckBox box)&wid ;()wid.show ;()return a.exec }

lbl,line,box wid wid .wid . Qt Designer signals and slots . Qt Designer QWidget ( ) setGeometry ( ) show , hide . QWidget Qt Creator . QtGui Module

88

.Qt Creator - New File or Project .File

- Qt c++ Project Qt Gui Application .Project

- .test

98

QtGui Module

- : class information QWidget . base class

Run . Qt Creator .Project |---test.pro |---Forms | |----widget.ui | |---Headers | |----widget.h | |---Source |----main.cpp |----widget.cpp Qt Creator: ---> ---> >---Qt Designer ---> ---> ---> QtGui Module

09

Qt Creator ( ) Folder test . : test.pro ( ) Modules ( ) gui , network . : widget.ui . : main.cpp . : widget.h .widget : widget.cpp .widget build all : widget.ui ui_widget.h .uic uic .namespace moc QObject .moc_widget.cpp . : ( .) moc_widget.cpp , ui_widget.h .widget.h , widget.cpp , main.cpp 19 QtGui Module

>#include >QtGui/QApplication #include widget.h (][int main)int argc, char *argv { ;(QApplication a)argc, argv ;Widget w ;()w.show } ;()return a.exec

main.cpp Main ------> ------> Widget >------ QWidget . ------> ------>

#ifndef WIDGET_H #define WIDGET_H >#include >QWidget { namespace Ui ;class Widget } class Widget : public QWidget { Q_OBJECT :public ;(0 = explicit Widget)QWidget *parent ;()~Widget :private ;Ui::Widget *ui ;} #endif // WIDGET_H

widget.h : : namespace .ui_widget.h : ;(* setupUi)QWidget ;(retranslateUi)QWidget *Widget setupUi : Ui::Widget retranslateUi ................................................. : .QWidget

ui Ui::Widget QtGui Module

29

#include widget.h #include ui_widget.h : (Widget::Widget)QWidget *parent ,(QWidget)parent (ui)new Ui::Widget { ;(ui->setupUi)this } ()Widget::~Widget { ;delete ui }

widget.cpp

.

ui_widget.h ( . )class Ui_Widget { :public { (void setupUi)QWidget *Widget ...... } Ui_Widget retranslateUi , setupUi : setupUi . : retranslateUi .

;}

{ (void retranslateUi)QWidget *Widget ....... } Ui::Widget Ui_Widget . retranslateUi , setupUi Ui::Widget widget.h

{ namespace Ui ;}{ class Widget: public Ui_Widget }

39

QtGui Module

signals and slots . car QObject : main.cpp -----> car.h car.cpp >----- car >----- car

car_gui QWidget . car_gui.h >----- car_gui car_gui.cpp >----- car_gui ( ) EX_QObject_car .Qt Designer:ObjectName namelbl :ObjectName modelbl :ObjectName enginestatuslbl :ObjectName speedlbl :ObjectName enginebtn

8 QLabel Class

QToolButton Class

:ObjectName speeddail

QDail Class

QtGui Module

49

#ifndef CAR_GUI_H #define CAR_GUI_H #include >QWidget> #include car.h namespace Ui { class car_gui; } class car_gui : public QWidget { Q_OBJECT public: explicit car_gui)QWidget *parent = 0(; ~car_gui)(; car *toyota; public Q_SLOTS: void setCarSpeed)int(; void setCarEngine)bool(; private: Ui::car_gui *ui; }; #endif // CAR_GUI_H

car_gui.h

>-------- car >-------- Ui::car_gui

}

: .

}

EX Am NO ple 23

car_gui

95

QtGui Module

#include car_gui.h #include ui_car_gui.h #include car.h

car_gui.cpp

car_gui::car_gui)QWidget *parent( : QWidget)parent(, ui)new Ui::car_gui( { QString str; ui->setupUi)this(; toyota = new car)this(; toyota->setProperty)carname , TOYOTA(; toyota->setModel)2005(; toyota->setMaxSpeed)200(; ui->speeddial->setMaximum)toyota->maxSpeed)((; ui->namelbl->setText)toyota->name)((; ui->modelbl->setText)toyota->property)carmodel(.toString)((; ui->enginestatuslbl->setText)toyota->EngineStatus)((; str.setNum)toyota->speed)((; ui->speedlbl->setText)str(; connect)toyota , SIGNAL)SpeedChanged)int((, this , SLOT)setCarSpeed)int(((; connect)toyota ,SIGNAL)Enginetoggled)bool((,this,SLOT)setCarEngine)bool(((; connect)ui->speeddial,SIGNAL)valueChanged)int((,toyota,SLOT)setSpeed)int(((; connect)ui->enginebtn,SIGNAL)clicked)bool(( , this,SLOT)setCarEngine)bool(((;

}

car_gui::~car_gui)( { delete ui; }QtGui Module

96

void car_gui::setCarSpeed)int speed( { QString str; str.setNum)speed(; ui->speedlbl->setText)str(; ui->speeddial->setValue)speed(; } void car_gui::setCarEngine)bool status( { if )status == true( { toyota->StartEngine)(; ui->enginestatuslbl->setText)toyota->EngineStatus)((; ui->enginebtn->setText)Stop Engine(; } else { toyota->StopEngine)(; ui->enginestatuslbl->setText)toyota->EngineStatus)((; ui->enginebtn->setText)Start Engine(; } } .signals and slots ) connect (

car classQ_SIGNALS SpeedChanged)int( Enginetoggled)bool( Q_SLOTS setSpeed)int(

car_gui classQ_SIGNALS ui->speeddial::valueChanged)int( ui->enginebtn:: clicked)bool( Q_SLOTS setCarSpeed)int( setCarEngine)bool(

97

QtGui Module

car_gui QObject Signals and Slots connectSlotsByName : slot void on_>object name>_>signal name>)>signal parameters>(; : class car_gui : public QWidget { ..... public Q_SLOTS: ..... void on_mycarname_SpeedChanged)int(; on_mycarname_SpeedChanged)int( Q_SLOTS ... car mycarname } car_gui::car_gui)QWidget *parent( :QWidget)parent(,ui)new Ui::car_gui( { .... .car mycarname toyota = new car)this(; toyota->setObjectName)mycarname(; ... SpeedChanged connect // connect)toyota , SIGNAL)SpeedChanged)int((, this , SLOT)setCarSpeed)int(((; .... QMetaObject::connectSlotsByName)this(; } void car_gui::on_toyota_SpeedChanged)int i ( { setCarSpeed) i(; }

: car_gui.h

: car_gui.cpp

. on_>object name>_>signal name>)>signal parameters>(.

QtGui Module

}

connect .

98

QWidget ( .) Events : QWidget ( : ) Event Function ( ) drag and drop . : 1- ( ) class.h .protected 2- ( ) implement code ( .) class.cpp (* .keyPressEvent)QKeyEvent .shift 1- : class.hclass classname { ..... :protected ;(* void keyPressEvent)QKeyEvent ;}

(void classname::keyPressEvent)QKeyEvent *key { (if)key->key)( == Qt::Key_Shift { .....do somthing } }

2- : class.cpp

99

QtGui Module

( : ) Event Filter Function . : 1- installEventFilter . 2- ( ) class.h .protected 3- ( ) implement code ( .) class.cpp : 1- :class.cpp(..)classname:: classname { ;(ui->textEdit->installEventFilter)this } textEdit

class classname bool void ( ) { 1 ..... true false :protected ;(* bool eventFilter)QObject *, QEvent ;} { (bool classname::eventFilter)QObject *obj, QEvent *event textEdit . { (if )obj == ui->textEdit KeyPress { (if )event->type)( == QEvent::KeyPress ;(QKeyEvent *ke = static_cast>QKeyEvent *>)event A ;if )ke->key)( == Qt::Key_A( return true true ;else return false A textEdit } false . } ;(return QWidget::eventFilter)this,event } QtGui Module

2- : class.h

3- : class.cpp

001

QWidget QWidget paintEvent QWidget ( Event ) QPainter . QWidget QWidget paintEvent protected widget.h QPainter paintEvent .widget.cpp>#include >QWidget class Widget : public QWidget { Q_OBJECT ...... :protected ;(* void paintEvent)QPaintEvent ...... ;}

widget.h

EX Am NO ple 42

>------ paintEvent

widget.cpp(void Widget::paintEvent)QPaintEvent *p ;(QPainter pp)this ;QPen pen ;(001,001,01)QColor color ;(pen.setColor)color ;(pp.setPen)pen ;()pp.save { >------ paintEvent

pp >------ QPainter -----> pen QPen color >------ QColor -----> color pen ----->-----> pen pp

101

QtGui Module

widget.cpp;(001,001)pp.translate ;(0,001,0,0)pp.drawLine ;(0,001)pp.translate ;(09)pp.rotate ;(0,001,0,0)pp.drawLine ;(0,001)pp.translate ;(09)pp.rotate ;(0,001,0,0)pp.drawLine ;(0,001)pp.translate ;(09)pp.rotate ;(0,001,0,0)pp.drawLine ;()pp.restore ;QBrush br ;(br.setColor)color ;(br.setStyle)Qt::Dense7Pattern ;(pp.fillRect)0,0,100,100,br ----->----->----->----->-----> ( 0 0 ) (001 001) ( 0 0 ) ( 001 0 ) ( 001 0) 09 ( 0 0 ) ( 001 0 )

------> ----->----->----->-----> br QBrush color br br

;(3)pen.setWidth Pen Width 3 ------> ;(pp.setPen)pen >------ pen ;(001,001,0,001)pp.drawEllipse ------> ------> ;(pp.setRenderHint)QPainter::Antialiasing , true ;(001,001,0,002)pp.drawEllipse ------> } Antialiasing .

QPainter .

QtGui Module

201

QPainter Class

: : .QWidget ( : ) Declaration

;( QPainter mypaint) QPaintDevice * device QPainter QPaintDecice QWidget . : : ( : ) Settings ( ) . ( : ) Drawing . ( : ) Rendering Quality .Antialiasing ( :) Coordinate Transformations ( ) Scale ( ) Rotate ( ) Translate ( ) save ( ) restore .

301

QtGui Module

: QWidget : QWidget : ( .) Modal Dialogs , Modeless Dialogs ( : ) Modal Dialogs Accepted .Rejected : ( ) Open File ( .) Ok , Cancel ( :) Modeless Dialogs . : ( ) Find ( ) Replace . ( : ) Declaration . ;(0 = QDialog mydialog) QWidget * parent : () exec Accepted .Rejected Accepted Ok Enter . Rejected Cancel ESC . QtGui Module

QDialog Class

401

: Qwidget Modal Dialog . - QtCreator .widget

:ObjectName

opendlgbtn : newfile or project file Qt Designer Form Class Files And Classes Dialog with Buttons .myDialog

retvallbl

:ObjectName

:ObjectName 501

sendldt

QtGui Module

Qt Creator . ... #include mydialog.h ... class Widget : public QWidget { ..... private slots: void on_opendlgbtn_clicked)(; ... }; >-------widget.h EX Am NO ple 25

on_opendlgbtn_clicked opendlgbtn clicked void Widget::on_opendlgbtn_clicked)( { myDialog mydlg)this(; if)mydlg.exec)( == true( ui->retvallbl->setText)mydlg.retval)((; } >-------widget.cpp

. ) opendlgbtn ( ) clicked ( myDialog mydlg)this(; .myDialog mydlg if)mydlg.exec)( == true( ui->retvallbl->setText)mydlg.retval)((; .cancel false ok true mydlg.exec QtGui Module

106

class myDialog : public QDialog { .. public: ... QString retval)(; ... }; QString myDialog::retval)( { return this->ui->sendldt->text)(; }

>-------mydialog.h

>-------mydialog.cpp

.QLineEdit sndldt retval ................................................................................ .Modal Dialog QDialog Modeless Dialog QDialog : exec)( void Widget::on_opendlgbtn_clicked)( >-------widget.cpp { myDialog *mydlg = new myDialog )this(; mydlg->show)(; } ................................................................................

107

QtGui Module

QMainWindow Class

: QWidget : : .Menu Bar QMenuBar .QWidget .Tool Bar QToolBar .QWidget .Status Bar QStatusBar .QWidget .Dock Widget QDockWidget .QWidget .Central Widget .QWidget

QtGui Module

801

Menu Bar QWidget QMenuBar .QAction QMenuBar Open , Save , Save as File : .new File1 , new File2 New :

actionNew_File1 = new QAction)MainWindow(; actionNew_File2 = new QAction)MainWindow(; actionOpen = new QAction)MainWindow(; actionSave = new QAction)MainWindow(; actionSave_as = new QAction)MainWindow(; menuBar = new QMenuBar)MainWindow(; menuFile = new QMenu)menuBar(; menuNew = new QMenu)menuFile(; MainWindow->setMenuBar)menuBar(; menuBar->addAction)menuFile->menuAction)((; menuFile->addAction)menuNew->menuAction)((; menuFile->addAction)actionOpen(; menuFile->addAction)actionSave(; menuFile->addAction)actionSave_as(; menuNew->addAction)actionNew_File1(; menuNew->addAction)actionNew_File2(;

} }

EX Am NO ple 26

109

QtGui Module

}

Tool Bar QWidget QToolBar QAction . :

actionNew_File1 = new QAction)MainWindow(; actionNew_File2 = new QAction)MainWindow(; actionOpen = new QAction)MainWindow(; actionSave = new QAction)MainWindow(; actionSave_as = new QAction)MainWindow(;

mainToolBar = new QToolBar)MainWindow(; MainWindow->addToolBar)Qt::TopToolBarArea, mainToolBar(; mainToolBar->addAction)actionOpen(; mainToolBar->addAction)actionSave(; mainToolBar->addAction)actionSave_as(; mainToolBar->addAction)actionNew_File1(; mainToolBar->addAction)actionNew_File2(;

Status Bar QWidget QStatusBar showMessage .clearMessage . : QtGui Module

}

}

}

110

Dock Widget QDockWidget QWidget QWidget .MainWindow Qt::LeftDockWidgetArea Qt::RightDockWidgetArea Qt::TopDockWidgetArea Qt::BottomDockWidgetArea : ;)QDockWidget *docw = new QDockWidget)MainWindow ;)MainWindow->addDockWidget)Qt::RightDockWidgetArea , docw Central Widget QWidget QWidget MainWindow Central widget ;(mainwindow->setCentralWidget)QWidget *mywidget : mainwindow ( .SDI ) Single Document Interface ( .MDI ) Multiple Document Interface : SDI QWidget CentralWidget. : MDI QMdiArea CentralWidget. : MainWindows .Qt Designer 111 QtGui Module

( SDI ) Single Document Interface

( MDI ) Multiple Document Interface

QtGui Module

211

Layout Managment

113

Layout Managment

Layout Managment

114

GUI : .Object Geometry .Layout .Size Policy :Object Geometry ( QWidget ) ( ) Pixel . . ( setGeometry ) int x, int y, int w, int h x , y w h ( setMaximumHeight ) int maxh maxh ( setMaximumWidth ) int maxw ( setMinimumHeight ) int minh ( setMinimumWidth ) int minw 511 Layout Managment

maxw minh minw

:Layout : .Horizontal Layout .Vertical Layout .Grid Layout .Form Layout : Horizontal Layout QHBoxLayout . :;QWidget *window = new QWidget >------- window ;(QPushButton *button1 = new QPushButton)One ;(QPushButton *button2 = new QPushButton)Two ;(QPushButton *button3 = new QPushButton)Three ;(QPushButton *button4 = new QPushButton)Four ;(QPushButton *button5 = new QPushButton)Five

;QHBoxLayout *layout = new QHBoxLayout >------- layout ;(1layout->addWidget)button ;(2layout->addWidget)button ;(3layout->addWidget)button ;(4layout->addWidget)button ;(5layout->addWidget)button ;(window->setLayout)layout layout >-------window ;()window->show

}

}

EX Am NO ple 72

Layout Managment

611

: Vertical Layout QVBoxLayout . : QHBoxLayout .QVBoxLayout : Grid Layout QGridLayout . .

;(addWidget ) QWidget * widget, int row, int column

;( addRow ) const QString & labelText, QWidget * field ;( setWidget ) int row, ItemRole role, QWidget * widget

: Form Layout QFormLayout :

Layout Classes .;QHBoxLayout *H1_layout = new QHBoxLayout ;QHBoxLayout *H2_layout = new QHBoxLayout ;QVBoxLayout *V_layout = new QHBoxLayout ;(V_layout ->addLayout)H1_layout ;(V_layout ->addLayout)H2_layout

Layout SizeHint .Size Policy 711 Layout Managment

:Size Policy QSizePolicy : ( setSizePolicy ) QSizePolicy :QSizePolicy ;QSizePolicy sizePolicy sizePlolicy .QSizePolicy ( sizePolicy. setHorizontalPolicy ) Policy policy policy . ( sizePolicy. setVerticalPolicy ) Policy policy policy . ;(sizePolicy.setHorizontalStretch)uchar stretchFactor stretchFactor . ;(sizePolicy.setVerticalStretch)uchar stretchFactor stretchFactor .

policy stretchFactor ................................................................................ Layout Managment

811

:Policies Layout SizeHint .SizeHint SizeHint QSizePolicy::Fixed .SizeHint SizeHint QSizePolicy::Minimum .SizeHint SizeHint QSizePolicy::Maximum .SizeHint SizeHint QSizePolicy::Preferred ( .SizeHint )QWidget SizeHint QSizePolicy::Expanding . SizeHint QSizePolicy::MinimumExpanding . SizeHint QSizePolicy::Ignored .

................................................................................

911

Layout Managment

:stretchFactor ( ) stretchFactor 0 . ( ) stretchFactor X 1 Y 2 Y .X

. Qt Designer............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ ............................................................................................................................................................................................................ Layout Managment

Notes

021

Drag and Drop

121

Drag and Drop

Drag and Drop

221

Drag And Drop ( ) object ( ) mouse ( ) object . : ( .) Drag ( .) Drop .) Multipurpose Internet Mail Extensions ( MIME MIME ( ) ( ) .MIME : . . . Event QWidget ( ) Events .QWidget ................................................................................ 321 Drag and Drop

: QDragEnterEvent . QDragLeaveEvent . QDragMoveEvent . . QDropEvent dragEnterEvent dragLeaveEvent dragMoveEvent dropEvent

MIME ( ) text/plain ( ...) image/jpeg . : MIME /http://www.iana.org/assignments/media-types : : .MIME : MIME . ................................................................................

Drag and Drop

421

.Drag and Drop QLabel . QWidget .Qt Designer

:1 Box :2 Box :3 Box

: .Class : QLabel .ObjectName : label .Class : QLabel .2_ObjectName : label .Class : QLabel .3_ObjectName : label ................................................................................ Drag and Drop

521

#ifndef WIDGET_H #define WIDGET_H #include >QWidget> #include >QLabel> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget)QWidget *parent = 0(; ~Widget)(; QWidget *DragedLabel; >-----QLabel *DropedLabel; >-----private: Ui::Widget *ui;

widget.h

EX Am NO ple 28

QLabel QLabel

protected: void mousePressEvent)QMouseEvent *(; void dragEnterEvent)QDragEnterEvent *(; void dropEvent)QDropEvent *(; }; #endif // WIDGET_H

>------ >------ >------

Drag and Drop

126

#include widget.h #include ui_widget.h #include >QMouseEvent> #include >QStringList> Widget::Widget)QWidget *parent( : QWidget)parent(, ui)new Ui::Widget( { ui->setupUi)this(; this->setAcceptDrops)true(; >------ } Widget::~Widget)( { delete ui; } void Widget::mousePressEvent)QMouseEvent *event( { if )event->button)( == Qt::LeftButton( { DragedLabel = childAt)event->pos)((; if)!DragedLabel( return; if )DragedLabel->inherits)QLabel( == true( { QString labelName = DragedLabel->objectName)( ; QString labelText = static_cast>QLabel*> )DragedLabel(->text)(; QString transferText = labelName + --- + labelText; QDrag *drag = new QDrag)this(; QMimeData *mimeData = new QMimeData; mimeData->setText)transferText(; drag->setMimeData)mimeData(; drag->exec)(;

widget.cpp

}

}

}

127

Drag and Drop

void Widget::dragEnterEvent)QDragEnterEvent *event( { if )event->mimeData)(->hasFormat)text/plain(( event->acceptProposedAction)(; } void Widget::dropEvent)QDropEvent *event( { QStringList strlist; strlist = event->mimeData)(->text)(.split)---(; DropedLabel = this->findChild>QLabel *>)strlist[0](; DropedLabel->setGeometry) event->pos)(.x)(-75,event->pos)(.y)(-25,150,50 (; DropedLabel->setText)strlist[1](; } event->acceptProposedAction)(;

void Widget::mousePressEvent)QMouseEvent *event( . if )event->button)( == Qt::LeftButton(

.

DragedLabel = childAt)event->pos)((; event->pos)( event->pos)( DragedLabel childAt X,Y .( ) if)!DragedLabel( return; . DragedLabel Drag and Drop

128

if )DragedLabel->inherits)QLabel( == true( .QLabel DragedLabel QString labelName = DragedLabel->objectName)( ; QString labelText = static_cast>QLabel*> )DragedLabel(->text)(; QString transferText = labelName + --- + labelText; ( MIME : ) . label_2---Box 2 label_2 QDrag *drag = new QDrag)this(; .QDrag drag

QMimeData *mimeData = new QMimeData; .QMimeData mimeData mimeData->setText)transferText(; .text/plain . mimeData drag->setMimeData)mimeData(; . mimeData mimeData drag->exec)(; . .................................................................................................................. void Widget::dragEnterEvent)QDragEnterEvent *event( . ) ( if )event->mimeData)(->hasFormat)text/plain(( event->acceptProposedAction)(; .text/plain MIME .

129

Drag and Drop

void Widget::dropEvent)QDropEvent *event( . ) ( QStringList strlist; .QStringList strlist

strlist = event->mimeData)(->text)(.split)---(; . event->mimeData)(->text)( : split)---( label_2---Box 2 .) objectname ( strlist[0]= label_2 .) objecttext ( strlist[1]= Box 2 DropedLabel = this->findChild>QLabel *>)strlist[0](; .MIME ) objectname ( DropedLabel DropedLabel->setGeometry) event->pos)(.x)(-75,event->pos)(.y)(-25,150,50 (; . X,Y DropedLabel->setText)strlist[1](; event->acceptProposedAction)(; . .

................................................................................

Drag and Drop

130

/ Model/View programmig

131

Model/View Programming

Model/View Programming

132

/ ( ) Model / View Programming . ( 4.4 ) 4.4 / . . / : .Models .Views .QModelIndexData

Model

Index

View

331

Model/View Programming

Ed i

tin g

( . ) Signals and Slots :Models QAbstractItemModel . : .List Model .Table Model- .Tree Model

. Model/View Programming

431

QAbstractItemModel : : QStringListModel .QString : QStandardItemModel QStandardItem . : QFileSystemModel . : QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel ( .) SQL ( ) Model QAbstractItemModel . ................................................................................

531

Model/View Programming

:Views QAbstractItemView ( ) Models : : QListView .

: QTableView .

: QTreeView .

Model/View Programming

631

: QModelIndex QModelIndex Model . Models Classes ( Row Column ) root ()QModelIndex .;( QModelIndex index = model->index) Row , Column , root = QModelIndex indexA ;(()model->index)0, 0, QModelIndex indexA A 0 0. = QModelIndex indexB ;(()model->index)1, 1, QModelIndex indexB B 1 1. = QModelIndex indexC ;(()model->index)2, 1, QModelIndex indexC C 2 1. = QModelIndex indexA ;(()model->index)0, 0, QModelIndex indexA A 0 0. = QModelIndex indexC ;(()model->index)2, 1, QModelIndex indexC C 2 1. = QModelIndex indexB ;(model->index)1, 0, indexA indexB B 1 0 .indexA

731

Model/View Programming

/ QWidget Qt Creator .widget.cpp #include widget.h #include ui_widget.h #include >QStringListModel> #include >QListView> #include >QStandardItem> #include >QModelIndex>

widget.cpp

: : EX Am NO ple 29

Widget::Widget)QWidget *parent( :QWidget)parent(,ui)new Ui::Widget( { ui->setupUi)this(; QStringList strlist; strlist >> mohamed >> ali >> ahmed >> samier >> sami; QStringListModel *mymodel = new QStringListModel; mymodel->setStringList)strlist(; QListView *myview = new QListView)this(; myview->setModel)mymodel(; QModelIndex myindex; mymodel->insertRow)mymodel->rowCount)((; myindex = mymodel->index)mymodel->rowCount)(-1(; mymodel->setData)myindex,new item inserted(; myindex = mymodel->index)2(; mymodel->setData)myindex, item modified(;Model/View Programming

}

138

QStringList strlist; strlist >> mohamed >> ali >> ahmed >> samier >> sami; .QString QStringListModel *mymodel = new QStringListModel; QStringListModel mymodel . mymodel->setStringList)strlist(;0 1 2 3 4 mohamed ali ahmed samier sami

.mymodel strlist

QListView *myview = new QListView)this(; . QListView myview myview->setModel)mymodel(; . myview mymodel QModelIndex myindex; . QModelIndex myindex mymodel->insertRow)mymodel->rowCount)((; .mymodel 0 1 2 3 4 mohamed ali ahmed samier sami

5

139

Model/View Programming

myindex = mymodel->index)mymodel->rowCount)(-1(; .mymodel myindex0 1 2 3 4 5 mohamed ali ahmed samier sami

myindex

mymodel->setData)myindex,new item inserted(; . myindex new intem inserted 0 1 2 3 4 mohamed ali ahmed samier sami new item inserted

myindex

5

myindex = mymodel->index)2(; .. 2 1 0 2 myindex myindex0 1 2 3 4 5 mohamed ali ahmed samier sami new item inserted

mymodel->setData)myindex, item modified(; . myindex item modified myindex0 1 2 3 4 5 mohamed ali samier sami new item inserted Model/View Programming

140

#include widget.h #include ui_widget.h #include >QStandardItemModel> #include >QTableView> #include >QStandardItem> #include >QModelIndex>

widget.cpp

: : EX Am NO ple 30

Widget::Widget)QWidget *parent( :QWidget)parent(,ui)new Ui::Widget( { ui->setupUi)this(; QList>QStandardItem*> itemlist; itemlist >> new QStandardItem)append 1(; itemlist >> new QStandardItem)append 2(; itemlist >> new QStandardItem)append 3(; itemlist >> new QStandardItem)append 4(; QStandardItemModel *mymodel = new QStandardItemModel; mymodel->setColumnCount)3(; for)int i = 0 ; i > 6 ;i++( { for)int j = 0 ; j > mymodel->columnCount)( ;j++( { mymodel->setItem)i,j,new QStandardItem) QString)member %0,%1(.arg)i(.arg)j( ((; } } mymodel->appendRow)itemlist(; QTableView *myview = new QTableView)this(; myview->setGeometry)0,0,500,300(; myview->setModel)mymodel(; QModelIndex myindex; myindex = mymodel->index)2,2(; mymodel->setData)myindex,modified(;Model/View Programming

}

141

QList>QStandardItem*> itemlist; itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append

1(; 2(; 3(; 4(; .QStandardItem

QStandardItemModel *mymodel = new QStandardItemModel; QStandardItemModel mymodel . mymodel->setColumnCount)3(; . 3

QListView *myview = new QListView)this(; . QListView myview for)int i = 0 ; i > 6 ;i++( { for)int j = 0 ; j > mymodel->columnCount)( ; j++ ( { mymodel->setItem) i , j ,new QStandardItem) QString)member %0,%1(.arg)i(.arg)j( ((; } } . 6 3 12member 0,1 member 1,1 member 2,1 member 3,1 member 4,1 member 5,1

3member 0,2 member 1,2 member 2,2 member 3,2 member 4,2 member 5,2

1 2 3 4 5 6

member 0,0 member 1,0 member 2,0 member 3,0 member 4,0 member 5,0

Model/View Programming

142

;(mymodel->appendRow)itemlist itemlist itemlist .itemlist1 1 2 3 4 5 6 70,0 member 0,1 member 0,2 member 0,3 member 0,4 member 0,5 member 1 append

21,0 member 1,1 member 1,2 member 1,3 member 1,4 member 1,5 member 2 append

32,0 member 2,1 member 2,2 member 2,3 member 2,4 member 2,5 member 3 append

4

4 append

;(QTableView *myview = new QTableView)this myview QTableView . ;(003,005,0,0)myview->setGeometry .

;(myview->setModel)mymodel mymodel myview . ;QModelIndex myindex myindex QModelIndex . ;(2,2)myindex = mymodel->index myindex 2 2 . mymodel ;(mymodel->setData)myindex,modified myindex .modified

341

Model/View Programming

#include widget.h #include widget.h #include ui_widget.h #include >QStandardItemModel> #include >QTreeView> #include >QStandardItem> #include >QModelIndex> #include >QDebug>

widget.cpp : : EX Am NO ple 31

Widget::Widget)QWidget *parent( :QWidget)parent(,ui)new Ui::Widget( { ui->setupUi)this(; QList>QStandardItem*> itemlist; itemlist >> new QStandardItem)append 1(; itemlist >> new QStandardItem)append 2(; itemlist >> new QStandardItem)append 3(; itemlist >> new QStandardItem)append 4(; QStandardItemModel *mymodel = new QStandardItemModel; mymodel->setColumnCount)2(; QModelIndex myindex ; for)int i = 0 ; i > 3 ;i++({ QStandardItem *item = new QStandardItem) QString)item %0(.arg)i( ( ; mymodel->setItem)i,item(; item->setColumnCount)2(; item->insertRow)0,new QStandardItem(; for)int j = 0 ; j > item->columnCount)( ;j++({ myindex = mymodel->index)0,j,item->index)((; mymodel->setData)myindex,QString)item %0,%1(.arg)i(.arg)j( (; } } mymodel->appendRow)itemlist(; QTreeView *myview = new QTreeView)this(; myview->setGeometry)0,0,500,300(; myview->setModel)mymodel(; }Model/View Programming

144

QList>QStandardItem*> itemlist; itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append itemlist >> new QStandardItem)append

1(; 2(; 3(; 4(; .QStandardItem

QStandardItemModel *mymodel = new QStandardItemModel; QStandardItemModel mymodel . mymodel->setColumnCount)2(;1

2 2

QModelIndex myindex; . QModelIndex myindex for)int i = 0 ; i > 3 ;i++({ QStandardItem *item = new QStandardItem) QString)item %0(.arg)i( ( ; . QStandardItem item mymodel->setItem)i,item(; .i item 1 2

item 0

145

Model/View Programming

item->setColumnCount)2(; item->insertRow)0,new QStandardItem(;

.item 2 .item

for)int j = 0 ; j > item->columnCount)( ; j++ ({ myindex = mymodel->index)0,j,item->index)((; .item j 0 myindex mymodel->setData)myindex,QString)item %0,%1(.arg)i(.arg)j( (; .myindex } } : 1 item 0 item 0,0 item 1 item 1,0 item 2 item 2,0 2 item 0,1 item 1,1 item 2,1 1 item 0 item 0,0 2 item 0,1

Model/View Programming

146

;(mymodel->appendRow)itemlist itemlist itemlist .itemlist0 item 0,0 item 1 item 0,1 item 2 item 0,2 item 1 append 1 2 1,0 item 1,1 item 1,2 item 2 append 3 append 4 append 3 4

;(QTreeView *myview = new QTreeView)this myview QTreeView . ;(003,005,0,0)myview->setGeometry .

;(myview->setModel)mymodel mymodel myview .

................................................................................

741

Model/View Programming

/ QAbstractItemDelegate QStyledItemDelegate .QtDemo........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . ........................................................................................................................................................................................................... . .......................................................................................................................................................................................................... Model/View Programming

Notes

841

Graphics View Framework

149

Graphics View Framework

Graphics View Framework

150

: Graphics View Framework 2D / ( ) Model / View . : ( : ) QGraphicsScene Class . ( : ) QGraphicsView Class . ( : ) QGraphicsItem Class .QGraphicsScene scene

View

Scene QGraphicsScene Item QGraphicsItem View QGraphicsView scene View View .Scene 151 Graphics View Framework

QGraphicsScene Class

: QObject

: : . ( ) . .;QGraphicsScene scene ( : ) Declaration

: addLine addRect .addEllipse addItem QGraphicsItem .Scene ( item) x , y ( ) x , y .Scene ................................................................................

Graphics View Framework

251

QGraphicsView Class

: QAbstractScrollArea QAbstractScrollArea QFrame QFrame QWidget

: Scene QGraphicsView . ;QGraphicsView view ( : ) Declaration

: setScene . . ................................................................................

351

Graphics View Framework

QGraphicsItem Class

:

: .QGraphicsScene ;QGraphicsItem item ( : ) Declaration

: Scene . QGraphicsItem : QGraphicsLineItem : QGraphicsItem . QGraphicsRectItem : QGraphicsItem . QGraphicsTextItem : QGraphicsItem . . QGraphicsItem . . ................................................................................ Graphics View Framework

451

Qt Creator . QWidget .widget.cpp EX widget.cpp Am#include widget.h #include ui_widget.h #include >QtGui>NO ple 32

Widget::Widget)QWidget *parent( : QWidget)parent(, ui)new Ui::Widget( { ui->setupUi)this(; QGraphicsScene *scene = new QGraphicsScene)0,0,2000,2000,this(; scene->addRect)QRectF)0, 0, 100, 100((; scene->addRect)QRectF)1800, 1800, 100, 100((; QGraphicsView *view1 = new QGraphicsView)scene,this(; view1->setGeometry)0,0,250,250(; view1->setSceneRect)0,0,200,200(; QGraphicsView *view2 = new QGraphicsView)scene,this(; view2->setGeometry)251,0,250,250(; view2->setSceneRect)1800,1800,200,200(; QGraphicsView *view3 = new QGraphicsView)scene,this(; view3->setGeometry)502,0,250,250(; QGraphicsItem *item = scene->itemAt)50, 50(; item->rotate)10(;

}

155

Graphics View Framework

;(QGraphicsScene *scene = new QGraphicsScene)0,0,2000,2000,this scene QGraphicsScene