Vous êtes sur la page 1sur 98

Qt in Education

An Introduction to Qt and Workshop

http://www.pelagicore.com/documents/2011-Apr-QtWorkshop.odp
2011 Nokia Corporation and its Subsidiary(-i s!. "h nc#os d Qt $at ria#s ar pro%id d und r th Cr ati% Co&&ons Attribution-Shar A#ik 2.' (ic ns A)r & nt.

"h *u## #ic ns t +t is a%ai#ab# h r , http,--cr ati% co&&ons.or)-#ic ns s-by-sa-2.'-# )a#cod . Nokia. Qt and th Nokia and Qt #o)os ar th r )ist r d trad &arks o* Nokia Corporation in /in#and and oth r countri s 0or#d0id .

"h 1r s nt r
2ohan "h #in 34ohan.th #in5p #a)icor .co&6

Cr at s &idd# 0ar *or in-% hic# in*otain& nt bas d on $ 7o "h sis 0ork 0ithin &b dd d (inu+. op n sourc and d si)n

What is Qt8
Qt is a cross platform development framework written in C++.

C99 *ra& 0ork : bindin)s *or oth r #an)ua) s

1ython. ;uby. C<. tc.

=ri)ina##y *or us r int r*ac s : no0 *or % rythin)


>atabas s. ?$(. W b@it. &u#ti& dia. n t0orkin). =p n7(. scriptin). non-7AI...

What is Qt8

Qt is &ad up o* &odu# s

A## &odu# s ha% a co&&on sch & and ar bui#t *ro& th sa& A1I d si)n id as
QtCore QtGui

Qt=p n7( QtW b@it Qt=p nC7 QtSB# QtS%) QtScript Qt?&#1att rns Qt$u#ti& dia QtN t0ork Qt?&# 1honon

What is Qt8

Qt +t nds C99 0ith &acros and introsp ction


foreach (int value, intList) { } QObject *o = new QPushButton o!"#etaObject()!"class$a#e()

%% returns &QPushButton&

connect(button, '()$*L(clic+e,()), win,ow, 'LO-(close()))

A## cod is sti## p#ain C99

> sktop tar) t p#at*or&s

Windo0s

$ac =S ?

(inu+-Ani+ ?11

E&b dd d tar) t p#at*or&s

Windo0s CE Sy&bian $a &o E&b dd d (inu+

>ir ct *ra& bu** r acc ss

Co&&unity tar) t p#at*or&s

Android : N c ssitas and $inistri

http,--sourc *or) .n t-p-n c ssitas-ho&

i=S. @ind# . QN?. 0+Works...

D ##o Wor#d

D ##o Wor#d

.inclu,e /Q*00lication" .inclu,e /QLabel" int #ain( int ar1c, char **ar1v ) { Q*00lication a00( ar1c, ar1v ) QLabel l( 23ello 4orl,52 ) l6show() return a006e7ec() }

D ##o Wor#d

#include <QApplication> #include <QLabel> int #ain( int ar1c, char **ar1v ) { Q*00lication a00( ar1c, ar1v ) QLabel l( 23ello 4orl,52 ) l6show() return a006e7ec() }

D ##o Wor#d

.inclu,e /Q*00lication" .inclu,e /QLabel" int #ain( int ar1c, char **ar1v ) { QApplication app( argc, argv ); QLabel l( 23ello 4orl,52 ) l6show() return a006e7ec() }

D ##o Wor#d

.inclu,e /Q*00lication" .inclu,e /QLabel" int #ain( int ar1c, char **ar1v ) { Q*00lication a00( ar1c, ar1v ) QLabel l( "Hello World!" ); l. !o"(); return a006e7ec() }

D ##o Wor#d

.inclu,e /Q*00lication" .inclu,e /QLabel" int #ain( int ar1c, char **ar1v ) { Q*00lication a00( ar1c, ar1v ) QLabel l( 23ello 4orl,52 ) l6show() return app.e#ec(); }

"h Q=b4 ct
QObject instances are individuals!

"h y can ha% a na& (QObject88object$a#e! "h y ar p#ac d in a hi rarchy o* QObject instanc s "h y can ha% conn ctions to oth r QObject instanc s E+a&p# , do s it &ak s ns to copy a 0id) t at run-ti& 8

$ ta data

Qt i&p# & nts introsp ction in C99 E% ry QObject has a meta object "h & ta ob4 ct kno0s about

c#ass na& (QObject88class$a#e! inh ritanc (QObject88inherits! prop rti s si)na#s and s#ots ) n ra# in*or&ation (QObject88class(nfo!

$ &ory $ana) & nt

QObject

can ha% par nt and chi#dr n


*0arent = *chil,9 = *chil,: = *chil,9;9 *chil,9;: new QObject() new QObject(0arent) new QObject(0arent) = new QObject(chil,9) = new QObject(chil,9) par nt chi#d1 chi#d1E1 chi#d2 chi#d1E2

Wh n a par nt ob4 ct is d # t d. it d # t s its chi#dr n


QObject QObject QObject QObject QObject

,elete 0arent

par nt d # t s chi#d1 and chi#d2 chi#d1 d # t s chi#d1E1 and chi#d1E2

$ &ory $ana) & nt

"his is us d 0h n i&p# & ntin) %isua# hi rarchi s.


