object-oriented programmingiuliana/oop/lectures/lecture_10.pdf · object-oriented programming...

39
Object- Oriented Programming Iuliana Bocicor Signals and slots User defined signals and slots Notepad example Drawing with Qt Other useful Qt classes Object-Oriented Programming Iuliana Bocicor [email protected] Babes-Bolyai University 2020 1 / 39

Upload: others

Post on 16-Jul-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Object-Oriented Programming

Iuliana [email protected]

Babes-Bolyai University

2020

1 / 39

Page 2: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Overview

1 Signals and slots

2 User defined signals and slots

3 Notepad example

4 Drawing with Qt

5 Other useful Qt classes

2 / 39

Page 3: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks I

When we change one widget, we often want another widgetto be notified. E.g.:

when the close button is pressed on a main window in anapplication, all application related windows should be closed;when a value is chosen in a combobox, a list should be pop-ulated with different values.

In other toolkits, this kind of communication is achievedusing callbacks.

3 / 39

Page 4: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks II

A callback is function that is called by another function,when an event happens.

If you want a processing function to notify you about someevent you pass a pointer to another function (the callback)to the processing function.

The processing function then calls the callback when appro-priate.

E.g.

Comparison function passed to a sorting algorithm.A progress bar object, which could be used by any client.

4 / 39

Page 5: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks III

Example - progress bar

The prototype of the callback function is defined by thedeveloper of the progress bar.

//callback function prototipe

typedef void (∗ P r o g r e s s L i s t e n e r ) ( int pe r c en t ) ;

//function that notifies progress using callback

void someComputation ( P r o g r e s s L i s t e n e r c a l l b a c k ){

for ( int i = 0 ; i < 100 ; i++){

cout << "Doing some computation\n" ;c a l l b a c k ( i ) ; //notification after each

step

}}

5 / 39

Page 6: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks IV

#i n c l u d e < f u n c t i o n a l>

//function that notifies progress using callback

void someComputation ( s td : : f u n c t i o n<void ( int )>c a l l b a c k )

{for ( int i = 0 ; i < 100 ; i++){

//do stuff

cout << "Doing some computation\n" ;c a l l b a c k ( i ) ; // notification after each

step

}}

6 / 39

Page 7: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks V

The client code (which needs the notifications) will have todefine a function with that specific prototype.

void onProg r e s s ( int pe r c en t ){

s t d : : cout << "progress:" << pe r c en t ;}

int main ( ){

someComputation ( onProg r e s s ) ;}

7 / 39

Page 8: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Callbacks VI

Callbacks in c++ have two fundamental flaws:

if there are several notifications needed, we either need sep-arate callback functions, or we could use generic parameters(void*), which cannot be verified at compile-time.

the processing function is coupled to the callback function(it needs to know its signature, its parameters).

8 / 39

Page 9: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Signals and slots I

Qt signals and slots are an alternative to the callback mech-anism.

They are used for communication between objects.

The signals and slots mechanism is a central feature of Qt.

9 / 39

Page 10: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Signals and slots II

A signal is emitted when a particular event occurs (e.g. abutton is clicked).

Qt widgets have many pre-defined signals which are emit-ted to indicate that a user action or a change of state hasoccurred.

A slot is a function that is called in response to a particularsignal.

A signal can be connected to a function (called a slot), sothat when the signal is emitted, the slot is automaticallyexecuted.

10 / 39

Page 11: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Signals and slots III

The signature of a signal must match the signature of thereceiving slot (in fact, a slot may have a shorter signaturethan the signal it receives because it can ignore extra argu-ments) ⇒ the signals and slots mechanism is type safe.

Slots can be used for receiving signals, but they are alsonormal member functions.

Just as an object does not know if anything receives itssignals, a slot does not know if it has any signals connectedto it ⇒ truly independent components can be created withQt and there is a loose coupling between signals andslots.

11 / 39

Page 12: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Signals and slots IV

You can connect as many signals as you want to a singleslot, and a signal can be connected to as many slots as youneed.

Qt widgets have many predefined signals, but we can sub-class widgets to add new signals to them.

Qt widgets have many pre-defined slots.

It is common practice to subclass widgets and add youruser-defined slots that can handle the signals.

12 / 39

Page 13: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Connecting signals and slots I

A slot is called when a signal connected to it is emitted.Slots are normal C++ functions and can be called normally;their only special feature is that signals can be connectedto them.

Several slots can be connected to one signal, the slots willbe executed one after the other, in the order they have beenconnected, when the signal is emitted.

13 / 39

Page 14: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Connecting signals and slots II

To connect a signal and a slot we use the function QOb-ject::connect and (optionally) the macros SIGNAL and SLOT.

