Qt Reference Documentation

Custom Type Sending Example

Files:

The Custom Type Sending example shows how to use a custom type with signals and slots.

Overview

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
 {
     Q_OBJECT

 public:
     Window();

 signals:
     void messageSent(const Message &message);

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

 private slots:
     void sendMessage();

 private:
     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
 {
 public:
     Message();
     Message(const Message &other);
     ~Message();

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

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

 private:
     QString m_body;
     QStringList m_headers;
 };

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

 Q_DECLARE_METATYPE(Message);

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.

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

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

     QHBoxLayout *buttonLayout = new QHBoxLayout();
     buttonLayout->addStretch();
     buttonLayout->addWidget(sendButton);
     buttonLayout->addStretch();

     QVBoxLayout *layout = new QVBoxLayout(this);
     layout->addWidget(editor);
     layout->addLayout(buttonLayout);

     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;
     editor->setPlainText(thisMessage.body());
 }

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: qt-info@nokia.com";
     QString body = "This is a test.\r\n";
     Message message(body, headers);
     window1.setMessage(message);

     Window window2;
     QObject::connect(&window1, SIGNAL(messageSent(Message)),
                      &window2, SLOT(setMessage(Message)));
     QObject::connect(&window2, SIGNAL(messageSent(Message)),
                      &window1, SLOT(setMessage(Message)));
     window1.show();
     window2.show();
     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.