Q<ialo1 *0arent = new Q<ialo1() Q)rou0Bo7 *bo7 = new Q)rou0Bo7(0arent) QPushButton *button = new QPushButton(0arent) Q=a,ioButton *o0tion9 = new Q=a,ioButton(bo7) Q=a,ioButton *o0tion: = new Q=a,ioButton(bo7) ,elete 0arent

par nt d # t s bo+ and button bo+ d # t s option1 and option2

Asa) 1att rns

As th

this!point

r as top # % # par nt

<ialo188<ialo1(Q4i,1et *0arent) 8 Q<ialo1(0arent) { Q)rou0Bo7 *bo7 = Q)rou0Bo7(this) QPushButton *button = QPushButton(this) Q=a,ioButton *o0tion9 = Q=a,ioButton(bo7) Q=a,ioButton *o0tion: = Q=a,ioButton(bo7) 666

A##ocat par nt on th stack


voi, 4i,1et88show<ialo1() { <ialo1 ,ialo1 if (,ialo16e7ec() == Q<ialo188*cce0te,) { 666 ,ialo1 is d # t d 0h n } th scop nds

D ap

Wh n usin) new and ,elete. & &ory is a##ocat d on th h ap. D ap & &ory &ust b +p#icit#y *r d usin) ,elete to a%oid & &ory # aks. =b4 cts a##ocat d on th h ap can #i% *or as #on) as th y ar n d d.

new

Construction

Destruction
,elete

Stack

(oca# %ariab# s ar a##ocat d on th stack. Stack %ariab# s ar auto&atica##y d struct d 0h n th y )o out o* scop . =b4 cts a##ocat d on th stack ar a#0ays d struct d 0h n th y )o out o* scop .

int a

Construction

Destruction
}

Stack and D ap

"o ) t auto&atic & &ory &ana) & nt. on#y th par nt n ds to b a##ocat d on th stack.
>?>ain4in,ow Q*00lication

int #ain(int ar1c, char **ar1v) { Q*00lication a(ar1c, ar1v) >?>ain4in,ow w w6show() return a6e7ec() }

>?>ain4in,ow88>?>ain4in,ow(666 { new QLabel(this) new 666 }

Si)na#s and S#ots

>yna&ica##y and #oos #y ti to) th r % nts and stat chan) s 0ith r actions What &ak s Qt tick

Si)na#s and S#ots in Action


e#it clic+e,()

Si)na#s and S#ots in Action


2+ connect(a,,Button,'()$*L(clic+e,()),this,'LO-(666))

clear()

0rivate slots8 voi, on;a,,Button;clic+e,() voi, on;,eleteButton;clic+e,()

connect(clearButton,'()$*L(clic+e,()),list4i,1et,'LO-(clear()))

Si)na#s and S#ots in Action


666 e#it clic+e,() 666

Q'trin1 new-e7t = Q(n0ut<ialo1881et-e7t(this, 2@nter te7t2, 2-e7t82) if( 5new-e7t6is@#0t?() ) ui!"list4i,1et!"a,,(te#(new-e7t)

}
{ 666 e#it clic+e,() 666 }

}
{ 666 e#it clic+e,() 666 }

foreach (QList4i,1et(te# *ite#, ui!"list4i,1et!"selecte,(te#s()) { ,elete ite# }

clear()

What is a s#ot8

A s#ot is d *in d in on o* th s#ots s ctions


0ublic slots8 voi, aPublic'lot() 0rotecte, slots8 voi, aProtecte,'lot() 0rivate slots8 voi, aPrivate'lot()

A s#ot can r turn %a#u s. but not throu)h conn ctions Any nu&b r o* si)na#s can b conn ct d to a s#ot
connect(src, '()$*L(si1()), ,est, 'LO-(slt()))

It is i&p# & nt d as an ordinary & thod It can b ca## d as an ordinary & thod

What is a si)na#8

A si)na# is d *in d in th si)na#s s ction


si1nals8 voi, a'i1nal()

A si)na# a#0ays r turns %oid A si)na# &ust not b i&p# & nt d

"h &oc pro%id s an i&p# & ntation

A si)na# can b conn ct d to any nu&b r o* s#ots Asua##y r su#ts in a dir ct ca##. but can b pass d as % nts b t0 thr ads. or % n o% r sock ts (usin) Frd party c#ass s! "h s#ots ar acti%at d in arbitrary ord r A si)na# is &itt d usin) th
e#it a'i1nal()

&it k y0ord

$akin) th conn ction


QObject*

QObject88connect( src, '()$*L( si1nature ), ,est, 'LO-( si1nature ) )

/function na#e" ( /ar1 t?0e"666 )

A signature consists of the function name and argument types. No variable names, nor values are allowed.
set-itle(Q'trin1 te7t) setBalue(C:) set(te#((te#Alass) clic+e,() to11le,(bool) set-e7t(Q'trin1) te7tAhan1e,(Q'trin1) ran1eAhan1e,(int,int)

Custom types reduces reusability.

$akin) th conn ction

Qt can i)nor ar)u& nts. but not cr at %a#u s *ro& nothin)


