Top Banner
QT Programming QT Programming Ruku Ruku Roychowdhury Roychowdhury
21

QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Jan 21, 2016

Download

Documents

Kristin Wilkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

QT ProgrammingQT Programming

Ruku Ruku RoychowdhuryRoychowdhury

Page 2: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Background• QT is a cross platform application framework.

• Widely used to develop GUI applications.

• Originally developed by Norwegian company Trolltech.

• After Nokia's acquisition of TrollTech, now Nokia's Qt Development Frameworks division is enhancing QT.

Page 3: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Some Applications with QT

• Google Earth

• KDE

• Adobe Photoshop Album

• Skype

• VLC media player

• VirtualBox

Page 4: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Features• Cross-platform application framework.

• Qt uses standard C++.

• Makes extensive use of Meta Object Compiler together with several macros to enrich the language.

• Can be used with many other languages by Language Binding.

Page 5: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

C++ Features• Procedural as well as Object oriented language

• Abstraction

• Inheritance– Allows Multiple inheritance

• Polymorphism– Runtime Polymorphism (Virtual functions)– Static Polymorphism (Function Overloading)

Page 6: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

QT Class Hierarchy

Multiple Inheritance

Page 7: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Event Handling• Event handling is done by Signals and Slots

• Signal– Emitted when a particular event occurs (e.g., clicked())– Qt widgets: predefined signals– Developer can create custom signals

• Slot– Function called in response to a signal– Qt widgets: predefined slots– Developer can create custom slots

• Connection: – connects signals to slots – established by developer– handled by Qt framework

Page 8: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Connections

Page 9: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Meta Object System• Meta information not supported by standard C++

– QObject class• Base class for objects that use meta-object system

– Q_OBJECT macro• Enables meta-object features

– “moc” tool• –Parses Q_OBJECT macro• –Extends source code with additional functions (extra

files)

Used for: getting list of signals & slots, properties and text translation

Page 10: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Meta-Object FeaturesFeatures provided by meta-object code:• Signals and slots

• metaObject() –returns associated meta-object for the class

• className() –returns class name as a string, without requiring native run-time type information (RTTI) support through the C++ compiler

• inherits() –returns whether this instance inherits the specified QObjectclass

• tr() –translate strings for internationalization

• setProperty() and property() –set and get properties of object by Property name

Page 11: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Signals and Slots (in details)

• QT signals and slots are TYPE SAFE– Signals & slots have to have exactly same parameters– It does not give any compilation error / warning– But gives runtime error when executing connect():

– No connection if either sender or receiver doesn’t exist– Or if signatures of signal and slot do not match

• connect() returns boolean value indicating success

Page 12: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Multiple Signal-Slot Connections

• One signal can be connected to multiple slots and vice versa also holds good.

Page 13: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Demonstration

• Every QWidget has a set of predefined signals and slots.

• A QPushButton and QRadioButton has predefined signals such as clicked() which they inherit from QAbstractButton.

• Slots can be written for these predefined signals and can be called directly without associating through connect().

Page 14: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Custom Signals and Slots

• A custom signal and slot can also be created.

• Custom signals and slots can be created in user’s own classes.– E.g. mysignal signal in the code sample– E.g. myslot slot in the code sample

• These signals and slot need to be explicitly connected with the connect() statement.– E.g.

QObject::connect(this,SIGNAL(mysignal()),this,SLOT(myslot())); in code sample

Page 15: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Example• Converter Constructor• converter2::converter2(QWidget *parent) : QWidget(parent), ui(new

Ui::converter2) { ui->setupUi(this); ui->label->setText("Unit1"); ui->label_2->setText("Unit2"); QObject::connect(this,SIGNAL(mysignal()),this,SLOT(myslot())); input=""; }

• Signal • Void converter2::on_pushButton_2_clicked() { emit mysignal(); }

• Slot Declaration• void converter2::myslot()

{ time_t now; time(&now); QString time=ctime(&now); ui->textEdit->setText(time); }

Page 16: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Creating custom signals and slots in the QWidgets

• User can create his own custom signals and slots for a QWidget.

• User has to extend any widget to create his custom subclass of that widget.– E.g. customButton is extended from QPushButton.

• It has custom signal clicked(int id) and slot click() in addition to the predefined signals and slots in QPushButton class.

Page 17: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Example• Custom Button Class

• class customButton: public QPushButton{ Q_OBJECT public: customButton(const QString& text, int id,QWidget* parent = NULL); customButton(const QString& text, QWidget* parent = NULL); private: static int nextId; int m_id; public slots: void click(); signals:void clicked (int id); };//Custom signal for CustomButton

Page 18: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Compilation

• Every class is declared in the header file.• Every class is defined in the .cpp file.

– For example, converter_2.h contains the class declarations

– Converter_2.cpp contains the class definitions.

converter_2.h

moc_converter_2.cpp

Moc parses .h file change the QT specific code to plain c++ code

+ converter_2.cpp

Linker links both of these .cpp file

Page 19: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Meta Object Compilation

• const QMetaObject QObject::staticMetaObject method is used to have meta object of the class defined in .h files.

• Moc generated code places data into two arrays

– qt_meta_data_<Classname> array• In the qt_meta_data array moc creates a own line for every QMeta

related item

– qt_meta_stringdata_<Classname> array• In the qt_meta_stringdata array moc puts all the method,

parameter, slot, signal.

Page 20: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Contd.

• when a signal is called, QMetaObject::activate() is called with the signal’s name and the signal’s arguments.

• The activate() function then figures out which connections have been established and fetches the names for the appropriate slots.

• Then it calls qt_metacall with the slot names and the arguments given to the signal.

• the metacall function delegates this with the help of a large switch—case statement to the real slots.

Page 21: QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.

Thank You