path: root/doc
diff options
authorOlivier Goffart <>2011-11-23 15:06:30 +0100
committerQt by Nokia <>2011-11-25 01:12:14 +0100
commit583c55b243d9894d93d32fbe15bece2a9beb1d10 (patch)
treedb6237144cfb511cac1468bf851cf66639a84367 /doc
parent79f675a1e0f628bbc25345ebc1eb1f5809166c6b (diff)
New QObject connection syntax
In addition to the old connection syntax, you can now connect using function pointers. connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue ); You can connect also to functor or C++11 lambdas The connections are now type safe (no more problems with namespaces or typedefs). Implicit type conversion is also supported. The new syntax forces us to change the meaning of signal form protected to public, in order to be able to access the signal's address everywhere The way it works is by introducing new overload of QObject::connect that take function pointer as parametter. Those new overload are template function, that are implemented inline. The actual implementation is in QObject::connectImpl which take a QObject::QSlotObject* as parametter for the slot. That slot object contains a virtual function which call the slot which has to be implemented in the header as it depends on the template parametter. So the internals of QObjectPrivate::Connection will store this QObjectSlot* in order to be able to make the call. You can read a full description here: History of commits before it was imported on gerrit: Thread on the mailing list: (The discussions on the mailing list were about trying to find a solution that do not need making signals public, but no user friendly solution was found) Note: support for QueuedConnection, and the symetric QObject::disconnect is added in another commit. Qt::UniqueConnection is not supported yet in the new overload. Change-Id: I67d08436b0720e7f2992be9f7e34770960fa58fa Reviewed-by: Thiago Macieira <>
Diffstat (limited to 'doc')
1 files changed, 23 insertions, 0 deletions
diff --git a/doc/src/snippets/code/src_corelib_kernel_qobject.cpp b/doc/src/snippets/code/src_corelib_kernel_qobject.cpp
index 086c62893d..11b70cc1ab 100644
--- a/doc/src/snippets/code/src_corelib_kernel_qobject.cpp
+++ b/doc/src/snippets/code/src_corelib_kernel_qobject.cpp
@@ -448,6 +448,29 @@ QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDi
QList<QPushButton *> childButtons = parentWidget.findChildren<QPushButton *>(QString(), Qt::FindDirectChildOnly);
//! [43]
+//! [44]
+QLabel *label = new QLabel;
+QLineEdit *lineEdit = new QLineEdit;
+QObject::connect(lineEdit, &QLineEdit::textChanged,
+ label, &QLabel::setText);
+//! [44]
+//! [45]
+void someFunction();
+QPushButton *button = new QPushButton;
+QObject::connect(button, &QPushButton::clicked, someFunction);
+//! [45]
+//! [46]
+QByteArray page = ...;
+QTcpSocket *socket = new QTcpSocket;
+socket->connectToHost("", 80);
+QObject::connect(socket, &QTcpSocket::connected, [=] () {
+ socket->write("GET " + page + "\r\n");
+ });
+//! [46]
//! [meta data]
//: This is a comment for the translator.