Signals
ran1eAhan1e,(int,int) ran1eAhan1e,(int,int) ran1eAhan1e,(int,int) valueAhan1e,(int) valueAhan1e,(int) valueAhan1e,(int) te7tAhan1e,(Q'trin1) clic+e,() clic+e,()

Slots
set=an1e(int,int) setBalue(int) u0,ate<ialo1() set=an1e(int,int) setBalue(int) u0,ate<ialo1() setBalue(int) setBalue(int) u0,ate<ialo1()

Auto&atic Conn ctions

Wh n usin) > si)n r it is con% ni nt to ha% auto&atic conn ctions b t0 n th int r*ac and your cod
on; object name ; signal name ( signal parameters )
on;a,,Button;clic+e,() on;,eleteButton;clic+e,() on;list4i,1et;current(te#Ahan1e,(QList4i,1et(te#*,QList4i,1et(te#*)

"ri)) r d by ca##in) Q>etaObject88connect'lotsB?$a#e "hink about r us 0h n na&in)

Co&par

on;wi,1et;si1nal

to u0,atePa1e>ar1ins

updat 1a) $ar)ins can b conn ct d to a nu&b r o* si)na#s or ca## d dir ct#y.

$uch &or ...

QtQuick : coo# ani&at d us r int r*ac s http,--Bt.nokia.co&-BtBuick-

7oa# o* th day

Gui#d and ntir app#ication


>ocu& nt 0indo0(s! >ia#o)s $ nus. too#bars. statusbars Gasic *i# &ana) & nt =p n. Sa% and Sa% As

W 0i## run out o* ti& H

7oa# o* th day

Cr atin) a pro4 ct

In QtCr ator. cr at a Qt !ui Application pro4 ct As th Qt"ore and Qt!ui &odu# s Gas th sk # ton pro4 ct on a Q>ain4in,ow

Cr atin) a pro4 ct

"h r su#tin) pro4 ct contains *i% *i# s

-e7t@,itor60ro : th pro4 ct

d *inition *i#

#ainwin,ow6ui : th us r

int r*ac o* th &ain 0indo0

#ainwin,ow6h%c00 : th c#ass

d c#aration and i&p# & ntation o* th &ain 0indo0

#ain6c00 : th &ain *unction.

) ttin) % rythin) initia#iI d and runnin)

"h p#an

"his # ctur 0i## bui#d a t +t ditor in an it rati% proc ss / atur s 0i## b add d to th app#ication. 0hi# &aintainin) a *unctionin) app#ication throu)hout th proc ss "his is ho0 &ost so*t0ar is b in) bui#t in r a# #i* H

Startin) 0ith th us r int r*ac

Add a Q-e7t@,it 0id) t to th &ain 0indo0 *or&

Startin) 0ith th us r int r*ac

App#y a #ayout to th c ntra# 0id) t

Startin) 0ith th us r int r*ac

Jou ha% to d s # ct and r s # ct th c ntra# 0id) t to s th s prop rti s a*t r you ha% app#i d th #ayout

S t th #ayout &ar)in prop rti s to I ro

Addin) actions

With th n +t st docu& +it th

t +t ditin) c ntra# 0id) t in p#ac . th p 0i## b to # t th us r cr at n 0 nts. c#os th curr nt docu& nt and to app#ication

/or ach o* th s us r actions. a Q*ction ob4 ct 0i## b cr at d

Why QAction

$any us r int r*ac

# & nts r * r to th sa& action

Action

Ctrl S

>o not *or) t too#tips. statusbar tips. tc.

Why QAction

A Q*ction ncapsu#at s a## s ttin)s n d d *or & nus. too# bars and k yboard shortcuts Co&&on#y us d prop rti s ar

te7t : th t +t us d % ry0h r icon : icon to b us d % ry0h r shortcut : shortcut chec+able-chec+e, : i* th action is ch ckab# and

th curr nt ch ck status

tool-i0-status-i0 : tips t +t *or too# tips (ho% r

and 0ait! and status bar tips (ho% r. no 0ait!

Why QAction
Q*ction *action = new Q*ction(parent) action!"set-e7t(2te7t2) action!"set(con(Q(con(28%icons%icon60n12)) action!"set'hortcut(QDe?'eEuence(2AtrlF)2))

Cr atin) a n 0 action S ttin) prop rti s *or t +t. icon and k yboard short-cut A QCariant can b associat d 0ith ach action. to carry data associat d 0ith th )i% n op ration

action!"set<ata(#?<ataQBariant)

Why QAction

> si)n r has an action ditor at th botto& o* th scr n


#ools $ %orm &ditor $ 'iews $ Action &ditor

Addin) a n 0 action brin)s up a dia#o) 0ith th d tai#s (na& . t +t. tips...! Action icons can b add d ith r *ro& *i# s or r sourc s

Icon r sourc s

1uttin) icons in a r sourc *i# # ts Qt &b d th & into th + cutab#


A%oid ha%in) to d p#oy &u#tip# *i# s No n d to tryin) to d t r&in th path *or th icons *or ach sp ci*ic insta## typ A## *its n at#y into th bui#d syst & ... Jou can add anythin) into r sourc s. not on#y icons

Cr atin) a r sourc *i#

Addin) a r sourc *i# to a pro4 ct is 4ust as addin) any sourc *i# to a pro4 ct Wh n ha%in) add d th r sourc *i# . &ak sur to c#os any > si)n r *or&s and r -op n th & *or > si)n r to disco% r th r sourc

Addin) r sourc s