QPushButton ∗ c l o s eBu t t on = new QPushButton ("Close" ) ;// Qt5

QObject : : connect ( c l o s eBut ton , &QPushButton : : c l i c k e d ,QApp l i c a t i on : : i n s t a n c e ( ) , &QApp l i c a t i on : : q u i t ) ;

// older Qt versions

QObject : : connect ( c l o s eBut ton , SIGNAL( c l i c k e d ( ) ) ,QApp l i c a t i on : : i n s t a n c e ( ) , SLOT( qu i t ( ) ) ) ;

DEMO

Simple example of connecting signals and slots (Lec-ture9 demo Qt designer).

14 / 39

Page 15: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Spin and slider example I

If the the user changes the value in the Spin Box:

The spinbox will emit the signal valueChanged(int) withan int argument, the current value of the spinner.

Because the spinner and slider are connected, the setValue(int)method of the slider will be invoked. The argument fromthe valueChanged method (the current value of the spinner)will be passed to the setValue method of the slider.

15 / 39

Page 16: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Spin and slider example II

The slider updates itself to reflect the new value, and emitsa valueChanged signal because its own value has changed.

Because the slider is also connected with the spinner, thesetValue slot of the spinner is invoked, in response to theslider’s signal.

The setValue of the spinner will not emit any signal becausethe current value is the same as the value provided in thesetValue (prevents infinite loop).

DEMO

Simple example of connecting signals and slots (Lec-ture10 demo spinner slider).

16 / 39

Page 17: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Subclassing QWidget

Create a separate class for the GUI that you are designing.

This class must inherit from QWidget.

Make it an independent, self-contained component, with itsown signals and slots.

17 / 39

Page 18: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

User defined signals and slots

To define custom signals and slots, use the Q OBJECTmacro.

This macro must be at the beginning of the class defini-tion.

class GenesGUI : public QWidget{

Q OBJECT//...

}

If you are using the Qt5 syntax, there is no need to add themacro.

18 / 39

Page 19: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

The meta-object system

Qt introduces a mechanism called the meta-object systemwhich provides two key services:

signals and slots;

introspection

the ability to introspect objects at runtime;the the methods and properties of an object can be listedat runtime;is necessary for implementing signals and slots;allows application programmers to obtain ”meta-information”about QObject subclasses at run-time, including the list ofsignals and slots supported by the object and its class name.

19 / 39

Page 20: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

The meta-object compiler I

The meta-object compiler (MOC) is the tool that providesintrospection support.

It is a code generator - parses the header files and generatesan additional C++ file that is compiled with the rest of theprogram.

The meta-object compiler takes all classes starting with theQ OBJECT macro and generates a moc *.cpp C++ sourcefile.

This file contains information about the class being moc-edsuch as class name, inheritance tree and also the names andpointers to the signal and slot members.

20 / 39

Page 21: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

The meta-object compiler II

This means that emitting a signal is actually calling a func-tion generated by the MOC.

Macros used by the MOC:

signalsslotsQ OBJECTemitSIGNALSLOT

DEMO

Simple example of introspection (IntrospectionExample).

21 / 39

Page 22: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Custom slots I

The new syntax in Qt 5 allows us to connect signals to anymember function of QObject, not only slots.

Before Qt 5 Custom slots had to be declared using the slotskeyword.

A slot is just a regular method and the compiler will handleit as any other function.

Using a signal-slot connection, slots can be invoked by anycomponent.

A signal emitted from an instance of class A can cause aprivate slot to be invoked in an instance of class B, even ifA and B are unrelated.

22 / 39

Page 23: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Custom signals I

Custom signals can be defined using the signals macro.

s i g n a l s :void gene sUpdatedS igna l ( ) ;void addGeneS igna l ( const s t d : : s t r i n g&

geneName , const s t d : : s t r i n g&organismName , const s t d : : s t r i n g&sequence ) ;

Signals can be emitted by an object when its internal statehas changed in some way that might be interesting to an-other object.

They can never have return types (use void).

23 / 39

Page 24: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Custom signals II

The emit macro is used to emit signals.

void GenesGUI : : d e l e t eGene ( ){

// ...

// emit the signal: the genes were updated

emit gene sUpdatedS igna l ( ) ;}

Signals are public access functions and can be emitted fromanywhere, but it is recommended to only emit them fromthe class that defines the signal and its subclasses.

When a signal is emitted, the slots connected to it are usu-ally executed immediately, just like a normal function call.

24 / 39

Page 25: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Custom signals III

Execution of the code following the emit statement will oc-cur once all slots have returned.

If several slots are connected to one signal, the slots will beexecuted one after the other, in the order they have beenconnected.

25 / 39

Page 26: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Example

DEMO

Gene manager (Lecture10 demo signals and slots).

