Your SlideShare is downloading. ×
  • Like
Qt Memory Management & Signal and Slots
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Qt Memory Management & Signal and Slots

  • 6,413 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
6,413
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
197
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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