=p nin) th r sourc *i# )i% s you th r sourc *i# ditor G *or any r sourc s can b add d to th r sourc *i# . you &ust *irst cr at a dir ctory in th r sourc *i# In this cas 0 put icons into th -icons dir ctory

Addin) actions

/ro& > si)n r. you can no0 pick an icon *or th actions that you p#an to sho0 in too#bars C#ick K...L n +t to th icon s ttin) 1ick KChoos ; sourc ...L

1ick th icon *ro& th r sourc dir ctory tr

Addin) actions

Wh n you ha% add d your actions to th action ditor. you n d to add th & to th *or&

"o add th & to th too#bar. si&p#y dra) and drop

Addin) actions

"o add th & to a & nu. doub# c#ick on K"yp h r L and nt r th & nu tit# . th n dra) and drop action onto th & nu

"ypin) KM/i# L. &ak s K/L a k yboard shortcut

"o nt r KML in a & nu. typ KMML as th t +t.

Addin) actions

!e"t N 0 C#os E+it

In this *irst it ration. 0 add th &ost basic actions Name #con Short$cut !ool$tip
actionN 0 actionC#os actionE+it Ctr#9N Ctr#9W Cr at a n 0 docu& nt C#os curr nt 0indo0 E+it app#ication

"h na& is auto&atica##y ) n rat d 0h n nt rin) th t +t *or th action C#os and E+it ar add d to th & nu.

"o add a s parator. doub# c#ick KAdd S paratorL th n dra) it into p#ac .

I&p# & ntin) n 0

Gy ri)ht c#ickin) on th action in th action ditor. you can )o th corr spondin) s#ot S#ots ar ) n rat d on th *#y in both h ad r and i&p# & ntation I&p# & ntin) th n 0 s#ot is asy : 4ust cr at a n 0 >ain4in,ow and sho0 it
voi, >ain4in,ow88on;action$ew;tri11ere,() { >ain4in,ow *w = new >ain4in,ow() w!"show() }

C#osin) and E+itin)


S ttin) th 4*;<eleteOnAlose attribut &ak s th 0id) t d # t its #* 0h n it is c#os d
>ain4in,ow88>ain4in,ow(Q4i,1et *0arent) 8 Q>ain4in,ow(0arent), ui(new Gi88>ain4in,ow) { ui!"setu0Gi(this) set*ttribute(Qt884*;<eleteOnAlose) connect(ui!"actionAlose, '()$*L(tri11ere,()), this, 'LO-(close())) connect(ui!"action@7it, '()$*L(tri11ere,()), E*00, 'LO-(close*ll4in,ows()))

A## 0id) ts has a c#os s#ot out-o*-th -bo+

"h E*00 point r r * rs to th curr nt Q*00lication instanc

"h Q*00lication c#ass has a s#ot *or c#osin) a## 0indo0s

Status updat

No0. 0 ha% a 0indo0 0ith a 0orkin) t +t ditin) 0id) t N 0 0indo0s can b cr at d Windo0s can b c#os d "h app#ication can +it (i. . c#os a## 0indo0s!

>o not c#os 0hat is &odi*i d

No0. th 0indo0s c#os r )ard# ss o* th ir cont nts "h +p ct d b ha%ior 0ou#d b to ask th us r b *or c#os a &odi*i d docu& nt "o i&p# & nt this. 0 add a &odi*i d *#a) 0hich is s t 0h n % r th t +t o* th Q-e7t@,it is chan) d W a#so int rc pt c#os % nts and ask th us r b *or c#osin) &odi*i d % nts

"h &odi*i d *#a)

> c#aration and initia#iIation

class >ain4in,ow 8 0ublic Q>ain4in,ow { 666 0rivate8 bool #;#o,ifie, }

>ain4in,ow88>ain4in,ow(Q4i,1et *0arent) 8 Q>ain4in,ow(0arent), ui(new Gi88>ain4in,ow), #;#o,ifie,(false) { 666

"h &odi*i d *#a)

pin) track o* th docu& nt


