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 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.