Skip to content

Signals, Slots, and Events

shua edited this page Jan 18, 2012 · 2 revisions

This tutorial should give an overview about architectural principles of the engine. You will not learn how to program a game from this tutorial, though you are encouraged to get to know the engine a bit first. You have been warned! For a list of all tutorials visit the Tutorials page in this wiki.


Our engine uses Qt's signal and slot methods to trigger events. Signals and slots are defined in a header, and then connected to each other later.

Create a signal

In order to create a signal or slot, use the signals or slots keywords in your header.

class MyClass : public QObject {
signals:
    void newSignal(int variableToPass)
public slots:
    int newSlot(int PassedVariable)
}

You may notice slots has the word 'public' before it. This is because slots are regular functions that can be called outside of Qt's signal/slot methods. You define what it does in the cpp file.

int MyClass::newSlot(int PassedVariable) {
    return PassedVariable;
}

Signals should not be defined and should only be called with the 'emit' keyword.

void MyClass::someFunction() {
    emit newSignal(4);
} 

Signals are connected to slots using the QObject::connect() function.

void MyClass::MyClass() {
    OtherClass oc;
    connect(this, SIGNAL(newSlot(int)), oc, SLOT(otherClassSlot(int)));
}

Our engine also uses an event system to send program information over a network. WIP

Create an Event WIP

In order to create an event, you must define it in your program

class CustomEvent : public dt::NetworkEvent {
    CustomEvent(QString text);
    const QString GetType
}

In order to put an event into the network queue use the NetworkManager's QueueEvent method:

dt::NetworkManager::Get()->QueueEvent(std::make_shared<CustomEvent>("Custom event text", intVar, floatVar, etcVar));

The NetworkManager will Queue the event and send it with all the other queued items.

Event Listeners

In order to recieve network events you must connect NetworkManager's NewEvent signal to a slot in your program:

connect((QObject*)dt::NetworkManager::Get(), SIGNAL(NewEvent(std::shared_ptr<dt::NetworkEvent>)), this, SLOT(HandleEventSlot(std::shared_ptr<dt::NetworkEvent>)));