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

54
1 C++ GUI Programming with Qt 3 Rozália Szabó Nacsa Eötvös Loránd University, Budapest [email protected]

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

Page 1: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

1

C++ GUI Programming with Qt 3

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

[email protected]

Page 2: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

2

The Task

QMainWindow (RichEdit)

QTextEdit(textEdit)

Page 3: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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

Page 4: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

4

Creating a Project File

File/New …File/New …

3

1

2

3

Page 5: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

5

Creating a Main Window

File/New …File/New …1

2

3

Page 6: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

6

„Choose available menus and toolbars”page

Page 7: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

7

„Setup Toolbar” page

Page 8: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

8

RichEdit: Properties

Page 9: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

9

Save the project

Accept the name: richedit

File/Save…File/Save…

Page 10: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

10

Adding main program to the project

File/New …File/New …

Page 11: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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

Page 12: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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.

Page 13: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

13

Preview – Ctrl+T

Page 14: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

14

Action Editor „in Action”

Page 15: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

15

Creating new Actions: Italic, Bold, Underlined Fonts

Right Click & …

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

Page 16: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

16

Properties of Bold Action

Page 17: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

17

Adding Pictures to the Project

Page 18: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

18

Properties of Font Actions

italicActionitalicAction

boldActionboldAction

underlineActionunderlineAction

Page 19: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

19

Creating Action Group: Alignment

Page 20: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

20

alignActionGroup’s properties

Page 21: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

21

Creating Actions within a GroupSelect thealignActionGroup, thenRight Click & …

1

2

Page 22: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

22

Properties of Align Actions

leftAlignActionleftAlignAction

rightAlignActionrightAlignAction

centerAlignActioncenterAlignAction

Page 23: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

23

Deleting unnecessary Actions

Page 24: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

24

Creating a Toolbar

Right Click at the right endof the toolbar.

Page 25: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

25

New ToolbarNew Toolbar

Drag & DropDrag & Drop

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

Page 26: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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

Page 27: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

27

Adding Font Actions to the Toolbar

Drag & DropDrag & Drop

Page 28: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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

Page 29: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

29

Click on the SpinBoxClick on the SpinBox

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

1

2

Page 30: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

30

SpinBox’s and ComboBox’s Properties

Page 31: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

31

Adding Menus to the Project

Right click on the menu barRight click on the menu bar1

2

Page 32: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

32

A new „Menu” in the Project

Page 33: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

33

Renaming menu text

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

F&ormatF&ormat

Page 34: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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.)

Page 35: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

35

Adding Separator to the Menu

Drag & DropDrag & Drop

Page 36: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

36

Drag & DropDrag & Drop

Page 37: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

37

Adding the Main Widget

QTextEdit (textEdit)

Page 38: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

38

textEdit’s properties

Page 39: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

39

Lay Out the Form

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

2

3

Page 40: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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)

Page 41: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

41

Using Documentation: QAction

Page 42: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

42

Using Documentation: QTextEdit

Page 43: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

43

Preview

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

Page 44: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

44

Qt designer

UIC

richedit.cpprichedit.cppmain.cppmain.cpp

richedit.uirichedit.ui

richedit.hrichedit.h

richedit.ui.hrichedit.ui.h

Page 45: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

45

Defining „constructor”: init()

void RichEdit::init(){

textEdit->setFocus();}

void RichEdit::init(){

textEdit->setFocus();}

richedit.ui.hrichedit.ui.h

Page 46: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “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

Page 47: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

47

New Connection

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

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

Page 48: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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

Page 49: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “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

Page 50: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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>

Page 51: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.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

Page 52: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

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>

Page 53: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

53

Compile & Run

Page 54: Rozália Szabó Nacsa Eötvös Loránd University, Budapest ...3 Reads and writes Reading Generates #includes Tool Generated source file Revision controlled source file The “ui.h”

54