Custom Type Sending Example


In the Custom Type Example, we showed how to integrate custom types with the meta-object system, enabling them to be stored in QVariant objects, written out in debugging information and used in signal-slot communication.

In this example, we demonstrate that the preparations made to the Message class and its declaration with Q_DECLARE_METATYPE() enable it to be used with direct signal-slot connections. We do this by creating a Window class containing signals and slots whose signatures include Message arguments.

The Window and Message Class Definitions

We define a simple Window class with a signal and public slot that allow a Message object to be sent via a signal-slot connection:

class Window : public QWidget


    void messageSent(const Message &message);

public slots:
    void setMessage(const Message &message);

private slots:
    void sendMessage();

    Message thisMessage;
    QTextEdit *editor;

The window will contain a text editor to show the contents of a message and a push button that the user can click to send a message. To facilitate this, we also define the sendMessage() slot. We also keep a Message instance in the thisMessage private variable which holds the actual message to be sent.

The Message class is defined in the following way:

class Message
    Message(const Message &other);

    Message(const QString &body, const QStringList &headers);

    QString body() const;
    QStringList headers() const;

    QString m_body;
    QStringList m_headers;

The type is declared to the meta-type system with the Q_DECLARE_METATYPE() macro:

This will make the type available for use in direct signal-slot connections.

The Window Class Implementation

The Window constructor sets up a user interface containing a text editor and a push button.

    editor = new QTextEdit();
    QPushButton *sendButton = new QPushButton(tr("&Send message"));

    connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage()));

    QHBoxLayout *buttonLayout = new QHBoxLayout();

    QVBoxLayout *layout = new QVBoxLayout(this);

    setWindowTitle(tr("Custom Type Sending"));

The button's clicked() signal is connected to the window's sendMessage() slot, which emits the messageSent(Message) signal with the Message held by the thisMessage variable:

void Window::sendMessage()
    thisMessage = Message(editor->toPlainText(), thisMessage.headers());
    emit messageSent(thisMessage);

We implement a slot to allow the message to be received, and this also lets us set the message in the window programatically:

void Window::setMessage(const Message &message)
    thisMessage = message;

In this function, we simply assign the new message to thisMessage and update the text in the editor.

Making the Connection

In the example's main() function, we perform the connection between two instances of the Window class:

int main(int argc, char *argv[])
    QApplication app(argc, argv);

    Window window1;
    QStringList headers;
    headers << "Subject: Hello World"
            << "From:";
    QString body = "This is a test.\r\n";
    Message message(body, headers);

    Window window2;
    QObject::connect(&window1, SIGNAL(messageSent(Message)),
                     &window2, SLOT(setMessage(Message)));
    QObject::connect(&window2, SIGNAL(messageSent(Message)),
                     &window1, SLOT(setMessage(Message)));;;
    return app.exec();

We set the message for the first window and connect the messageSent(Message) signal from each window to the other's setMessage(Message) slot. Since the signals and slots mechanism is only concerned with the type, we can simplify the signatures of both the signal and slot when we make the connection.

When the user clicks on the Send message button in either window, the message shown will be emitted in a signal that the other window will receive and display.

Further Reading

Although the custom Message type can be used with direct signals and slots, an additional registration step needs to be performed if you want to use it with queued signal-slot connections. See the Queued Custom Type Example for details.

More information on using custom types with Qt can be found in the Creating Custom Qt Types document.


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.