QtCanpool 3.3.0
载入中...
搜索中...
未找到
qxobjectpool.cpp
浏览该文件的文档.
1
5#include "qxobjectpool.h"
6
8
9QxObjectPool::QxObjectPool(QObject *parent)
10 : QObject(parent)
11{
12}
13
15
16void QxObjectPool::addObject(QObject *object)
17{
18 if (!object)
19 return;
20 if (m_objects.contains(QPointer<QObject>(object)))
21 return;
22
23 m_objects.append(object);
24 emit objectAdded(object);
25}
26
27void QxObjectPool::removeObject(QObject *object)
28{
29 if (!object)
30 return;
31
32 const int index = m_objects.indexOf(QPointer<QObject>(object));
33 if (index < 0)
34 return;
35
36 emit aboutToRemoveObject(object);
37 m_objects.removeAt(index);
38}
39
40QList<QObject *> QxObjectPool::objects() const
41{
42 // Nulls are what a destroyed object leaves behind, and this is the one place
43 // that has to look past them; every other query comes through here.
44 QList<QObject *> live;
45 live.reserve(m_objects.count());
46 for (const QPointer<QObject> &entry : m_objects) {
47 if (entry)
48 live.append(entry.data());
49 }
50 return live;
51}
52
53QObject *QxObjectPool::objectByName(const QString &name) const
54{
55 if (name.isEmpty())
56 return Q_NULLPTR;
57
58 const QList<QObject *> live = objects();
59 for (QObject *object : live) {
60 if (object->objectName() == name)
61 return object;
62 }
63 return Q_NULLPTR;
64}
65
void aboutToRemoveObject(QObject *object)
Emitted just before removeObject() takes object out.
T * object() const
The first live published object castable to T, or null.
QList< QObject * > objects() const
Everything published and still alive, in the order it was published.
void objectAdded(QObject *object)
Emitted after object joined the pool.
void removeObject(QObject *object)
Takes object out.
QObject * objectByName(const QString &name) const
The first live published object whose objectName() is name, or null.
void addObject(QObject *object)
Publishes object under its objectName().
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
#define QX_PLUGIN_BEGIN_NAMESPACE
#define QX_PLUGIN_END_NAMESPACE
const char * name
Definition qxtheme.cpp:25