Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally."— Presentation transcript:

1 QT Programming QT Programming Ruku Roychowdhury

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

3 Some Applications with QT Google Earth KDE Adobe Photoshop Album Skype VLC media player VirtualBox

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

5 C++ Features Procedural as well as Object oriented language Abstraction Inheritance –Allows Multiple inheritance Polymorphism –Runtime Polymorphism (Virtual functions) –Static Polymorphism (Function Overloading)

6 QT Class Hierarchy Multiple Inheritance

7 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

8 Connections

9 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

10 Meta-Object Features Features 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

11 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

12 Multiple Signal-Slot Connections One signal can be connected to multiple slots and vice versa also holds good.

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

14 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

15 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); }

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

17 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

18 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

19 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_ array In the qt_meta_data array moc creates a own line for every QMeta related item –qt_meta_stringdata_ array In the qt_meta_stringdata array moc puts all the method, parameter, slot, signal.

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

21 Thank You


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

Similar presentations


Ads by Google