Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates...

Post on 28-Jul-2020

0 views 0 download

Transcript of Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates...

1

C++ GUI Programming with Qt 3

Rozália Szabó NacsaEötvös Loránd University, Budapest

nacsa@inf.elte.hu

2

The Task

QMainWindow (RichEdit)

QTextEdit(textEdit)

3

Reads and writesReadingGenerates#includesToolGenerated source fileRevision controlled source file

The “ui.h” extension approachQt

designer

UIC

richedit.cpprichedit.cppmain.cppmain.cpp

richedit.uirichedit.ui

richedit.hrichedit.h

Application specific functionsare given in the richedit.ui.h

implementation file.

Application specific functionsare given in the richedit.ui.h

implementation file.

richedit.ui.hrichedit.ui.h

4

Creating a Project File

File/New …File/New …

3

1

2

3

5

Creating a Main Window

File/New …File/New …1

2

3

6

„Choose available menus and toolbars”page

7

„Setup Toolbar” page

8

RichEdit: Properties

9

Save the project

Accept the name: richedit

File/Save…File/Save…

10

Adding main program to the project

File/New …File/New …

11

main.cpp

#include <qapplication.h>#include "richedit.h"

int main( int argc, char ** argv ){

QApplication a( argc, argv );RichEdit w;w.show();a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );return a.exec();

}

#include <qapplication.h>#include "richedit.h"

int main( int argc, char ** argv ){

QApplication a( argc, argv );RichEdit w;w.show();a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );return a.exec();

}

main.cppmain.cpp

The generated main.cppprogram

12

Compile & Run

1. (qmake –project)2. qmake –o Makefile richedit.pro3. nmake4. richedit

1. (qmake –project)2. qmake –o Makefile richedit.pro3. nmake4. richedit

No slots, but it is still inthe menu.

13

Preview – Ctrl+T

14

Action Editor „in Action”

15

Creating new Actions: Italic, Bold, Underlined Fonts

Right Click & …

A new actioon appears in he Action Editor.Properties are given in thenext slide.

16

Properties of Bold Action

17

Adding Pictures to the Project

18

Properties of Font Actions

italicActionitalicAction

boldActionboldAction

underlineActionunderlineAction

19

Creating Action Group: Alignment

20

alignActionGroup’s properties

21

Creating Actions within a GroupSelect thealignActionGroup, thenRight Click & …

1

2

22

Properties of Align Actions

leftAlignActionleftAlignAction

rightAlignActionrightAlignAction

centerAlignActioncenterAlignAction

23

Deleting unnecessary Actions

24

Creating a Toolbar

Right Click at the right endof the toolbar.

25

New ToolbarNew Toolbar

Drag & DropDrag & Drop

Select the action you want toadd to the toolbar, drag & drop & …

26

Inserting separator into the toolbar

1.Right Click on a toolbar button (!!!)2. Insert Separator

1.Right Click on a toolbar button (!!!)2. Insert Separator

27

Adding Font Actions to the Toolbar

Drag & DropDrag & Drop

28

Creating a Font ComboBox on the Toolbar

Click on the ComboBoxClick on the ComboBox

Click on the Separator (!)Click on the Separator (!)

1

2

29

Click on the SpinBoxClick on the SpinBox

Click on the ComboBox (!)Click on the ComboBox (!)

1

2

30

SpinBox’s and ComboBox’s Properties

31

Adding Menus to the Project

Right click on the menu barRight click on the menu bar1

2

32

A new „Menu” in the Project

33

Renaming menu text

Press F2 to edit the text.Press F2 to edit the text.

F&ormatF&ormat

34

Insering alignActionGroup into Format Menu

Drag & Drag & DropDrag & Drag & Drop

1. Select alignActionGroup in ActionEditor

2. Keep press the left mouse button3. Keep pressing find the Format menu4. Keep pressing move down and find

the position of the submenu. (The redline shows the position.)

35

Adding Separator to the Menu

Drag & DropDrag & Drop

36

Drag & DropDrag & Drop

37

Adding the Main Widget

QTextEdit (textEdit)

38

textEdit’s properties

39

Lay Out the Form

Click on the Form.(No selected widgets!)1

2

3

40

Creating Connections to ChangeFontAttributes

Sender: boldActionSignal: toggled(bool)Receiver: textEditSlot: setBold(bool)

Sender: boldActionSignal: toggled(bool)Receiver: textEditSlot: setBold(bool)

Sender: italicActionSignal: toggled(bool)Receiver: textEditSlot: setItalic(bool)

Sender: italicActionSignal: toggled(bool)Receiver: textEditSlot: setItalic(bool)

Sender: underlineActionSignal: toggled(bool)Receiver: textEditSlot: setUnderline(bool)

Sender: underlineActionSignal: toggled(bool)Receiver: textEditSlot: setUnderline(bool)

41

Using Documentation: QAction

42

Using Documentation: QTextEdit