26 / 39

Page 27: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

QMainWindow

A main window provides a framework for building an appli-cation’s user interface.

QMainWindow has its own layout to which you can add:

A menu bar (on the top): QMenuBar;Toolbars: QToolBar;A central widget: this will typically be a QTextEdit or aQGraphicsView;A status bar (on the bottom) : QStatusBar.

27 / 39

Page 28: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Using the menu bar

QMainWindow provides the function menuBar(), which al-lows adding QMenus to the menu bar and adding QActionsto the pop-up menus.

QAction can be used for common commands can be invokedvia menus, toolbar buttons, and keyboard shortcuts.

QMenu∗ f i l eMenu = this−>menuBar ( )−>addMenu ("&File" ) ;QAction ∗ openAct ion = new QAction ("&Open" , this ) ;f i l eMenu−>addAct ion ( openAct ion ) ;

28 / 39

Page 29: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Using the tool bar

QToolBar provides a movable panel that contains a set ofcontrols.

Toolbar buttons are added by adding actions, using thefunction addAction.

QToolBar∗ f i l e T o o l B a r = addToolBar ("&File" ) ;f i l eToo lB a r−>addAct ion ( openAct ion ) ;f i l eToo lB a r−>addAct ion ( s a v eAc t i on ) ;

29 / 39

Page 30: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Notepad example

DEMO

Notepad example (Lecture10 demo Notepad).

30 / 39

Page 31: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

QPainter I

QPainter is used for low-level painting on custom definedwidgets.

It can draw simple and complex shapes (from lines to pies).

The QPen object, which defines how a painter should drawlines and outlines of shapes.

The QBrush defines the fill pattern of shapes drawn by aQPainter.

The QPainterPath is an object composed of building blockssuch as rectangles, ellipses, lines.

31 / 39

Page 32: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

QPainter II

The paintEvent method (of the QWidget class) is invokedwhen the QWidget needs to repaint all or part of the widget.

The keyPressEvent method is invoked when a key is pressed(only if the widget has the focus).

The mousePressEvent method is invoked when a mouse keyis pressed.

DEMO

Drawing example (Lecture10 demo Drawing).

32 / 39

Page 33: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes I

QString

Unicode character string.

The functions that have a QString as parameter will accepta const char*.

There are methods to convert a QString in an std::stringand viceversa.

QStr ing s = "145" ;s t d : : s t r i n g s s = s . t o S t dS t r i n g ( ) ;QStr ing s2 = QStr ing : : f r omStdS t r i ng ( s s ) ;

33 / 39

Page 34: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes II

There are methods to convert a QString in a number andviceversa.

QStr ing s = "145" ;int a = s . t o I n t ( ) ;double d = 3 . 8 ;QStr ing s3 = QStr ing : : number ( d ) ;

34 / 39

Page 35: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes III

QVector

This is a template class that provides a dynamic array.

It stores its items in adjacent memory locations and pro-vides fast index-based access.

It offers operations similar to the STL vector.

It can be converted to an STL vector and viceversa (functiontoStdVector, fromStdVector).

35 / 39

Page 36: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes IV

QMessageBox

Provides a modal dialog to show information.

It can display text, an item and standard buttons (OK, Can-cel, Open, Close, Save, Discard, etc.) for user response.

There are 4 message types, which really only differ in thepredefined icon they each show:

QuestionInformationWarningCritical

36 / 39

Page 37: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes V

QMessageBox : : i n f o rma t i o n ( this , "Info" , "Selection

changed" ) ;// ...

int r e t = QMessageBox : : warn ing ( this , "My Application

" , "The document has been modified.Do you want

to save your changes?" , QMessageBox : : Save |QMessageBox : : D i s c a rd |QMessageBox : : Cancel ,QMessageBox : : Save ) ;

if ( r e t == QMessageBox : : Save ) {//do save

}

37 / 39

Page 38: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Useful Qt classes VI

QDebug

Provides an output stream for debugging information.

It is useful to call the qDebug() function to obtain a defaultQDebug object to use for writing debugging information.

In VS, the message will be displayed in the ”Output” win-dow.

qDebug ( ) << "Here is a message for debug." ;

38 / 39

Page 39: Object-Oriented Programmingiuliana/oop/Lectures/Lecture_10.pdf · Object-Oriented Programming Iuliana Bocicor Signals and slots User de ned signals and slots Notepad example Drawing

Object-Oriented

Programming

IulianaBocicor

Signals andslots

User definedsignals andslots

Notepadexample

Drawing withQt

Other usefulQt classes

Summary

Signals and slots are used for communication between ob-jects.

A signal is emitted when a particular event occurs.

A slot is a function called in response to a particular signal.

Signals and slots must be connected.

39 / 39