>ain4in,ow88>ain4in,ow(Q4i,1et *0arent) 666 { 666 connect(ui!"te7t@,it, '()$*L(te7tAhan1e,()), this, 'LO-(,ocu#ent>o,ifie,())) } voi, >ain4in,ow88,ocu#ent>o,ifie,() { #;#o,ifie, = true }

C#osin) sa* #y

Wh n a 0indo0 is c#os d. th top-# % # 0id) t can acc pt or i)nor th c#os % nt

Acc pt & ans that th 0indo0 0i## c#os (and b d # t d in this cas ! I)nor & ans that th 0indo0 0i## r &ain op n

A## % nts ar hand# d in th QObject88event & thod pro%id s %irtua# & thods *or th &ost co&&on % nts. .). Q4i,1et88close@vent
Q4i,1et

C#osin) sa* #y

; -i&p# & ntin) th close@vent & thod to ask th us r b *or c#osin) a 0indo0 0ith a &odi*i d docu& nt

voi, >ain4in,ow88close@vent(QAlose@vent *e) { if(#;#o,ifie,) { if(Q>essa1eBo788warnin1(this, 2<ocu#ent >o,ifie,2, 2-he ,ocu#ent has been #o,ifie,, ,o ?ou want to close itHIn2 2Jou will lose all ?our chan1es62, Q>essa1eBo788Jes K Q>essa1eBo788$o, Q>essa1eBo788$o) == Q>essa1eBo788Jes) { e!"acce0t() } else { > p ndin) on th e!"i1nore() us rNs ans0 r. th } % nt is acc pt d } else { or i)nor d e!"acce0t() } }

An&odi*i d 0indo0s ar a#0ays c#os d

Q$ ssa) Go+

"h Q>essa1eBo7 c#ass can b us d to sho0


Euestion

dia#o)s dia#o)s
;ou)h#y th sa& typ o* dia#o)s 0ith a tit# . a & ssa) and on or &or buttons.

infor#ation warnin1

dia#o)s dia#o)s

critical about

dia#o)s *or both th curr nt app#ication and Qt its #*


"h )r n 0ords ar *unction na& s *or static & &b rs. "o sho0 an in*or&ation dia#o). us Q>essa1eBo788infor#ation. tc.

Q$ ssa) Go+
1ar nt 0indo0 >ia#o) tit#

Q>essa1eBo788warnin1(this, 2<ocu#ent >o,ifie,2, 2-he ,ocu#ent has been #o,ifie,, ,o ?ou want to close itHIn2 2Jou will lose all ?our chan1es62, Q>essa1eBo788Jes K Q>essa1eBo788$o,

"h buttons to us . 1ick *ro& (k. (pen. )a*e. "ancel. "lose. +iscard. Appl,. -eset. -estore de.aults. /elp. )a*e all. 0es. 0es to all. 1o. 1o to all. Abort. -etr, and 2gnore

Q>essa1eBo788$o) == Q>essa1eBo788Jes

"h & ssa) o* th dia#o). Notic th us o* NInN

"h d *au#t button

"h r turn %a#u is th button us d *or c#osin) th dia#o)

Wh n is a docu& nt &odi*i d8

"h us r +p cts to s i* a docu& nt is &odi*i d or not *ro& th tit# o* th 0indo0 Gy s ttin) a 0indo0 tit# 0ith th sub-strin) KL*ML. th &odi*i d indication can b acti%at d usin) set4in,ow>o,ifie,. "his *unction is ca## d
*ro& th constructor as 0 ## as *ro& th ,ocu#ent>o,ifie, s#ot

voi, >ain4in,ow88u0,ate4in,ow-itle() { set4in,ow-itle(tr(2N9L*M2)6ar1(E*00!"a00lication$a#e())) set4in,ow>o,ifie,(#;#o,ifie,) }

App#ication na&

Wh n bui#din) th tit# . 0 us d th Q*00lication88a00lication$a#e & thod "h Q*00lication ob4 ct k ps track o* th app#icationNs na& . % rsion. produc r. tc. "his is us d *or 0indo0 tit# s. tc t ra
a6set*00lication$a#e(2-e7t @,itor2) a6set*00licationBersion(2O692) a6setOr1aniPation$a#e(2@7a#0le'oft2) a6setOr1aniPation<o#ain(2e7a#0le6co#2) App#ication na& and % rsion 1roduc r na& and do&ain App#ication icon

a6set4in,ow(con(Q(con(28%icons%new60n12))

Status updat

No0. 0 ha% a 0indo0 0ith a 0orkin) t +t ditin) 0id) t N 0 0indo0s can b cr at d Windo0s can b c#os d "h app#ication can +it (i. . c#os a## 0indo0s!

W can k

p track o* docu& nt &odi*ications

A 0indo0 cannot b c#os d 0ithout acc ptin) th #oss o* docu& nt &odi*ications

S # ctin) a *ont

"h standard *ont o* th t +t ditor is % ry Kp#ainL. "o r & dy. 0 0i## # t th us r pick th *ont W add an action. add a Ci 0 & nu and put th action in th & nu
Name
actionS # ct/ont

!e"t S # ct /ont...

#con

Short$cut

!ool$tip S # ct th disp#ay *ont

N 0 & nus ar a#0ays p#ac d to th ri)ht. but th y can b dra)) d in to p#ac a*t r0ards

S # ctin) a *ont

In th s#ot. a n 0 QQont %a#u is r Bu st d usin) th QQont<ialo1881etQont & thod

voi, >ain4in,ow88on;action'electQont;tri11ere,() { bool o+ QQont font = QQont<ialo1881etQont(Ro+, ui!"te7t@,it!"font(), this) if(o+) ui!"te7t@,it!"setQont(font)

"h boo# an. o+. is us d to d t r&in i* th us r actua##y pick d a *ont. "his is b caus QQont %a#u s a#0ays ar %a#id.

$or on *onts

Asin) th curr nt i&p# & ntation. ach 0indo0 pops up 0ith th d *au#t *ont and th us r has to pick a n 0 *ont "h us r +p cts s ttin)s to stick. i. . us th #ast %a#u 0h n op nin) n 0 0indo0s

QS ttin)s

S ttin)s ar stor d di** r nt#y on a## &a4or p#at*or&s Gy usin) a Q'ettin1s ob4 ct. you ) t a cross p#at*or& int r*ac *or handin) s ttin)s
Q'ettin1s settin1s #?'trin1 = settin1s6value(2+e?2,2,efault2)6to'trin1() settin1s6setBalue(2+e?2, #?'trin1) settin1s6re#ove(2+e?2)

Any QBariant can b stor d : but think about r adabi#ity *or ad%anc d us rs

Gack to th *ont

Sa%in) th *ont

