1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 QLineEdit *lineEdit = static_cast<QLineEdit *>(
43 qt_find_obj_child(myWidget, "QLineEdit", "my line edit"));
45 lineEdit->setText("Default");
50 QObject *obj = new QPushButton;
51 obj->metaObject()->className(); // returns "QPushButton"
53 QPushButton::staticMetaObject.className(); // returns "QPushButton"
58 QPushButton::staticMetaObject.className(); // returns "QPushButton"
60 QObject *obj = new QPushButton;
61 obj->metaObject()->className(); // returns "QPushButton"
66 QObject *obj = new QTimer; // QTimer inherits QObject
68 QTimer *timer = qobject_cast<QTimer *>(obj);
69 // timer == (QObject *)obj
71 QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
77 QTimer *timer = new QTimer; // QTimer inherits QObject
78 timer->inherits("QTimer"); // returns true
79 timer->inherits("QObject"); // returns true
80 timer->inherits("QAbstractButton"); // returns false
82 // QVBoxLayout inherits QObject and QLayoutItem
83 QVBoxLayout *layout = new QVBoxLayout;
84 layout->inherits("QObject"); // returns true
85 layout->inherits("QLayoutItem"); // returns true (even though QLayoutItem is not a QObject)
90 qDebug("MyClass::setPrecision(): (%s) invalid precision %f",
91 qPrintable(objectName()), newPrecision);
96 class MainWindow : public QMainWindow
102 bool eventFilter(QObject *obj, QEvent *ev);
108 MainWindow::MainWindow()
110 textEdit = new QTextEdit;
111 setCentralWidget(textEdit);
113 textEdit->installEventFilter(this);
116 bool MainWindow::eventFilter(QObject *obj, QEvent *event)
118 if (obj == textEdit) {
119 if (event->type() == QEvent::KeyPress) {
120 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
121 qDebug() << "Ate key press" << keyEvent->key();
127 // pass the event on to the parent class
128 return QMainWindow::eventFilter(obj, event);
135 myObject->moveToThread(QApplication::instance()->thread());
140 class MyObject : public QObject
145 MyObject(QObject *parent = 0);
148 void timerEvent(QTimerEvent *event);
151 MyObject::MyObject(QObject *parent)
154 startTimer(50); // 50-millisecond timer
155 startTimer(1000); // 1-second timer
156 startTimer(60000); // 1-minute timer
159 void MyObject::timerEvent(QTimerEvent *event)
161 qDebug() << "Timer ID:" << event->timerId();
167 QList<QObject *> list = window()->queryList("QAbstractButton"));
168 foreach (QObject *obj, list)
169 static_cast<QAbstractButton *>(obj)->setEnabled(false);
174 QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
179 QListWidget *list = parentWidget->findChild<QListWidget *>();
184 QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname");
189 QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
194 monitoredObj->installEventFilter(filterObj);
199 class KeyPressEater : public QObject
205 bool eventFilter(QObject *obj, QEvent *event);
208 bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
210 if (event->type() == QEvent::KeyPress) {
211 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
212 qDebug("Ate key press %d", keyEvent->key());
215 // standard event processing
216 return QObject::eventFilter(obj, event);
223 KeyPressEater *keyPressEater = new KeyPressEater(this);
224 QPushButton *pushButton = new QPushButton(this);
225 QListView *listView = new QListView(this);
227 pushButton->installEventFilter(keyPressEater);
228 listView->installEventFilter(keyPressEater);
235 QLabel *senderLabel = new QLabel(tr("Name:"));
236 QLabel *recipientLabel = new QLabel(tr("Name:", "recipient"));
242 int n = messages.count();
243 showMessage(tr("%n message(s) saved", "", n));
248 n == 1 ? tr("%n message saved") : tr("%n messages saved")
253 label->setText(tr("F\374r \310lise"));
258 if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) {
260 get_the_value(&data); // expensive operation
261 emit valueChanged(data);
267 QLabel *label = new QLabel;
268 QScrollBar *scrollBar = new QScrollBar;
269 QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
270 label, SLOT(setNum(int)));
276 QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
277 label, SLOT(setNum(int value)));
282 class MyWidget : public QWidget
290 void buttonClicked();
293 QPushButton *myButton;
298 myButton = new QPushButton(this);
299 connect(myButton, SIGNAL(clicked()),
300 this, SIGNAL(buttonClicked()));
306 QObject::connect: Cannot queue arguments of type 'MyType'
307 (Make sure 'MyType' is registered using qRegisterMetaType().)
312 disconnect(myObject, 0, 0, 0);
317 myObject->disconnect();
322 disconnect(myObject, SIGNAL(mySignal()), 0, 0);
327 myObject->disconnect(SIGNAL(mySignal()));
332 disconnect(myObject, 0, myReceiver, 0);
337 myObject->disconnect(myReceiver);
342 if (signal == QMetaMethod::fromSignal(&MyObject::valueChanged)) {
343 // signal is valueChanged
349 void on_<object name>_<signal name>(<signal parameters>);
354 void on_button1_clicked();
359 class MyClass : public QObject
362 Q_CLASSINFO("Author", "Pierre Gendron")
363 Q_CLASSINFO("URL", "http://www.my-organization.qc.ca")
375 [RESET resetFunction]
376 [NOTIFY notifySignal]
387 Q_PROPERTY(QString title READ title WRITE setTitle USER true)
392 class MyClass : public QObject
398 MyClass(QObject *parent = 0);
401 enum Priority { High, Low, VeryHigh, VeryLow };
402 void setPriority(Priority priority);
403 Priority priority() const;
409 class QLibrary : public QObject
412 Q_FLAGS(LoadHint LoadHints)
420 ResolveAllSymbolsHint = 0x01,
421 ExportExternalSymbolsHint = 0x02,
422 LoadArchiveMemberHint = 0x04
424 Q_DECLARE_FLAGS(LoadHints, LoadHint)
430 //: This name refers to a host name.
431 hostNameLabel->setText(tr("Name:"));
433 /*: This text refers to a C++ code example. */
434 QString example = tr("Example");
438 QPushButton *button = parentWidget->findChild<QPushButton *>("button1", Qt::FindDirectChildOnly);
443 QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDirectChildOnly);
448 QList<QPushButton *> childButtons = parentWidget.findChildren<QPushButton *>(QString(), Qt::FindDirectChildOnly);
452 QLabel *label = new QLabel;
453 QLineEdit *lineEdit = new QLineEdit;
454 QObject::connect(lineEdit, &QLineEdit::textChanged,
455 label, &QLabel::setText);
460 QPushButton *button = new QPushButton;
461 QObject::connect(button, &QPushButton::clicked, someFunction);
465 QByteArray page = ...;
466 QTcpSocket *socket = new QTcpSocket;
467 socket->connectToHost("qt-project.org", 80);
468 QObject::connect(socket, &QTcpSocket::connected, [=] () {
469 socket->write("GET " + page + "\r\n");
474 disconnect(myObject, &MyObject::mySignal(), 0, 0);
478 QObject::disconnect(lineEdit, &QLineEdit::textChanged,
479 label, &QLabel::setText);
483 if (isSignalConnected(QMethaMethod::fromSignal(&MyObject::valueChanged))) {
485 data = get_the_value(); // expensive operation
486 emit valueChanged(data);
491 //: This is a comment for the translator.
493 //~ loc-layout_id foo_dialog
495 //~ magic-stuff This might mean something magic.
496 QString text = MyMagicClass::tr("Sim sala bim.");
499 //! [explicit tr context]
500 QString text = QScrollBar::tr("Page up");
501 //! [explicit tr context]