43

Preview

These functions can be activated in different way.These functions can be activated in different way.

44

Qt designer

UIC

richedit.cpprichedit.cppmain.cppmain.cpp

richedit.uirichedit.ui

richedit.hrichedit.h

richedit.ui.hrichedit.ui.h

45

Defining „constructor”: init()

void RichEdit::init(){

textEdit->setFocus();}

void RichEdit::init(){

textEdit->setFocus();}

richedit.ui.hrichedit.ui.h

46

New Slot: changeAlignment(QAction*)

Function: changeAlignment(QAction*)Return type: voidSpecifier: virtualAccess: publicType: slot

Function: changeAlignment(QAction*)Return type: voidSpecifier: virtualAccess: publicType: slot

47

New Connection

Sender: alignActionGroupSignal: selected(QAction*)Receiver: RichEditSlot: changeAlignment(QAction*)

Sender: alignActionGroupSignal: selected(QAction*)Receiver: RichEditSlot: changeAlignment(QAction*)

48

RichEdit::changeAlignment(QAction *action)

void RichEdit::changeAlignment( QAction * alignAction){

if (alignAction == leftAlignAction)textEdit->setAlignment(Qt::AlignLeft);

else if (alignAction == rightAlignAction)textEdit->setAlignment(Qt::AlignRight);

else if (alignAction == centerAlignAction)textEdit->setAlignment(Qt::AlignCenter);

}

void RichEdit::changeAlignment( QAction * alignAction){

if (alignAction == leftAlignAction)textEdit->setAlignment(Qt::AlignLeft);

else if (alignAction == rightAlignAction)textEdit->setAlignment(Qt::AlignRight);

else if (alignAction == centerAlignAction)textEdit->setAlignment(Qt::AlignCenter);

}

richedit.ui.hrichedit.ui.h

49

New Function: saveAndContinue(const QString & action)

Function: saveAndContinue(const QString & action)Return type: intSpecifier: virtualAccess: protectedType: function

Function: saveAndContinue(const QString & action)Return type: intSpecifier: virtualAccess: protectedType: function

50

RichEdit::saveAndContinue(const QString & action)

int RichEdit::saveAndContinue( const QString & action ) {int continueAction = 1;if ( textEdit->isModified() ) {

switch( QMessageBox::information(this, "Rich Edit","The document contains unsaved changes.\n""Do you want to save the changes?","&Save", "&Don't Save", "&Cancel " + action,0, // Enter == button 02 ) ) { // Escape == button 2

case 0: // Save; continuefileSave();break;

case 1: // Do not save; continuebreak;

case 2: // CancelcontinueAction = 0;break;

}}return continueAction;

}

int RichEdit::saveAndContinue( const QString & action ) {int continueAction = 1;if ( textEdit->isModified() ) {

switch( QMessageBox::information(this, "Rich Edit","The document contains unsaved changes.\n""Do you want to save the changes?","&Save", "&Don't Save", "&Cancel " + action,0, // Enter == button 02 ) ) { // Escape == button 2

case 0: // Save; continuefileSave();break;

case 1: // Do not save; continuebreak;

case 2: // CancelcontinueAction = 0;break;

}}return continueAction;

}

richedit.ui.hrichedit.ui.h

Includes (in Implementation):<qmessagebox.h>

Includes (in Implementation):<qmessagebox.h>

51

New Function: saveAndContinue(const QString & action)

Function: saveAndContinue(const QString & action)Return type: intSpecifier: virtualAccess: protectedType: function

Function: saveAndContinue(const QString & action)Return type: intSpecifier: virtualAccess: protectedType: function

52

RichEdit::saveAndContinue(const QString & action)

int RichEdit::saveAndContinue( const QString & action ) {int continueAction = 1;if ( textEdit->isModified() ) {

switch( QMessageBox::information(this, "Rich Edit","The document contains unsaved changes.\n""Do you want to save the changes?","&Save", "&Don't Save", "&Cancel " + action,0, // Enter == button 02 ) ) { // Escape == button 2

case 0: // Save; continuefileSave();break;

case 1: // Do not save; continuebreak;

case 2: // CancelcontinueAction = 0;break;

}}return continueAction;

}

int RichEdit::saveAndContinue( const QString & action ) {int continueAction = 1;if ( textEdit->isModified() ) {

switch( QMessageBox::information(this, "Rich Edit","The document contains unsaved changes.\n""Do you want to save the changes?","&Save", "&Don't Save", "&Cancel " + action,0, // Enter == button 02 ) ) { // Escape == button 2

case 0: // Save; continuefileSave();break;

case 1: // Do not save; continuebreak;

case 2: // CancelcontinueAction = 0;break;

}}return continueAction;

}

richedit.ui.hrichedit.ui.h

Includes (in Implementation):<qmessagebox.h>

Includes (in Implementation):<qmessagebox.h>

53

Compile & Run

54