voi, >ain4in,ow88on;action'electQont;tri11ere,() { bool o+ QQont font = QQont<ialo1881etQont(Ro+, ui!"te7t@,it!"font(), this) if(o+) { Q'ettin1s settin1s settin1s6setBalue(2viewQont2, font) ui!"te7t@,it!"setQont(font) } }

; storin) th *ont

>ain4in,ow88>ain4in,ow(Q4i,1et *0arent) 8 666 { 666 > *au#t %a#u Q'ettin1s settin1s ui!"te7t@,it!"setQont( settin1s6value(2viewQont2, Q*00lication88font())6value/QQont"()) 666

Custo& *ontsH

Status updat

No0. 0 ha% a 0indo0 0ith a 0orkin) t +t ditin) 0id) t N 0 0indo0s can b cr at d Windo0s can b c#os d "h app#ication can +it (i. . c#os a## 0indo0s! W can k p track o* docu& nt &odi*ications

A 0indo0 cannot b c#os d 0ithout acc ptin) th #oss o* docu& nt &odi*ications

As r con*i)urab# *onts 1 rsist nt us r s ttin)s

"h

dit & nu
asy to add
!ool$tip Cut Copy 1ast #con Short$cut Ctr#9? Ctr#9C Ctr#9C

"h actions cut. copy and past ar


Name actionCut actionCopy action1ast

!e"t Cut Copy 1ast

"h actions ar add d to both th too# bar and & nu

;i)ht c#ick on th too# bar to add s parators

"h

dit & nu

S#ots *or th n 0 actions ar i&p# & nt d in th t +t ditin) 0id) t


connect(ui!"actionAut, '()$*L(tri11ere,()), ui!"te7t@,it, 'LO-(cut())) connect(ui!"actionAo0?, '()$*L(tri11ere,()), ui!"te7t@,it, 'LO-(co0?())) connect(ui!"actionPaste, '()$*L(tri11ere,()), ui!"te7t@,it, 'LO-(0aste()))

"o a%oid tryin) to copy in %ain. # tNs add conn ctions *or nab#in) and disab#in) th actions d p ndin) on th s # ction
connect(ui!"te7t@,it, '()$*L(co0?*vailable(bool)), ui!"actionAut, 'LO-(set@nable,(bool))) connect(ui!"te7t@,it, '()$*L(co0?*vailable(bool)), ui!"actionAo0?, 'LO-(set@nable,(bool)))

Andoin)

Addin) support *or undo and r do is 4ust as asy as *or th c#ipboard op rations
Name actionAndo action; do #con Short$cut Ctr#9O Ctr#9J !ool$tip Ando th #ast action ; do th #ast action

!e"t Ando ; do

connect(ui!"actionGn,o, '()$*L(tri11ere,()), ui!"te7t@,it, 'LO-(un,o())) connect(ui!"action=e,o, '()$*L(tri11ere,()), ui!"te7t@,it, 'LO-(re,o())) connect(ui!"te7t@,it, '()$*L(un,o*vailable(bool)), ui!"actionGn,o, 'LO-(set@nable,(bool))) connect(ui!"te7t@,it, '()$*L(re,o*vailable(bool)), ui!"action=e,o, 'LO-(set@nable,(bool)))

Status updat

No0. 0 ha% a 0indo0 0ith a 0orkin) t +t ditin) 0id) t N 0 0indo0s can b cr at d Windo0s can b c#os d "h app#ication can +it (i. . c#os a## 0indo0s! W can k p track o* docu& nt &odi*ications

A 0indo0 cannot b c#os d 0ithout acc ptin) th #oss o* docu& nt &odi*ications As r con*i)urab# *onts 1 rsist nt us r s ttin)s

"h

+p ct d c#ipboard actions, cut. copy and past

Ando and r do

An&odi*yin) docu& nts

Anti# no0. docu& nts can b &odi*i d. but n % r un&odi*i d Wh n is th &odi*i d *#a) c# ar d8

In th constructor. i. . n 0 docu& nts ar not &odi*i d Wh n a docu& nt is #oad d Wh n a docu& nt is sa% d

"his & ans that 0 &ust i&p# & nt *i# op rations

Int r*acin) *i# s

Qt *i# handin) is too #ar) a sub4 ct to co% r h r H


(oadin)

QQile file(#;file$a#e) if(5file6o0en(Q(O<evice88=ea,Onl? K Q(O<evice88-e7t)) EQatal(2@rror o0enin1 file for rea,in12) Q-e7t'trea# in(Rfile) in "" te7t'trin1

Sa%in)

