Qt Memory Management & Signal and Slots

8,189 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,189
On SlideShare
0
From Embeds
0
Number of Embeds
187
Actions
Shares
0
Downloads
218
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Qt Memory Management & Signal and Slots

  1. 1. Qt Memory Management &Signals and Slots<br />Jussi Pohjolainen<br />Tampere University of Applied Sciences<br />
  2. 2. About Memory Management<br />In Java: Garbage Collector<br />In C++: stack, heap, static<br />Stack: release is done automatically when out of scope<br />Heap: release is done by the programmer<br />Qt is C++, but it has it&apos;s &quot;own way&quot; of releasing heap-objects.<br />
  3. 3. Qt Object Trees<br />QObjects organize themselves in object trees<br />When you create a QObject with another object as parent, it&apos;s added to the parent&apos;s children() list, and is deleted when the parent is.<br />
  4. 4. Simple Example<br /> int main()<br /> { <br /> QWidget window; <br /> QPushButton* quit = <br /> new QPushButton(&quot;Quit&quot;, &window);<br /> //QPushButton is released, when <br /> //QWidget is out of scope<br /> }<br />
  5. 5. QObject<br />Automatic Memory Handling works only when class is inherited from QObject<br />Every GUI-classes (widgets) are inherited from QObject.<br />
  6. 6.
  7. 7. SIGNAL & SLots<br />
  8. 8. Qt&apos;s Meta-Object System<br />Meta-object system: extension to C++ by Qt<br />signals-slots (event-handling)<br />Introspection without RTTI<br />className()<br />Internationalization <br />tr()<br />Setting properties dynamically<br />setProperty()<br />
  9. 9. Meta Object Compiler (moc)<br />Enabling meta-object features:<br />class Counter : public QObject<br />{<br /> Q_OBJECT<br />...<br />moc generates another .cpp file<br />counter.cpp -&gt; moc_counter.cpp<br />
  10. 10. Signal & Slots<br />Qt&apos;s event handling mechanism<br />Signals are emitted by widgets when something happens<br />Slots are used to handle signals<br />Most of the work is done by Qt&apos;s meta classes and macros. Code can look strange, but in the end, it&apos;s standard C++.<br />
  11. 11. Signal & Slots<br />Communication between objects<br />In Java: Event listener<br />Requires additional work (interfaces etc)<br />Connecting:<br />QObject::connect(exitButton,<br /> SIGNAL( clicked() ), <br /> &app, <br /> SLOT( quit() ));<br />
  12. 12. Signal & Slots<br />QObject::connect(exitButton, // Sender<br /> SIGNAL( clicked() ), // Sender&apos;s signal <br /> &app, // Receiving object<br /> SLOT( quit() )); // Receiver&apos;s slot<br />QPushButton<br />QApplication<br />Signals<br />Signals<br />clicked()<br />Slots<br />Slots<br />quit()<br />
  13. 13. Defining Signals and Slots<br />The signals and slots are available to any QObject&apos;s subclass.<br />Slots are normal methods<br />Signals are just declarations used by the moc compiler<br />
  14. 14. Example<br />class Counter : public QObject<br /> {<br />Q_OBJECT<br /> public:<br /> Counter() { m_value = 0; }<br /> int value() const { return m_value; }<br /> public slots:<br /> void setValue(int value);<br /> signals:<br /> void valueChanged(int newValue);<br /> private:<br /> int m_value;<br /> };<br />
  15. 15. Example<br />#include &quot;counter.h&quot;<br />void Counter::setValue(int value)<br /> {<br /> if (value != m_value) {<br /> m_value = value;<br /> emit valueChanged(value);<br /> }<br /> }<br />
  16. 16. Example<br />#include &lt;QtCore/QCoreApplication&gt;<br />#include &quot;counter.h&quot;<br />int main(int argc, char *argv[])<br />{<br /> QCoreApplication app(argc, argv);<br /> Counter a, b;<br /> QObject::connect(&a, SIGNAL(valueChanged(int)),<br /> &b, SLOT(setValue(int)));<br /> a.setValue(12); // a.value() == 12, b.value() == 12<br /> b.setValue(48); // a.value() == 12, b.value() == 48<br /> return app.exec();<br />}<br />
  17. 17. About Signals and Slots<br />Only available if class inherites QObject<br />Truly independent components, objects don&apos;t know of each other<br />Multiple signals to one slot<br />One signal to multiple slots<br />
  18. 18. SignalMapper<br />Multiple signals to one slot<br />Multiple buttons -&gt; something happens<br />We want different logic depending on the button<br />
  19. 19. #include &lt;QtGui&gt;<br />#include &quot;listener.h&quot;<br />int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> QFrame parent;<br /> QVBoxLayout* layout = new QVBoxLayout(&parent);<br /> QPushButton* push1 = new QPushButton(&quot;Hello&quot;);<br /> QPushButton* push2 = new QPushButton(&quot;World&quot;);<br /> layout-&gt;addWidget(push1);<br /> layout-&gt;addWidget(push2);<br /> QSignalMapper* signalMapper = new QSignalMapper(&parent);<br /> signalMapper-&gt;setMapping(push1, QString(&quot;Hello&quot;));<br /> signalMapper-&gt;setMapping(push2, QString(&quot;World&quot;));<br /> QObject::connect(push1,<br /> SIGNAL(clicked()),<br /> signalMapper,<br /> SLOT(map()));<br /> QObject::connect(push2,<br /> SIGNAL(clicked()),<br /> signalMapper,<br /> SLOT(map()));<br /> Listener* listener = new Listener(&parent);<br /> QObject::connect(signalMapper,<br /> SIGNAL(mapped(const QString &)),<br /> listener,<br /> SLOT(buttonWasClicked(const QString &))); <br /> parent.show();<br /> return a.exec();<br />}<br />
  20. 20. Listener<br />#include &quot;listener.h&quot;<br />Listener::Listener(QObject* parent) : QObject(parent) { }<br />void Listener::buttonWasClicked(const QString& whichButton) {<br /> if(whichButton == &quot;Hello&quot;)<br /> qDebug() &lt;&lt; &quot;Hello was pressed!&quot;;<br /> else if(whichButton == &quot;World&quot;)<br /> qDebug() &lt;&lt; &quot;World was pressed&quot;;<br />}<br />
  21. 21. push1<br />SignalMapper<br />buttonWasClicked(&quot;Hello&quot;)<br />push1 =&gt; &quot;Hello&quot;<br />push2 =&gt; &quot;World&quot;<br />clicked()<br />slots:<br /> map()<br />signals:<br /> mapped(QString)<br />clicked()<br />buttonWasClicked(&quot;World&quot;)<br />push2<br />

×