Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_kernel_qobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4//! [1]
5QObject *obj = new QPushButton;
6obj->metaObject()->className(); // returns "QPushButton"
7
8QPushButton::staticMetaObject.className(); // returns "QPushButton"
9//! [1]
10
11
12//! [2]
13QPushButton::staticMetaObject.className(); // returns "QPushButton"
14
15QObject *obj = new QPushButton;
16obj->metaObject()->className(); // returns "QPushButton"
17//! [2]
18
19
20//! [3]
21QObject *obj = new QTimer; // QTimer inherits QObject
22
23QTimer *timer = qobject_cast<QTimer *>(obj);
24// timer == (QObject *)obj
25
26QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
27// button == nullptr
28//! [3]
29
30
31//! [4]
32QTimer *timer = new QTimer; // QTimer inherits QObject
33timer->inherits("QTimer"); // returns true
34timer->inherits("QObject"); // returns true
35timer->inherits("QAbstractButton"); // returns false
36
37// QVBoxLayout inherits QObject and QLayoutItem
38QVBoxLayout *layout = new QVBoxLayout;
39layout->inherits("QObject"); // returns true
40layout->inherits("QLayoutItem"); // returns true (even though QLayoutItem is not a QObject)
41//! [4]
42
43
44//! [5]
45qDebug("MyClass::setPrecision(): (%s) invalid precision %f",
46 qPrintable(objectName()), newPrecision);
47//! [5]
48
49
50//! [6]
51class MainWindow : public QMainWindow
52{
53public:
55
56protected:
57 bool eventFilter(QObject *obj, QEvent *ev) override;
58
59private:
60 QTextEdit *textEdit;
61};
62
64{
65 textEdit = new QTextEdit;
66 setCentralWidget(textEdit);
67
68 textEdit->installEventFilter(this);
69}
70
71bool MainWindow::eventFilter(QObject *obj, QEvent *event)
72{
73 if (obj == textEdit) {
74 if (event->type() == QEvent::KeyPress) {
75 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
76 qDebug() << "Ate key press" << keyEvent->key();
77 return true;
78 } else {
79 return false;
80 }
81 } else {
82 // pass the event on to the parent class
83 return QMainWindow::eventFilter(obj, event);
84 }
85}
86//! [6]
87
88
89//! [7]
90myObject->moveToThread(QApplication::instance()->thread());
91//! [7]
92
93
94//! [8]
95class MyObject : public QObject
96{
98
99public:
101
102protected:
103 void timerEvent(QTimerEvent *event) override;
104};
105
106MyObject::MyObject(QObject *parent)
107 : QObject(parent)
108{
109 using namespace std::chrono_literals;
110
111 startTimer(50ms);
112 startTimer(5s);
113 startTimer(10min);
114 startTimer(1h);
115}
116
117void MyObject::timerEvent(QTimerEvent *event)
118{
119 qDebug() << "Timer ID:" << event->id();
120}
121//! [8]
122
123//! [10]
124QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
125//! [10]
126
127
128//! [11]
129QListWidget *list = parentWidget->findChild<QListWidget *>();
130//! [11]
131
132
133//! [12]
134QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname");
135//! [12]
136
137
138//! [13]
139QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>();
140//! [13]
141
142
143//! [14]
144monitoredObj->installEventFilter(filterObj);
145//! [14]
146
147
148//! [15]
149class KeyPressEater : public QObject
150{
152 ...
153
154protected:
156};
157
158bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
159{
160 if (event->type() == QEvent::KeyPress) {
161 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
162 qDebug("Ate key press %d", keyEvent->key());
163 return true;
164 } else {
165 // standard event processing
166 return QObject::eventFilter(obj, event);
167 }
168}
169//! [15]
170
171
172//! [16]
173KeyPressEater *keyPressEater = new KeyPressEater(this);
174QPushButton *pushButton = new QPushButton(this);
175QListView *listView = new QListView(this);
176
177pushButton->installEventFilter(keyPressEater);
178listView->installEventFilter(keyPressEater);
179//! [16]
180
181
182//! [17]
183MyWindow::MyWindow()
184{
185 QLabel *senderLabel = new QLabel(tr("Name:"));
186 QLabel *recipientLabel = new QLabel(tr("Name:", "recipient"));
187//! [17]
188}
189
190
191//! [21]
192if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) {
193 QByteArray data;
194 get_the_value(&data); // expensive operation
195 emit valueChanged(data);
196}
197//! [21]
198
199
200//! [22]
201QLabel *label = new QLabel;
202QScrollBar *scrollBar = new QScrollBar;
203QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
204 label, SLOT(setNum(int)));
205//! [22]
206
207
208//! [23]
209// WRONG
210QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
211 label, SLOT(setNum(int value)));
212//! [23]
213
214
215//! [24]
216class MyWidget : public QWidget
217{
219
220public:
222
223signals:
225
226private:
227 QPushButton *myButton;
228};
229
230MyWidget::MyWidget()
231{
232 myButton = new QPushButton(this);
233 connect(myButton, SIGNAL(clicked()),
234 this, SIGNAL(buttonClicked()));
235}
236//! [24]
237
238
239//! [25]
240QObject::connect: Cannot queue arguments of type 'MyType'
241(Make sure 'MyType' is registered using qRegisterMetaType().)
242//! [25]
243
244
245//! [26]
246disconnect(myObject, nullptr, nullptr, nullptr);
247//! [26]
248
249
250//! [27]
251myObject->disconnect();
252//! [27]
253
254
255//! [28]
256disconnect(myObject, SIGNAL(mySignal()), nullptr, nullptr);
257//! [28]
258
259
260//! [29]
261myObject->disconnect(SIGNAL(mySignal()));
262//! [29]
263
264
265//! [30]
266disconnect(myObject, nullptr, myReceiver, nullptr);
267//! [30]
268
269
270//! [31]
271myObject->disconnect(myReceiver);
272//! [31]
273
274
275//! [32]
276if (signal == QMetaMethod::fromSignal(&MyObject::valueChanged)) {
277 // signal is valueChanged
278}
279//! [32]
280
281
282//! [33]
283void on_<object name>_<signal name>(<signal parameters>);
284//! [33]
285
286
287//! [34]
289//! [34]
290
291
292//! [35]
293class MyClass : public QObject
294{
295 Q_OBJECT
296 Q_CLASSINFO("Author", "Pierre Gendron")
297 Q_CLASSINFO("URL", "http://www.my-organization.qc.ca")
298
299public:
300 ...
301};
302//! [35]
303
304//! [37]
305Q_PROPERTY(QString title READ title WRITE setTitle USER true)
306//! [37]
307
308
309//! [38]
310class MyClass : public QObject
311{
312 Q_OBJECT
313
314public:
315 MyClass(QObject *parent = nullptr);
316 ~MyClass();
317
318 enum Priority { High, Low, VeryHigh, VeryLow };
319 Q_ENUM(Priority)
320 void setPriority(Priority priority);
321 Priority priority() const;
322};
323//! [38]
324
325
326//! [39]
327class QItemSelectionModel : public QObject
328{
329 Q_OBJECT
330
331public:
332 ...
333 enum SelectionFlag {
334 NoUpdate = 0x0000,
335 Clear = 0x0001,
336 Select = 0x0002,
337 Deselect = 0x0004,
338 Toggle = 0x0008,
339 Current = 0x0010,
340 Rows = 0x0020,
341 Columns = 0x0040,
342 SelectCurrent = Select | Current,
343 ToggleCurrent = Toggle | Current,
344 ClearAndSelect = Clear | Select
345 };
346
347 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
348 Q_FLAG(SelectionFlags)
349 ...
350}
351//! [39]
352
353
354//! [41]
355QPushButton *button = parentWidget->findChild<QPushButton *>("button1", Qt::FindDirectChildrenOnly);
356//! [41]
357
358
359//! [42]
360QListWidget *list = parentWidget->findChild<QListWidget *>(Qt::FindDirectChildrenOnly);
361//! [42]
362
363
364//! [43]
365QList<QPushButton *> childButtons = parentWidget.findChildren<QPushButton *>(Qt::FindDirectChildrenOnly);
366//! [43]
367
368//! [44]
369QLabel *label = new QLabel;
370QLineEdit *lineEdit = new QLineEdit;
371QObject::connect(lineEdit, &QLineEdit::textChanged,
372 label, &QLabel::setText);
373//! [44]
374
375//! [45]
376void someFunction();
377QPushButton *button = new QPushButton;
378QObject::connect(button, &QPushButton::clicked, someFunction);
379//! [45]
380
381//! [46]
383QTcpSocket *socket = new QTcpSocket;
384socket->connectToHost("qt-project.org", 80);
385QObject::connect(socket, &QTcpSocket::connected, [=] () {
386 socket->write("GET " + page + "\r\n");
387 });
388//! [46]
389
390//! [47]
391disconnect(myObject, &MyObject::mySignal(), nullptr, nullptr);
392//! [47]
393
394//! [48]
395QObject::disconnect(lineEdit, &QLineEdit::textChanged,
396 label, &QLabel::setText);
397//! [48]
398
399//! [49]
400static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
401if (isSignalConnected(valueChangedSignal)) {
402 QByteArray data;
403 data = get_the_value(); // expensive operation
404 emit valueChanged(data);
405}
406//! [49]
407
408//! [50]
409void someFunction();
410QPushButton *button = new QPushButton;
411QObject::connect(button, &QPushButton::clicked, this, someFunction, Qt::QueuedConnection);
412//! [50]
413
414//! [51]
415QByteArray page = ...;
416QTcpSocket *socket = new QTcpSocket;
417socket->connectToHost("qt-project.org", 80);
418QObject::connect(socket, &QTcpSocket::connected, this, [=] () {
419 socket->write("GET " + page + "\r\n");
420 }, Qt::AutoConnection);
421//! [51]
422
423//! [52]
424class MyClass : public QWidget
425{
426 Q_OBJECT
427
428public:
429 MyClass(QWidget *parent = nullptr);
430 ~MyClass();
431
432 bool event(QEvent* ev) override
433 {
434 if (ev->type() == QEvent::PolishRequest) {
435 // overwrite handling of PolishRequest if any
436 doThings();
437 return true;
438 } else if (ev->type() == QEvent::Show) {
439 // complement handling of Show if any
440 doThings2();
441 QWidget::event(ev);
442 return true;
443 }
444 // Make sure the rest of events are handled
445 return QWidget::event(ev);
446 }
447};
448//! [52]
449
450
451//! [53]
452{
453const QSignalBlocker blocker(someQObject);
454// no signals here
455}
456//! [53]
457
458//! [54]
459const bool wasBlocked = someQObject->blockSignals(true);
460// no signals here
461someQObject->blockSignals(wasBlocked);
462//! [54]
463
464{
465//! [invalid-timer-id]
466 QObject *obj;
467 ...
468 const auto id = Qt::TimerId{obj->startTimer(100ms)};
469 if (id != Qt::TimerId::Invalid)
470 // The timer has been started successfully
471//! [invalid-timer-id]
472}
bool eventFilter(QObject *obj, QEvent *ev) override
Filters events if this object has been installed as an event filter for the watched object.
void timerEvent(QTimerEvent *event) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
QList< int > list
[14]
qDebug("Items in list: %d", myList.size())
[23]
QLabel * label
[21]
const bool wasBlocked
[52]
static const QMetaMethod valueChangedSignal
[48]
QAbstractButton * button
[8]
void on_button1_clicked()
[33]
QObject * obj
[1]
QLineEdit * lineEdit
QListView * listView
disconnect(myObject, SIGNAL(mySignal()), nullptr, nullptr)
[27]
QList< QWidget * > widgets
[11]
QVBoxLayout * layout
QScrollBar * scrollBar
QByteArray page
[45]
QPushButton * pushButton
QTcpSocket * socket
[1]
KeyPressEater * keyPressEater
[15]
QList< QPushButton * > allPButtons
[12]
QTimer * timer
[3]
QList< QPushButton * > childButtons
[42]