QQile file(#;file$a#e) if(5file6o0en(Q(O<evice884riteOnl? K Q(O<evice88-e7t)) EQatal(2@rror o0enin1 file for writin12) Q-e7t'trea# out(Rfile) out // te7t'trin1

"h docu& nt *i# na&

I* ord r to i&p# & nt *i# op rations. ach docu& nt 0indo0 &ust ha% a *i# na& As a start. it is s t in th constructor

MainWindow::MainWindow(const QString &fileName, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_modified(false), m_fileName(fileName) !!! if("m_fileName!isNull()) load#ile()$ updateWindow%itle()$

&

"h na& can b Q'trin1(). i. . nu##. as n 0. unna& d docu& nts n ds this

Apdatin) th 0indo0 tit#

"h 0indo0 tit# &ust r *# ct th *i# na&


voi, >ain4in,ow88u0,ate4in,ow-itle() { set4in,ow-itle(tr(2N9L*M ! N:2) 6ar1(#;file$a#e6is$ull()H2untitle,28QQile(nfo(#;file$a#e)6file$a#e()) 6ar1(Q*00lication88a00lication$a#e())) set4in,ow>o,ifie,(#;#o,ifie,)

E+tracts th *i# na& part o* a *i# na& 0ith a path

; su#ts in,
untitle,* ! -e7t @,itor testin16t7t ! -e7t @,itor

(oadin) docu& nts

>ocu& nts ar #oad d *ro& th loa,Qile & &b r *unction It att &pts to #oad th curr nt *i# I* it *ai#s. it s ts th curr nt *i# na& to nu##

voi, >ain4in,ow88loa,Qile() { QQile file(#;file$a#e) if(5file6o0en(Q(O<evice88=ea,Onl? K Q(O<evice88-e7t)) { Q>essa1eBo788warnin1(this, Q*00lication88a00lication$a#e(), tr(2Aoul, not o0en file N96InN:2) 6ar1(QQile(nfo(#;file$a#e)6file$a#e()) 6ar1(file6error'trin1())) #;file$a#e = Q'trin1() } else { Q-e7t'trea# in(Rfile) ui!"te7t@,it!"set-e7t(in6rea,*ll()) } #;#o,ifie, = false u0,ate4in,ow-itle() }

(oadin) docu& nts

>ocu& nts ar #oad d *ro& th loa,Qile & &b r *unction It att &pts to #oad th curr nt *i# I* it *ai#s. it s ts th curr nt *i# na& to nu##

voi, >ain4in,ow88loa,Qile() { QQile file(#;file$a#e) if(5file6o0en(Q(O<evice88=ea,Onl? K Q(O<evice88-e7t)) { Q>essa1eBo788warnin1(this, Q*00lication88a00lication$a#e(), tr(2Aoul, not o0en file N96InN:2) 6ar1(QQile(nfo(#;file$a#e)6file$a#e()) 6ar1(file6error'trin1())) #;file$a#e = Q'trin1() } else { Q-e7t'trea# in(Rfile) ui!"te7t@,it!"set-e7t(in6rea,*ll()) } #;#o,ifie, = false u0,ate4in,ow-itle() }

(oadin) docu& nts

>ocu& nts ar #oad d *ro& th loa,Qile & &b r *unction It att &pts to #oad th curr nt *i# I* it *ai#s. it s ts th curr nt *i# na& to nu##

voi, >ain4in,ow88loa,Qile() { QQile file(#;file$a#e) if(5file6o0en(Q(O<evice88=ea,Onl? K Q(O<evice88-e7t)) { Q>essa1eBo788warnin1(this, Q*00lication88a00lication$a#e(), tr(2Aoul, not o0en file N96InN:2) 6ar1(QQile(nfo(#;file$a#e)6file$a#e()) 6ar1(file6error'trin1())) #;file$a#e = Q'trin1() } else { Q-e7t'trea# in(Rfile) ui!"te7t@,it!"set-e7t(in6rea,*ll()) } #;#o,ifie, = false u0,ate4in,ow-itle() }

(oadin) docu& nts

>ocu& nts ar #oad d *ro& th loa,Qile & &b r *unction It att &pts to #oad th curr nt *i# I* it *ai#s. it s ts th curr nt *i# na& to nu##

voi, >ain4in,ow88loa,Qile() { QQile file(#;file$a#e) if(5file6o0en(Q(O<evice88=ea,Onl? K Q(O<evice88-e7t)) { Q>essa1eBo788warnin1(this, Q*00lication88a00lication$a#e(), tr(2Aoul, not o0en file N96InN:2) 6ar1(QQile(nfo(#;file$a#e)6file$a#e()) 6ar1(file6error'trin1())) #;file$a#e = Q'trin1() } else { Q-e7t'trea# in(Rfile) ui!"te7t@,it!"set-e7t(in6rea,*ll()) } #;#o,ifie, = false u0,ate4in,ow-itle() }

=p nin) docu& nts

No0. 0 can #oad *i# s on start-up


int #ain(int ar1c, char *ar1vLM) { Q*00lication a(ar1c, ar1v) 666 >ain4in,ow *w if(a6ar1u#ents()6len1th()"9) w = new >ain4in,ow(a6ar1u#ents()6last()) else w = new >ain4in,ow() w!"show() return a6e7ec() }

Jou can sp ci*y ar)u& nts in th ;un S ttin)s. und r th pro4 ct tab in QtCr ator

Addin) *i# actions

Actions to # t th us r op n and sa% docu& nts


!e"t Name action=p n actionSa% actionSa% As #con Short$cut Ctr#9= Ctr#9S
Ctr#9Shi*t9S

!ool$tip =p n a docu& nt Sa% th curr nt docu& nt Sa% curr nt docu& nt as

=p n... Sa% Sa% As...

"h actions ar add d to th /i# & nu and too# bar

=p nin) docu& nts

QQile<ialo1881etO0enQile$a#e(this, Initia# dir ctory

Askin) *or *i# na& s. 0 us th QQile<ialo1881etO0en-'aveQile$a#e 1ar nt 0indo0 & thods


>ia#o) tit# 2O0en ,ocu#ent2, Q<ir88currentPath(), 2-e7t ,ocu#ents (*6t7t)2) /i# typ *i#t r

/i# typ *i#t rs can contain s % ra# *i# +t nsions, 2<ocu#ent (*6t7t *6rtf *6,oc)2 Gut a#so s % ra# docu& nt typ s, 2<ocu#ent (*6t7t) (#a1es (*60n1)2

=p nin) docu& nts

1uttin) a## to) th r op ns docu& nts

voi, >ain4in,ow88on;actionO0en;tri11ere,() { Q'trin1 file$a#e = QQile<ialo1881etO0enQile$a#e(this, 2O0en ,ocu#ent2, Q<ir88currentPath(), 2-e7t ,ocu#ents (*6t7t)2) if(5file$a#e6is$ull()) { >ain4in,ow *w = new >ain4in,ow(file$a#e) w!"show() }

=p nin) docu& nts

W can a%oid op nin) a n 0 0indo0 i* th curr nt on is un&odi*i d and 0ithout *i# na&
voi, >ain4in,ow88on;actionO0en;tri11ere,() { Q'trin1 file$a#e = QQile<ialo1881etO0enQile$a#e(666) if(5file$a#e6is$ull()) { if(5#;#o,ifie, RR #;file$a#e6is$ull()) { #;file$a#e = file$a#e loa,Qile() } else { >ain4in,ow *w = new >ain4in,ow(file$a#e) w!"show() } } }

Sa%in) docu& nts

Sa%in) docu& nts is s#i)ht#y &or co&p#icat d than #oadin)


)a*e 0ithout a na& # ads to )a*e As )a*e As s ts a na& and att &pts to sa% )a*e is on o* th options 0h n a &odi*i d docu& nt is c#os d )a*e As can b canc # d Sa%in) can *ai#

Sa%in) docu& nts


bool >ain4in,ow88saveQile() { if(#;file$a#e6is$ull()) return saveQile*s()

I* th na& is nu##. 0 n d to sa% as

QQile file(#;file$a#e) if(5file6o0en(Q(O<evice884riteOnl? K Q(O<evice88-e7t)) { Q>essa1eBo788warnin1(666) #;file$a#e = Q'trin1() I* th *i# cannot b u0,ate4in,ow-itle() return false op n d *or 0ritin). } 0 disp#ay a 0arnin) Q-e7t'trea# out(Rfile) out // ui!"te7t@,it!"toPlain-e7t() #;#o,ifie, = false u0,ate4in,ow-itle() return true

Sa% th docu& nt

and s t th *i# na& to nu##

Apdat th &odi*i d *#a) and tit#

; turn true on succ ss

Sa%in) docu& nts


bool >ain4in,ow88saveQile*s() { Q'trin1 file$a#e = QQile<ialo1881et'aveQile$a#e(666) if(5file$a#e6is$ull()) { #;file$a#e = file$a#e return saveQile() } return false

I* a na& is )i% n. att &pt to sa% I* no na& is )i% n. th sa% is a *ai#ur

Ask th us r *or a *i# na&

"h & thod saveQile*s n % r ca##s saveQile un# s 5file$a#e6is$ull() thus th r is no risk *or in*init r cursion

Sa%in) docu& nts

As sa% and sa% as not ar auto&atica##y conn ct d s#ots. th y n d to b conn ct d to th corr spondin) actions in th constructor
connect(ui!"action'ave, '()$*L(tri11ere,()), this, 'LO-(saveQile())) connect(ui!"action'ave*s, '()$*L(tri11ere,()), this, 'LO-(saveQile*s()))

Sa%in) docu& nts

"h *ina# pi c o* th puII# is th c#os % nt

sa% d8 J >o not c#os C#os

Sa%in) docu& nts


