Qt Memory Management & Signal and Slots
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Qt Memory Management & Signal and Slots

on

  • 8,871 views

 

Statistics

Views

Total Views
8,871
Views on SlideShare
8,706
Embed Views
165

Actions

Likes
1
Downloads
188
Comments
0

5 Embeds 165

http://windsplife.blogspot.tw 74
http://windsplife.blogspot.com 42
http://www.slideshare.net 33
http://a-ot042.ning.com 13
http://php.tpu.fi 3

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Qt Memory Management & Signal and Slots Presentation Transcript

  • 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.
  • 6.
  • 7. SIGNAL & SLots
  • 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() ));
  • 12. Signal & Slots
    QObject::connect(exitButton, // Sender
    SIGNAL( clicked() ), // Sender's signal
    &app, // Receiving object
    SLOT( quit() )); // Receiver's slot
    QPushButton
    QApplication
    Signals
    Signals
    clicked()
    Slots
    Slots
    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;
    };
  • 15. Example
    #include "counter.h"
    void Counter::setValue(int value)
    {
    if (value != m_value) {
    m_value = value;
    emit valueChanged(value);
    }
    }
  • 16. Example
    #include <QtCore/QCoreApplication>
    #include "counter.h"
    int main(int argc, char *argv[])
    {
    QCoreApplication app(argc, argv);
    Counter a, b;
    QObject::connect(&a, SIGNAL(valueChanged(int)),
    &b, SLOT(setValue(int)));
    a.setValue(12); // a.value() == 12, b.value() == 12
    b.setValue(48); // a.value() == 12, b.value() == 48
    return app.exec();
    }
  • 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
  • 19. #include <QtGui>
    #include "listener.h"
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QFrame parent;
    QVBoxLayout* layout = new QVBoxLayout(&parent);
    QPushButton* push1 = new QPushButton("Hello");
    QPushButton* push2 = new QPushButton("World");
    layout->addWidget(push1);
    layout->addWidget(push2);
    QSignalMapper* signalMapper = new QSignalMapper(&parent);
    signalMapper->setMapping(push1, QString("Hello"));
    signalMapper->setMapping(push2, QString("World"));
    QObject::connect(push1,
    SIGNAL(clicked()),
    signalMapper,
    SLOT(map()));
    QObject::connect(push2,
    SIGNAL(clicked()),
    signalMapper,
    SLOT(map()));
    Listener* listener = new Listener(&parent);
    QObject::connect(signalMapper,
    SIGNAL(mapped(const QString &)),
    listener,
    SLOT(buttonWasClicked(const QString &)));
    parent.show();
    return a.exec();
    }
  • 20. Listener
    #include "listener.h"
    Listener::Listener(QObject* parent) : QObject(parent) { }
    void Listener::buttonWasClicked(const QString& whichButton) {
    if(whichButton == "Hello")
    qDebug() << "Hello was pressed!";
    else if(whichButton == "World")
    qDebug() << "World was pressed";
    }
  • 21. push1
    SignalMapper
    buttonWasClicked("Hello")
    push1 => "Hello"
    push2 => "World"
    clicked()
    slots:
    map()
    signals:
    mapped(QString)
    clicked()
    buttonWasClicked("World")
    push2