Upload
kristin-wilkins
View
252
Download
0
Embed Size (px)
Citation preview
QT ProgrammingQT Programming
Ruku Ruku RoychowdhuryRoychowdhury
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.
Some Applications with QT
• Google Earth
• KDE
• Adobe Photoshop Album
• Skype
• VLC media player
• VirtualBox
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.
C++ Features• Procedural as well as Object oriented language
• Abstraction
• Inheritance– Allows Multiple inheritance
• Polymorphism– Runtime Polymorphism (Virtual functions)– Static Polymorphism (Function Overloading)
QT Class Hierarchy
Multiple Inheritance
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
Connections
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
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
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
Multiple Signal-Slot Connections
• One signal can be connected to multiple slots and vice versa also holds good.
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().
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
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); }
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.
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
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
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.
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.
Thank You