voi, >ain4in,ow88close@vent(QAlose@vent *e) { if(#;#o,ifie,) { switch(Q>essa1eBo788warnin1(this, 2<ocu#ent >o,ifie,2, 2-he ,ocu#ent has 6662, Q>essa1eBo788Jes K Q>essa1eBo788$o K Q>essa1eBo788Aancel, Q>essa1eBo788Aancel)) { case Q>essa1eBo788Jes8 "h us r 0ants to sa% if(saveQile()) e!"acce0t() else > p ndin) on th sa% . c#os or i)nor e!"i1nore() brea+ case Q>essa1eBo788$o8 "h us r do not 0ant to sa% e!"acce0t() brea+ case Q>essa1eBo788Aancel8 Canc # th c#osin) e!"i1nore() brea+ } } else { C#os un&odi*i d docu& nts e!"acce0t() } }

Status updat

No0. 0 ha% a 0indo0 0ith a 0orkin) t +t ditin) 0id) t N 0 0indo0s can b cr at d Windo0s can b c#os d "h app#ication can +it (i. . c#os a## 0indo0s! W can k p track o* docu& nt &odi*ications

A 0indo0 cannot b c#os d 0ithout acc ptin) th #oss o* docu& nt &odi*ications As r con*i)urab# *onts 1 rsist nt us r s ttin)s "h +p ct d c#ipboard actions, cut. copy and past

Ando and r do

Can #oad docu& nts Can sa% docu& nts

"hank you *or your att ntionH 4ohan.th #in5p #a)icor .co&

Vous aimerez peut-être aussi