1. Qt uses object-oriented memory management where objects are deleted automatically when their parent object is deleted.
2. Signals and slots provide a mechanism for communication between Qt objects using signals that are emitted by one object and caught by slot functions in other objects.
3. A signal mapper can be used to map multiple signals to a single slot so that different logic can be handled depending on which signal was emitted.
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Qt Memory Management & Signals and Slots Guide
1. Qt Memory Management &Signals and Slots Jussi Pohjolainen Tampere University of Applied Sciences
2. About Memory Management In Java: Garbage Collector In C++: stack, heap, static Stack: release is done automatically when out of scope Heap: release is done by the programmer Qt is C++, but it has it's "own way" of releasing heap-objects.
3. Qt Object Trees QObjects organize themselves in object trees When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.
4. Simple Example int main() { QWidget window; QPushButton* quit = new QPushButton("Quit", &window); //QPushButton is released, when //QWidget is out of scope }
5. QObject Automatic Memory Handling works only when class is inherited from QObject Every GUI-classes (widgets) are inherited from QObject.
8. Qt's Meta-Object System Meta-object system: extension to C++ by Qt signals-slots (event-handling) Introspection without RTTI className() Internationalization tr() Setting properties dynamically setProperty()
9. Meta Object Compiler (moc) Enabling meta-object features: class Counter : public QObject { Q_OBJECT ... moc generates another .cpp file counter.cpp -> moc_counter.cpp
10. Signal & Slots Qt's event handling mechanism Signals are emitted by widgets when something happens Slots are used to handle signals Most of the work is done by Qt's meta classes and macros. Code can look strange, but in the end, it's standard C++.
11. Signal & Slots Communication between objects In Java: Event listener Requires additional work (interfaces etc) Connecting: QObject::connect(exitButton, SIGNAL( clicked() ), &app, SLOT( quit() ));
13. Defining Signals and Slots The signals and slots are available to any QObject's subclass. Slots are normal methods Signals are just declarations used by the moc compiler
14. Example class Counter : public QObject { Q_OBJECT public: Counter() { m_value = 0; } int value() const { return m_value; } public slots: void setValue(int value); signals: void valueChanged(int newValue); private: int m_value; };
17. About Signals and Slots Only available if class inherites QObject Truly independent components, objects don't know of each other Multiple signals to one slot One signal to multiple slots
18. SignalMapper Multiple signals to one slot Multiple buttons -> something happens We want different logic depending on the button