Qt5.15 QObject类官方文档翻译

QObject 类

The QObject class is the base class of all Qt objects. More…
QObject类是所有Qt对象的基类。更多…

Header: #include
qmake: QT += core
Inherited By: QAbstractAnimation, QAbstractEventDispatcher, QAbstractItemDelegate, QAbstractItemModel, QAbstractNetworkCache, QAbstractState, QAbstractTextDocumentLayout, QAbstractTransition, QAccessiblePlugin, QAction, QActionGroup, QAxFactory, QAxObject, QAxScript, QAxScriptManager, QButtonGroup, QClipboard, QCompleter, QCoreApplication, QDataWidgetMapper, QDBusAbstractAdaptor, QDBusAbstractInterface, QDBusPendingCallWatcher, QDBusServer, QDBusServiceWatcher, QDBusVirtualObject, QDesignerFormEditorInterface, QDesignerFormWindowManagerInterface, QDnsLookup, QDrag, QDtls, QDtlsClientVerifier, QEventLoop, QExtensionFactory, QExtensionManager, QFileSelector, QFileSystemWatcher, QFutureWatcher, QGenericPlugin, QGesture, QGraphicsAnchor, QGraphicsEffect, QGraphicsItemAnimation, QGraphicsObject, QGraphicsScene, QGraphicsTransform, QHttpMultiPart, QIconEnginePlugin, QImageIOPlugin, QInputMethod, QIODevice, QItemSelectionModel, QJSEngine, QLayout, QLibrary, QLocalServer, QMacToolBar, QMacToolBarItem, QMimeData, QMovie, QNetworkAccessManager, QNetworkConfigurationManager, QNetworkCookieJar, QNetworkSession, QObjectCleanupHandler, QOffscreenSurface, QOpenGLContext, QOpenGLContextGroup, QOpenGLDebugLogger, QOpenGLShader, QOpenGLShaderProgram, QOpenGLTimeMonitor, QOpenGLTimerQuery, QOpenGLVertexArrayObject, QPdfWriter, QPictureFormatPlugin, QPluginLoader, QQmlComponent, QQmlContext, QQmlEngineExtensionPlugin, QQmlExpression, QQmlFileSelector, QQmlPropertyMap, QQuickImageResponse, QQuickItem, QQuickItemGrabResult, QQuickRenderControl, QQuickTextDocument, QQuickTextureFactory, QScreen, QScroller, QSessionManager, QSettings, QSGAbstractRenderer, QSGEngine, QSGTexture, QSGTextureProvider, QSharedMemory, QShortcut, QSignalMapper, QSocketNotifier, QStyle, QStyleHints, QStylePlugin, QSyntaxHighlighter, QSystemTrayIcon, QTcpServer, QTextDocument, QTextObject, QThread, QThreadPool, QTimeLine, QTimer, QTranslator, QUndoGroup, QUndoStack, QValidator, QWidget, QWindow, QWinEventNotifier, QWinJumpList, QWinTaskbarButton, QWinTaskbarProgress, QWinThumbnailToolBar, and QWinThumbnailToolButton
  • List of all members, including inherited members
    所有成员的列表,包括继承的成员
  • Obsolete members 过时成员

Note: All functions in this class are reentrant.
注意:该类中的所有函数都是。

Note: These functions are also thread-safe:
注:这些功能还包括:

  • connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
    (const QObject *sender,const char *signal,const QObject *receiver,const char *method,Qt::ConnectionType type)
  • connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
    (const QObject *sender,const char *signal,const char *method,Qt::ConnectionType type)const
  • connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
    (const QObject *sender,PointerToMemberFunction signal,const QObject *receiver,PointerToMemberFunction method,Qt::ConnectionType type)
  • connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
    (const QObject *sender,PointerToString Function signal,Functor functor)
  • connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
    (const QObject *sender,PointerToMemberFunction signal,const QObject *context,Functor functor,Qt::ConnectionType type)
  • disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
    (const QObject *sender,const char *signal,const QObject *receiver,const char *method)
  • disconnect(const char *signal, const QObject *receiver, const char *method) const
    (const char *signal,const QObject *receiver,const char *method)const
  • disconnect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
    (const QObject *sender,PointerToMemberFunction signal,const QObject *receiver,PointerToMemberFunction method)
  • deleteLater()

Properties 性能

  • objectName : QString

Public Functions 公共职能

QObject(QObject *parent = nullptr)
virtual ~QObject()
bool blockSignals(bool block)
const QObjectList & children() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const
bool disconnect(const char *signal = nullptr, const QObject *receiver = nullptr, const char *method = nullptr) const
bool disconnect(const QObject *receiver, const char *method = nullptr) const
void dumpObjectInfo() const
void dumpObjectTree() const
QList dynamicPropertyNames() const
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
T findChild(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
QList findChildren(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
QList findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
bool inherits(const char *className) const
void installEventFilter(QObject *filterObj)
bool isWidgetType() const
bool isWindowType() const
void killTimer(int id)
virtual const QMetaObject * metaObject() const
void moveToThread(QThread *targetThread)
QString objectName() const
QObject * parent() const
QVariant property(const char *name) const
void removeEventFilter(QObject *obj)
void setObjectName(const QString &name)
void setParent(QObject *parent)
bool setProperty(const char *name, const QVariant &value)
bool signalsBlocked() const
int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)
int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)
QThread * thread() const

Public Slots 公共插槽

void deleteLater()

Signals 信号

void destroyed(QObject *obj = nullptr)
void objectNameChanged(const QString &objectName)

Static Public Members 静态公共成员

QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool disconnect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
bool disconnect(const QMetaObject::Connection &connection)
bool disconnect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
const QMetaObject staticMetaObject
QString tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1)

Protected Functions 保护功能

virtual void childEvent(QChildEvent *event)
virtual void connectNotify(const QMetaMethod &signal)
virtual void customEvent(QEvent *event)
virtual void disconnectNotify(const QMetaMethod &signal)
bool isSignalConnected(const QMetaMethod &signal) const
int receivers(const char *signal) const
QObject * sender() const
int senderSignalIndex() const
virtual void timerEvent(QTimerEvent *event)

Related Non-Members 相关非成员

typedef QObjectList
QList qFindChildren(const QObject *obj, const QRegExp &regExp)
T qobject_cast(QObject *object)
T qobject_cast(const QObject *object)

Macros 宏

QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
Q_CLASSINFO(Name, Value)
Q_DISABLE_COPY(Class)
Q_DISABLE_COPY_MOVE(Class)
Q_DISABLE_MOVE(Class)
Q_EMIT
Q_ENUM()
Q_ENUM_NS()
Q_FLAG()
Q_FLAG_NS()
Q_GADGET
Q_INTERFACES()
Q_INVOKABLE
Q_NAMESPACE
Q_NAMESPACE_EXPORT(EXPORT_MACRO)
Q_OBJECT
Q_PROPERTY()
Q_REVISION
Q_SET_OBJECT_NAME(Object)
Q_SIGNAL
Q_SIGNALS
Q_SLOT
Q_SLOTS

Detailed Description 详细描述

QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect(). To avoid never ending notification loops you can temporarily block signals with blockSignals(). The protected functions connectNotify() and disconnectNotify() make it possible to track connections.
QObject是Qt的核心。这个模型的核心特性是一个非常强大的无缝对象通信机制。您可以使用()将信号连接到插槽,并使用()断开连接。为了避免无休止的通知循环,您可以使用()暂时阻止信号。受保护的函数()和()可以跟踪连接。

QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent’s children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().
QObject在中组织自己。当你创建一个以另一个对象作为父对象的QObject时,该对象会自动将自己添加到父对象的()列表中。父级取得对象的所有权;也就是说,它将在析构函数中自动删除其子对象。可以按名称查找对象,也可以使用()或()键入。

Every object has an objectName() and its class name can be found via the corresponding metaObject() (see QMetaObject::className()). You can determine whether the object’s class inherits another class in the QObject inheritance hierarchy by using the inherits() function.
每个对象都有一个(),它的类名可以通过对应的()找到(参见())。可以使用()函数确定对象的类是否继承QObject继承层次结构中的另一个类。

When an object is deleted, it emits a destroyed() signal. You can catch this signal to avoid dangling references to QObjects.
当一个对象被删除时,它发出一个()信号。您可以捕获此信号以避免对QObject的悬空引用。

QObjects can receive events through event() and filter the events of other objects. See installEventFilter() and eventFilter() for details. A convenience handler, childEvent(), can be reimplemented to catch child events.
QObject可以通过()接收事件,并过滤其他对象的事件。详见()和()。可以重新实现一个方便的处理程序()来捕获子事件。

Last but not least, QObject provides the basic timer support in Qt; see QTimer for high-level support for timers.
最后但并非最不重要的是,QObject在Qt中提供了基本的计时器支持;有关定时器的高级支持,请参见。

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
请注意,对于实现信号、插槽或属性的任何对象,该宏都是必需的。还需要对源文件运行。我们强烈建议在QObject的所有子类中使用这个宏,不管它们是否实际上使用信号、插槽和属性,因为如果不这样做,可能会导致某些函数表现出奇怪的行为。

All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than qobject_cast(obj) or obj->inherits(“QWidget”).
所有Qt小部件都继承QObject。方便函数()返回一个对象是否是一个小部件。它比< *>(obj)或obj->(““)快得多。

Some QObject functions, e.g. children(), return a QObjectList. QObjectList is a typedef for QList.
一些QObject函数,例如(),返回a。是的typedef。

Thread Affinity 线程关联

A QObject instance is said to have a thread affinity, or that it lives in a certain thread. When a QObject receives a queued signal or a posted event, the slot or event handler will run in the thread that the object lives in.
QObject实例被称为具有线程关联性,或者它位于某个线程中。当QObject接收到或时,插槽或事件处理程序将在对象所在的线程中运行。

**Note: **If a QObject has no thread affinity (that is, if thread() returns zero), or if it lives in a thread that has no running event loop, then it cannot receive queued signals or posted events.
注意:如果QObject没有线程关联(也就是说,if()返回零),或者如果它存在于没有运行事件循环的线程中,那么它不能接收排队的信号或发布的事件。

By default, a QObject lives in the thread in which it is created. An object’s thread affinity can be queried using thread() and changed using moveToThread().
默认情况下,QObject位于创建它的线程中。可以使用()查询对象的线程关联,也可以使用()更改对象的线程关联。

All QObjects must live in the same thread as their parent. Consequently:
所有QObject都必须与其父对象位于同一线程中。因此:

  • setParent() will fail if the two QObjects involved live in different threads.
    如果两个QObject位于不同的线程中,则()将失败。
  • When a QObject is moved to another thread, all its children will be automatically moved too.
    当一个QObject被移动到另一个线程时,它的所有子线程也会被自动移动。
  • moveToThread() will fail if the QObject has a parent.
    如果QObject有父对象,则()将失败。
  • If QObjects are created within QThread::run(), they cannot become children of the QThread object because the QThread does not live in the thread that calls QThread::run().
    如果QObject是在()中创建的,它们不能成为对象的子对象,因为不存在于调用()的线程中。

**Note: **A QObject’s member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child’s constructor, or by calling setParent(). Without this step, the object’s member variables will remain in the old thread when moveToThread() is called.
注意:QObject的成员变量不会自动成为其子级。父-子关系必须通过传递指向子对象的指针或调用()来设置。如果没有这一步,当调用()时,对象的成员变量将保留在旧线程中。

No Copy Constructor or Assignment Operator 无复制构造函数或赋值运算符

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.
QObject既没有复制构造函数,也没有赋值运算符。这是设计好的。实际上,它们是被声明的,但是是在一个带有宏()的 private 节中。事实上,所有从QObject派生的Qt类(直接或间接)都使用这个宏来声明它们的复制构造函数和赋值操作符是私有的。在Qt页面上的讨论中可以找到原因。

The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can’t use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.
主要的结果是,您应该使用指向QObject(或指向QObject子类)的指针,否则您可能会将QObject子类用作值。例如,如果没有复制构造函数,就不能使用QObject的子类作为存储在容器类中的值。必须存储指针。

Auto-Connection 自动连接

Qt’s meta-object system provides a mechanism to automatically connect signals and slots between QObject subclasses and their children. As long as objects are defined with suitable object names, and slots follow a simple naming convention, this connection can be performed at run-time by the QMetaObject::connectSlotsByName() function.
Qt的元对象系统提供了一种机制来自动连接QObject子类和其子类之间的信号和插槽。只要用合适的对象名定义对象,并且插槽遵循简单的命名约定,就可以在运行时通过()函数执行此连接。

uic generates code that invokes this function to enable auto-connection to be performed between widgets on forms created with Qt Designer. More information about using auto-connection with Qt Designer is given in the Using a Designer UI File in Your C++ Application section of the Qt Designer manual.
生成调用此函数的代码,以便在使用Qt Designer创建的表单上的小部件之间执行自动连接。有关使用Qt Designer自动连接的更多信息,请参阅Qt Designer手册的部分。

Dynamic Properties 动力特性

From Qt 4.2, dynamic properties can be added to and removed from QObject instances at run-time. Dynamic properties do not need to be declared at compile-time, yet they provide the same advantages as static properties and are manipulated using the same API - using property() to read them and setProperty() to write them.
从Qt 4.2开始,动态属性可以在运行时添加和删除QObject实例。动态属性不需要在编译时声明,但它们提供了与静态属性相同的优点,并且使用相同的API进行操作-使用()读取它们并使用()写入它们。

From Qt 4.3, dynamic properties are supported by Qt Designer, and both standard Qt widgets and user-created forms can be given dynamic properties.
从Qt 4.3开始,支持动态属性,标准Qt小部件和用户创建的表单都可以被赋予动态属性。

Internationalization (I18n) 国际化(I18 n)

All QObject subclasses support Qt’s translation features, making it possible to translate an application’s user interface into different languages.
所有QObject子类都支持Qt的翻译功能,使得将应用程序的用户界面翻译成不同的语言成为可能。

To make user-visible text translatable, it must be wrapped in calls to the tr() function. This is explained in detail in the Writing Source Code for Translation document.
要使用户可见的文本可翻译,必须将其包装在对()函数的调用中。这在文件中有详细解释。

**See also **QMetaObject, QPointer, QObjectCleanupHandler, Q_DISABLE_COPY(), and Object Trees & Ownership.
另请参见、、()和。

Property Documentation 物业文件

objectName : QString

This property holds the name of this object
此属性保存此对象的名称

You can find an object by name (and type) using findChild(). You can find a set of objects with findChildren().
可以使用()按名称(和类型)查找对象。可以使用()查找一组对象。

qDebug("MyClass::setPrecision(): (%s) invalid precision %f",
       qPrintable(objectName()), newPrecision);

By default, this property contains an empty string.
默认情况下,此属性包含空字符串。

Access functions: 访问功能:

QString objectName() const
void setObjectName(const QString &name)

Notifier signal: 通知器信号:

void objectNameChanged(const QString &objectName) [see note below]

**Note: **This is a private signal. It can be used in signal connections but cannot be emitted by the user.
注意:这是一个私人信号。它可以用于信号连接,但不能由用户发射。

**See also **metaObject() and QMetaObject::className().
参见()和()。

Member Function Documentation
成员职能文档

QObject::QObject(QObject *parent = nullptr)

Constructs an object with parent object parent.
用父对象构造一个对象。

The parent of an object may be viewed as the object’s owner. For instance, a dialog box is the parent of the OK and Cancel buttons it contains.
对象的父对象可以被视为对象的所有者。例如,a是它包含的和按钮的父级。

The destructor of a parent object destroys all child objects.
父对象的析构函数将销毁所有子对象。

Setting parent to nullptr constructs an object with no parent. If the object is a widget, it will become a top-level window.
设置为 nullptr 将构造没有父对象的对象。如果对象是一个小部件,它将成为一个顶级窗口。

**Note: **This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
注意:此函数可以通过元对象系统和QML调用。明白了

**See also **parent(), findChild(), and findChildren().
参见()、()和()。

[slot]void QObject::deleteLater()

Schedules this object for deletion.
计划删除此对象。

The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started. If deleteLater() is called after the main event loop has stopped, the object will not be deleted. Since Qt 4.8, if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes.
当控制返回到事件循环时,对象将被删除。如果调用此函数时事件循环未运行(例如deleteLater()在()之前对对象调用),一旦事件循环开始,对象将被删除。如果在主事件循环停止后调用deleteLater(),则不会删除对象。从Qt 4.8开始,如果deleteLater()在一个没有运行事件循环的线程中的对象上被调用,该对象将在线程结束时被销毁。

Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called. This does not apply to objects deleted while a previous, nested event loop was still running: the Qt event loop will delete those objects as soon as the new nested event loop starts.
注意,进入和离开新的事件循环(例如,通过打开模态对话框)将不执行延迟删除;对于要删除的对象,控件必须返回到调用deleteLater()的事件循环。这不适用于在上一个嵌套事件循环仍在运行时删除的对象:Qt事件循环将在新的嵌套事件循环开始时删除这些对象。

**Note: **It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue.
注意:多次调用此函数是安全的;当递送第一延迟删除事件时,从事件队列中移除对象的任何未决事件。

Note: This function is thread-safe.
注:此功能为。

**See also **destroyed() and QPointer.
参见()和。

[signal]void QObject::destroyed(QObject *obj = nullptr)

This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
此信号在销毁对象之前立即发出,在通知的任何实例之后发出,并且无法阻止。

All the objects’s children are destroyed immediately after this signal is emitted.
发出此信号后,对象的所有子对象将立即销毁。

**See also **deleteLater() and QPointer.
参见()和。

[signal]void QObject::objectNameChanged(const QString &objectName)

This signal is emitted after the object’s name has been changed. The new object name is passed as objectName.
此信号在对象名称更改后发出。新的对象名被传递为。

**Note: **This is a private signal. It can be used in signal connections but cannot be emitted by the user.
注意:这是一个私人信号。它可以用于信号连接,但不能由用户发射。

**Note: **Notifier signal for property objectName.
注意:属性的通告程序信号。

**See also **QObject::objectName. 另见。

[virtual]QObject::~QObject()

Destroys the object, deleting all its child objects.
销毁对象,删除其所有子对象。

All signals to and from the object are automatically disconnected, and any pending posted events for the object are removed from the event queue. However, it is often safer to use deleteLater() rather than deleting a QObject subclass directly.
所有发往和来自该对象的信号都将自动断开,并且该对象的任何挂起的已发布事件都将从事件队列中删除。但是,使用()通常比直接删除子类更安全。

**Warning: **All child objects are deleted. If any of these objects are on the stack or global, sooner or later your program will crash. We do not recommend holding pointers to child objects from outside the parent. If you still do, the destroyed() signal gives you an opportunity to detect when an object is destroyed.
警告:所有子对象都将被删除。如果这些对象中的任何一个在堆栈上或全局中,你的程序迟早会崩溃。我们不建议从父对象外部保存指向子对象的指针。如果你仍然这样做,()信号给你一个机会来检测一个对象何时被破坏。

**Warning: **Deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete the QObject directly if it exists in a different thread than the one currently executing. Use deleteLater() instead, which will cause the event loop to delete the object after all pending events have been delivered to it.
警告:在挂起事件等待传递时删除可能会导致崩溃。如果存在于与当前执行的线程不同的线程中,则不能直接删除。使用()代替,这将导致事件循环在所有挂起事件都传递给对象后删除该对象。

**See also **deleteLater(). 参见()。

bool QObject::blockSignals(bool block)

If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). If block is false, no such blocking will occur.
如果为真,则阻止此对象发出的信号(即,发射信号将不会调用与之连接的任何东西)。如果为false,则不会发生此类阻塞。

The return value is the previous value of signalsBlocked().
返回值是()的前一个值。

Note that the destroyed() signal will be emitted even if the signals for this object have been blocked.
请注意,即使此对象的信号已被阻止,也会发出()信号。

Signals emitted while being blocked are not buffered.
在被阻塞时发出的信号不被缓冲。

**See also **signalsBlocked() and QSignalBlocker.
参见()和。

[virtual protected]void QObject::childEvent(QChildEvent *event)

This event handler can be reimplemented in a subclass to receive child events. The event is passed in the event parameter.
此事件处理程序可以在子类中重新实现以接收子事件。事件在参数中传递。

QEvent::ChildAdded and QEvent::ChildRemoved events are sent to objects when children are added or removed. In both cases you can only rely on the child being a QObject, or if isWidgetType() returns true, a QWidget. (This is because, in the ChildAdded case, the child is not yet fully constructed, and in the ChildRemoved case it might have been destructed already).
并且当添加或移除子对象时,事件被发送到对象。在这两种情况下,你只能依赖于子元素是a,或者if()返回 true ,a。(This因为,在这种情况下,孩子还没有完全构造,在这种情况下,它可能已经被破坏了)。

QEvent::ChildPolished events are sent to widgets when children are polished, or when polished children are added. If you receive a child polished event, the child’s construction is usually completed. However, this is not guaranteed, and multiple polish events may be delivered during the execution of a widget’s constructor.
当子部件被抛光时,或者当添加抛光的子部件时,事件被发送到窗口小部件。如果你收到一个孩子抛光事件,孩子的建设通常是完成。但是,这并不能保证,在执行小部件的构造函数期间可能会传递多个polish事件。

For every child widget, you receive one ChildAdded event, zero or more ChildPolished events, and one ChildRemoved event.
对于每个子小部件,您将收到一个事件、零个或多个事件以及一个事件。

The ChildPolished event is omitted if a child is removed immediately after it is added. If a child is polished several times during construction and destruction, you may receive several child polished events for the same child, each time with a different virtual table.
如果在添加子项后立即移除子项,则会省略该事件。如果在构建和销毁期间多次抛光子级,则您可能会收到同一子级的多个子级抛光事件,每次使用不同的虚拟表。

**See also **event(). 参见()。

const QObjectList &QObject::children() const

Returns a list of child objects. The QObjectList class is defined in the header file as the following:
返回子对象的列表。类在 头文件中定义如下:

typedef QList QObjectList;

The first child added is the first object in the list and the last child added is the last object in the list, i.e. new children are appended at the end.
第一个添加的子对象是列表中的对象,最后一个添加的子对象是列表中的对象,即新的子节点被附加在末尾。

Note that the list order changes when QWidget children are raised or lowered. A widget that is raised becomes the last object in the list, and a widget that is lowered becomes the first object in the list.
请注意,当子级为或时,列表顺序会更改。升起的小部件将成为列表中的最后一个对象,降低的小部件将成为列表中的第一个对象。

**See also **findChild(), findChildren(), parent(), and setParent().
参见()、()、()和()。

[static]QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns a handle to the connection that can be used to disconnect it later.
创建给定的从对象中的到对象中的的连接。返回连接的句柄,可用于以后断开连接。

You must use the SIGNAL() and SLOT() macros when specifying the signal and the method, for example:
指定和时,必须使用 SIGNAL()SLOT() 宏,例如:

QLabel *label = new QLabel;
QScrollBar *scrollBar = new QScrollBar;
QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
                 label,  SLOT(setNum(int)));

This example ensures that the label always displays the current scroll bar value. Note that the signal and slots parameters must not contain any variable names, only the type. E.g. the following would not work and return false:
此示例确保标签始终显示当前滚动条值。请注意,信号和插槽参数不能包含任何变量名,只能包含类型。例如,下面的代码将不起作用,并返回false:

// WRONG
QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
                 label, SLOT(setNum(int value)));

A signal can also be connected to another signal:
一个信号也可以连接到另一个信号:

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget();

signals:
    void buttonClicked();

private:
    QPushButton *myButton;
};

MyWidget::MyWidget()
{
    myButton = new QPushButton(this);
    connect(myButton, SIGNAL(clicked()),
            this, SIGNAL(buttonClicked()));
}

In this example, the MyWidget constructor relays a signal from a private member variable, and makes it available under a name that relates to MyWidget.
在这个例子中, MyWidget 构造函数中继了一个来自私有成员变量的信号,并使其在与 MyWidget 相关的名称下可用。

A signal can be connected to many slots and signals. Many signals can be connected to one slot.
一个信号可以连接到多个插槽和信号。多个信号可以连接到一个插槽。

If a signal is connected to several slots, the slots are activated in the same order in which the connections were made, when the signal is emitted.
如果一个信号连接到几个插槽,则在发射信号时,插槽以与进行连接的顺序相同的顺序被激活。

The function returns a QMetaObject::Connection that represents a handle to a connection if it successfully connects the signal to the slot. The connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren’t compatible. You can check if the handle is valid by casting it to a bool.
如果函数成功地将信号连接到插槽,则返回表示连接句柄的。如果连接句柄无法创建连接,例如,如果无法验证或的存在,或者它们的签名不兼容,则连接句柄将无效。您可以通过将句柄转换为bool来检查句柄是否有效。

By default, a signal is emitted for every connection you make; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return an invalid QMetaObject::Connection.
默认情况下,为您建立的每个连接发出一个信号;针对复制连接发射两个信号。您可以通过一个()调用来断开所有这些连接。如果传递,则只有在连接不是重复的情况下才会建立连接。如果已经有一个重复的(完全相同的信号到相同对象上的完全相同的插槽),连接将失败,连接将返回无效。

**Note: **Qt::UniqueConnections do not work for lambdas, non-member functions and functors; they only apply to connecting to member functions.
注意:Qt::UniqueConnections不适用于lambda、非成员函数和仿函数;它们仅适用于连接到成员函数。

The optional type parameter describes the type of connection to establish. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time. If the signal is queued, the parameters must be of types that are known to Qt’s meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message
可选参数描述要建立的连接类型。具体地,它确定特定信号是立即递送到时隙还是排队以便在稍后的时间递送。如果信号是排队的,参数的类型必须是Qt的元对象系统已知的,因为Qt需要复制参数以将它们存储在幕后的事件中。如果尝试使用排队连接并得到错误消息

QObject::connect: Cannot queue arguments of type 'MyType'
(Make sure 'MyType' is registered using qRegisterMetaType().)

call qRegisterMetaType() to register the data type before you establish the connection.
在建立连接之前,调用()注册数据类型。

Note: This function is thread-safe.
注:此功能为。

**See also **disconnect(), sender(), qRegisterMetaType(), Q_DECLARE_METATYPE(), and Differences between String-Based and Functor-Based Connections.
参见()、()和。

[static]QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection)

Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns a handle to the connection that can be used to disconnect it later.
创建给定的从对象中的到对象中的的连接。返回连接的句柄,可用于以后断开连接。

The Connection handle will be invalid if it cannot create the connection, for example, the parameters were invalid. You can check if the QMetaObject::Connection is valid by casting it to a bool.
如果连接句柄无法创建连接(例如,参数无效),则连接句柄将无效。你可以通过将其转换为bool来检查是否有效。

This function works in the same way as connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) but it uses QMetaMethod to specify signal and method.
此函数的工作方式与 connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) 相同,但它用于指定信号和方法。

This function was introduced in Qt 4.8.
这个函数是在Qt 4.8中引入的。

**See also **connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type).
另见(const QObject *sender,const char *signal,const QObject *receiver,const char *method,Qt::ConnectionType type)。

QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const

This function overloads connect().
此函数重载connect()。

Connects signal from the sender object to this object’s method.
从对象连接到此对象的。

Equivalent to connect(sender, signal, this, method, type).
相当于connect(,, this ,,)。

Every connection you make emits a signal, so duplicate connections emit two signals. You can break a connection using disconnect().
您建立的每个连接都会发出一个信号,因此重复的连接会发出两个信号。可以使用()断开连接。

Note: This function is thread-safe.
注:此功能为。

**See also **disconnect(). 参见()。

[static]template QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)

This function overloads connect().
此函数重载connect()。

Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns a handle to the connection that can be used to disconnect it later.
创建给定的从对象中的到对象中的的连接。返回连接的句柄,可用于以后断开连接。

The signal must be a function declared as a signal in the header. The slot function can be any member function that can be connected to the signal. A slot can be connected to a given signal if the signal has at least as many arguments as the slot, and there is an implicit conversion between the types of the corresponding arguments in the signal and the slot.
信号必须是在标头中声明为信号的函数。slot函数可以是任何可以连接到信号的成员函数。如果信号的参数至少与插槽一样多,并且信号和插槽中相应参数的类型之间存在隐式转换,则插槽可以连接到给定信号。

Example: 范例:

QLabel *label = new QLabel;
QLineEdit *lineEdit = new QLineEdit;
QObject::connect(lineEdit, &QLineEdit::textChanged,
                 label,  &QLabel::setText);

This example ensures that the label always displays the current line edit text.
此示例确保标签始终显示当前行编辑文本。

A signal can be connected to many slots and signals. Many signals can be connected to one slot.
一个信号可以连接到多个插槽和信号。多个信号可以连接到一个插槽。

If a signal is connected to several slots, the slots are activated in the same order as the order the connection was made, when the signal is emitted
如果一个信号连接到多个插槽,则在发出信号时,这些插槽的激活顺序与连接顺序相同

The function returns an handle to a connection if it successfully connects the signal to the slot. The Connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of signal (if it was not declared as a signal) You can check if the QMetaObject::Connection is valid by casting it to a bool.
如果函数成功地将信号连接到插槽,则返回连接的句柄。如果连接句柄不能创建连接,则连接句柄将无效,例如,如果无法验证是否存在(如果未将其声明为信号)您可以通过将其转换为bool来检查是否有效。

By default, a signal is emitted for every connection you make; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return an invalid QMetaObject::Connection.
默认情况下,为您建立的每个连接发出一个信号;针对复制连接发射两个信号。您可以通过一个()调用来断开所有这些连接。如果传递,则只有在连接不是重复的情况下才会建立连接。如果已经有一个重复的(完全相同的信号到相同对象上的完全相同的插槽),连接将失败,连接将返回无效。

The optional type parameter describes the type of connection to establish. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time. If the signal is queued, the parameters must be of types that are known to Qt’s meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message
可选参数描述要建立的连接类型。具体地,它确定特定信号是立即递送到时隙还是排队以便在稍后的时间递送。如果信号是排队的,参数的类型必须是Qt的元对象系统已知的,因为Qt需要复制参数以将它们存储在幕后的事件中。如果尝试使用排队连接并得到错误消息

QObject::connect: Cannot queue arguments of type 'MyType'
(Make sure 'MyType' is registered using qRegisterMetaType().)

make sure to declare the argument type with
确保声明参数类型Q_DECLARE_METATYPE

Overloaded functions can be resolved with help of qOverload.
重载的函数可以在的帮助下解决。

Note: This function is thread-safe.
注:此功能为。

**See also **Differences between String-Based and Functor-Based Connections. 另见。

[static]template QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

This function overloads connect().
此函数重载connect()。

Creates a connection from signal in sender object to functor, and returns a handle to the connection
创建从in对象到的连接,并返回该连接的句柄

The signal must be a function declared as a signal in the header. The slot function can be any function or functor that can be connected to the signal. A function can be connected to a given signal if the signal has at least as many argument as the slot. A functor can be connected to a signal if they have exactly the same number of arguments. There must exist implicit conversion between the types of the corresponding arguments in the signal and the slot.
信号必须是在标头中声明为信号的函数。插槽函数可以是任何可以连接到信号的函数或函子。一个函数可以连接到一个给定的信号,如果信号至少有一样多的参数槽。一个函子可以连接到一个信号,如果他们有完全相同数量的参数。信号和槽中相应参数的类型之间必须存在隐式转换。

Example: 范例:

void someFunction();
QPushButton *button = new QPushButton;
QObject::connect(button, &QPushButton::clicked, someFunction);

Lambda expressions can also be used:
也可以使用Lambda表达式:

QByteArray page = ...;
QTcpSocket *socket = new QTcpSocket;
socket->connectToHost("qt-project.org", 80);
QObject::connect(socket, &QTcpSocket::connected, [=] () {
        socket->write("GET " + page + "\r\n");
    });

The connection will automatically disconnect if the sender is destroyed. However, you should take care that any objects used within the functor are still alive when the signal is emitted.
如果发送方被销毁,连接将自动断开。但是,您应该注意,当发出信号时,functor中使用的任何对象仍然是活动的。

Overloaded functions can be resolved with help of qOverload.
重载的函数可以在的帮助下解决。

Note: This function is thread-safe.
注:此功能为。

[static]template QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)

This function overloads connect().
此函数重载connect()。

Creates a connection of a given type from signal in sender object to functor to be placed in a specific event loop of context, and returns a handle to the connection.
创建要放置在的特定事件循环中的给定from in对象的连接,并返回该连接的句柄。

**Note: **Qt::UniqueConnections do not work for lambdas, non-member functions and functors; they only apply to connecting to member functions.
注意:Qt::UniqueConnections不适用于lambda、非成员函数和仿函数;它们仅适用于连接到成员函数。

The signal must be a function declared as a signal in the header. The slot function can be any function or functor that can be connected to the signal. A function can be connected to a given signal if the signal has at least as many argument as the slot. A functor can be connected to a signal if they have exactly the same number of arguments. There must exist implicit conversion between the types of the corresponding arguments in the signal and the slot.
信号必须是在标头中声明为信号的函数。插槽函数可以是任何可以连接到信号的函数或函子。一个函数可以连接到一个给定的信号,如果信号至少有一样多的参数槽。一个函子可以连接到一个信号,如果他们有完全相同数量的参数。信号和槽中相应参数的类型之间必须存在隐式转换。

Example: 范例:

void someFunction();
QPushButton *button = new QPushButton;
QObject::connect(button, &QPushButton::clicked, this, someFunction, Qt::QueuedConnection);

Lambda expressions can also be used:
也可以使用Lambda表达式:

QByteArray page = ...;
QTcpSocket *socket = new QTcpSocket;
socket->connectToHost("qt-project.org", 80);
QObject::connect(socket, &QTcpSocket::connected, this, [=] () {
        socket->write("GET " + page + "\r\n");
    }, Qt::AutoConnection);

The connection will automatically disconnect if the sender or the context is destroyed. However, you should take care that any objects used within the functor are still alive when the signal is emitted.
如果发送方或上下文被破坏,连接将自动断开。但是,您应该注意,当发出信号时,functor中使用的任何对象仍然是活动的。

Overloaded functions can be resolved with help of qOverload.
重载的函数可以在的帮助下解决。

Note: This function is thread-safe.
注:此功能为。

This function was introduced in Qt 5.2.
此功能在Qt 5.2中引入。

[virtual protected]void QObject::connectNotify(const QMetaMethod &signal)

This virtual function is called when something has been connected to signal in this object.
这个虚函数是在这个对象中连接到某个东西时调用的。

If you want to compare signal with a specific signal, you can use QMetaMethod::fromSignal() as follows:
如果要与特定信号进行比较,可以使用(),如下所示:

if (signal == QMetaMethod::fromSignal(&MyObject::valueChanged)) {
    // signal is valueChanged
}

**Warning: **This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.
警告:此函数违反了面向对象的模块化原则。但是,当您需要执行昂贵的初始化时,只有当某些东西连接到信号时,它才可能有用。

**Warning: **This function is called from the thread which performs the connection, which may be a different thread from the thread in which this object lives.
警告:此函数是从执行连接的线程调用的,该线程可能与此对象所在的线程不同。

This function was introduced in Qt 5.0.
该功能在Qt 5.0中引入。

**See also **connect() and disconnectNotify().
参见()和()。

[virtual protected]void QObject::customEvent(QEvent *event)

This event handler can be reimplemented in a subclass to receive custom events. Custom events are user-defined events with a type value at least as large as the QEvent::User item of the QEvent::Type enum, and is typically a QEvent subclass. The event is passed in the event parameter.
此事件处理程序可以在子类中重新实现以接收自定义事件。自定义事件是用户定义的事件,其类型值至少与枚举的项一样大,并且通常是子类。事件在参数中传递。

**See also **event() and QEvent.
参见()和。

[static]bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)

Disconnects signal in object sender from method in object receiver. Returns true if the connection is successfully broken; otherwise returns false.
从对象中断开对象中的连接。如果连接成功断开,则返回 true ;否则返回 false

A signal-slot connection is removed when either of the objects involved are destroyed.
当涉及的任一对象被销毁时,信号插槽连接将被删除。

disconnect() is typically used in three ways, as the following examples demonstrate.
disconnect()通常以三种方式使用,如以下示例所示。

  1. Disconnect everything connected to an object’s signals:
    断开连接到对象信号的所有设备:

    disconnect(myObject, nullptr, nullptr, nullptr);
    

    equivalent to the non-static overloaded function
    等价于非静态重载函数

    myObject->disconnect();
    
  2. Disconnect everything connected to a specific signal:
    断开连接到特定信号的所有设备:

    disconnect(myObject, SIGNAL(mySignal()), nullptr, nullptr);
    

    equivalent to the non-static overloaded function
    等价于非静态重载函数

    myObject->disconnect(SIGNAL(mySignal()));
    
  3. Disconnect a specific receiver:
    断开特定接收器:

    disconnect(myObject, nullptr, myReceiver, nullptr);
    

    equivalent to the non-static overloaded function
    等价于非静态重载函数

    myObject->disconnect(myReceiver);
    

nullptr may be used as a wildcard, meaning “any signal”, “any receiving object”, or “any slot in the receiving object”, respectively.
nullptr 可以用作通配符,分别表示“任何信号”、“任何接收对象”或“接收对象中的任何槽”。

The sender may never be nullptr. (You cannot disconnect signals from more than one object in a single call.)
可能永远不会 nullptr 。(You不能在单个调用中断开来自多个对象的信号。)

If signal is nullptr, it disconnects receiver and method from any signal. If not, only the specified signal is disconnected.
如果是 nullptr ,则断开和与任何信号的连接。否则,仅断开指定信号。

If receiver is nullptr, it disconnects anything connected to signal. If not, slots in objects other than receiver are not disconnected.
如果是 nullptr ,则断开连接到的任何内容。如果不是,则不会断开对象以外的插槽。

If method is nullptr, it disconnects anything that is connected to receiver. If not, only slots named method will be disconnected, and all other slots are left alone. The method must be nullptr if receiver is left out, so you cannot disconnect a specifically-named slot on all objects.
如果是 nullptr ,则断开连接到的任何内容。否则,将仅断开名为的插槽,而所有其他插槽将保持原样。如果省略,则必须是 nullptr ,这样您就无法断开所有对象上指定名称的插槽。

Note: This function is thread-safe.
注:此功能为。

**See also **connect(). 参见()。

[static]bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)

Disconnects signal in object sender from method in object receiver. Returns true if the connection is successfully broken; otherwise returns false.
从对象中断开对象中的连接。如果连接成功断开,则返回 true ;否则返回 false

This function provides the same possibilities like disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method) but uses QMetaMethod to represent the signal and the method to be disconnected.
此函数提供与 disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method) 相同的可能性,但用于表示要断开的信号和方法。

Additionally this function returns false and no signals and slots disconnected if:
此外,如果出现以下情况,此函数将返回false,并且没有信号和插槽断开连接:

  1. signal is not a member of sender class or one of its parent classes.
    不是sender类或其父类的成员。
  2. method is not a member of receiver class or one of its parent classes.
    不是接收器类或其父类的成员。
  3. signal instance represents not a signal.
    实例表示不是信号。

QMetaMethod() may be used as wildcard in the meaning “any signal” or “any slot in receiving object”. In the same way nullptr can be used for receiver in the meaning “any receiving object”. In this case method should also be QMetaMethod(). sender parameter should be never nullptr.
QMetaMethod()可以用作通配符,表示“任何信号”或“接收对象中的任何槽”。以相同的方式, nullptr 可以用于“任何接收对象”的含义。在这种情况下,方法也应该是QMetaMethod()。参数永远不应该是 nullptr

This function was introduced in Qt 4.8.
这个函数是在Qt 4.8中引入的。

**See also **disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method).
参见(const QObject *sender,const char *signal,const QObject *receiver,const char *method)。

bool QObject::disconnect(const char *signal = nullptr, const QObject *receiver = nullptr, const char *method = nullptr) const

This function overloads disconnect().
此函数重载disconnect()。

Disconnects signal from method of receiver. 从断开连接。

A signal-slot connection is removed when either of the objects involved are destroyed.
当涉及的任一对象被销毁时,信号插槽连接将被删除。

Note: This function is thread-safe.
注:此功能为。

bool QObject::disconnect(const QObject *receiver, const char *method = nullptr) const

This function overloads disconnect().
此函数重载disconnect()。

Disconnects all signals in this object from receiver’s method.
将此对象中的所有信号与的断开连接。

A signal-slot connection is removed when either of the objects involved are destroyed.
当涉及的任一对象被销毁时,信号插槽连接将被删除。

[static]bool QObject::disconnect(const QMetaObject::Connection &connection)

Disconnect a connection.
断开连接。

If the connection is invalid or has already been disconnected, do nothing and return false.
如果无效或已断开连接,则不执行任何操作并返回false。

**See also **connect(). 参见()。

[static]template bool QObject::disconnect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)

This function overloads disconnect().
此函数重载disconnect()。

Disconnects signal in object sender from method in object receiver. Returns true if the connection is successfully broken; otherwise returns false.
从对象中断开对象中的连接。如果连接成功断开,则返回 true ;否则返回 false

A signal-slot connection is removed when either of the objects involved are destroyed.
当涉及的任一对象被销毁时,信号插槽连接将被删除。

disconnect() is typically used in three ways, as the following examples demonstrate.
disconnect()通常以三种方式使用,如以下示例所示。

  1. Disconnect everything connected to an object’s signals:
    断开连接到对象信号的所有设备:

    disconnect(myObject, nullptr, nullptr, nullptr);
    
  2. Disconnect everything connected to a specific signal:
    断开连接到特定信号的所有设备:

    disconnect(myObject, &MyObject::mySignal(), nullptr, nullptr);
    
  3. Disconnect a specific receiver:
    断开特定接收器:

    disconnect(myObject, nullptr, myReceiver, nullptr);
    
  4. Disconnect a connection from one specific signal to a specific slot:
    断开从一个特定信号到特定插槽的连接:

    QObject::disconnect(lineEdit, &QLineEdit::textChanged,
                     label,  &QLabel::setText);
    

nullptr may be used as a wildcard, meaning “any signal”, “any receiving object”, or “any slot in the receiving object”, respectively.
nullptr 可以用作通配符,分别表示“任何信号”、“任何接收对象”或“接收对象中的任何槽”。

The sender may never be nullptr. (You cannot disconnect signals from more than one object in a single call.)
可能永远不会 nullptr 。(You不能在单个调用中断开来自多个对象的信号。)

If signal is nullptr, it disconnects receiver and method from any signal. If not, only the specified signal is disconnected.
如果是 nullptr ,则断开和与任何信号的连接。否则,仅断开指定信号。

If receiver is nullptr, it disconnects anything connected to signal. If not, slots in objects other than receiver are not disconnected.
如果是 nullptr ,则断开连接到的任何内容。如果不是,则不会断开对象以外的插槽。

If method is nullptr, it disconnects anything that is connected to receiver. If not, only slots named method will be disconnected, and all other slots are left alone. The method must be nullptr if receiver is left out, so you cannot disconnect a specifically-named slot on all objects.
如果是 nullptr ,则断开连接到的任何内容。否则,将仅断开名为的插槽,而所有其他插槽将保持原样。如果省略,则必须是 nullptr ,这样您就无法断开所有对象上指定名称的插槽。

**Note: **It is not possible to use this overload to disconnect signals connected to functors or lambda expressions. That is because it is not possible to compare them. Instead, use the overload that takes a
注意:不能使用此重载来断开连接到函子或lambda表达式的信号。这是因为不可能比较它们。相反,请使用重载,该重载需要QMetaObject::Connection

Note: This function is thread-safe.
注:此功能为。

**See also **connect(). 参见()。

[virtual protected]void QObject::disconnectNotify(const QMetaMethod &signal)

This virtual function is called when something has been disconnected from signal in this object.
这个虚函数在对象中的某些东西被断开连接时被调用。

See connectNotify() for an example of how to compare signal with a specific signal.
有关如何与特定信号进行比较的示例,请参见()。

If all signals were disconnected from this object (e.g., the signal argument to disconnect() was nullptr), disconnectNotify() is only called once, and the signal will be an invalid QMetaMethod (QMetaMethod::isValid() returns false).
如果所有信号都与该对象断开(例如,()的信号参数是 nullptr ),disconnectNotify()只被调用一次,并且将是无效的(()返回 false )。

**Warning: **This function violates the object-oriented principle of modularity. However, it might be useful for optimizing access to expensive resources.
警告:此函数违反了面向对象的模块化原则。但是,它可能有助于优化对昂贵资源的访问。

**Warning: **This function is called from the thread which performs the disconnection, which may be a different thread from the thread in which this object lives. This function may also be called with a QObject internal mutex locked. It is therefore not allowed to re-enter any of any QObject functions from your reimplementation and if you lock a mutex in your reimplementation, make sure that you don’t call QObject functions with that mutex held in other places or it will result in a deadlock.
警告:此函数是从执行断开连接的线程调用的,该线程可能与此对象所在的线程不同。这个函数也可以在内部互斥锁被锁定的情况下调用。因此,不允许重新输入重新实现中的任何函数,如果在重新实现中锁定了互斥锁,请确保不要调用在其他地方持有互斥锁的函数,否则将导致死锁。

This function was introduced in Qt 5.0.
该功能在Qt 5.0中引入。

**See also **disconnect() and connectNotify().
参见()和()。

void QObject::dumpObjectInfo() const

Dumps information about signal connections, etc. for this object to the debug output.
转储有关信号连接等的信息。将此对象的值添加到调试输出。

**Note: **Before Qt 5.9, this function was not const.
注意:在Qt 5.9之前,这个函数不是const。

**See also **dumpObjectTree(). 参见()。

void QObject::dumpObjectTree() const

Dumps a tree of children to the debug output.
将子级树转储到调试输出。

**Note: **Before Qt 5.9, this function was not const.
注意:在Qt 5.9之前,这个函数不是const。

**See also **dumpObjectInfo(). 参见()。

QList QObject::dynamicPropertyNames() const

Returns the names of all properties that were dynamically added to the object using setProperty().
返回使用()动态添加到对象的所有属性的名称。

This function was introduced in Qt 4.2.
此功能在Qt 4.2中引入。

[virtual]bool QObject::event(QEvent *e)

This virtual function receives events to an object and should return true if the event e was recognized and processed.
此虚函数接收对象的事件,如果事件被识别并处理,则应返回true。

The event() function can be reimplemented to customize the behavior of an object.
可以重新实现event()函数来自定义对象的行为。

Make sure you call the parent event class implementation for all the events you did not handle.
确保为所有未处理的事件调用父事件类实现。

Example: 范例:

class MyClass : public QWidget
{
    Q_OBJECT

public:
    MyClass(QWidget *parent = nullptr);
    ~MyClass();

    bool event(QEvent* ev) override
    {
        if (ev->type() == QEvent::PolishRequest) {
            // overwrite handling of PolishRequest if any
            doThings();
            return true;
        } else  if (ev->type() == QEvent::Show) {
            // complement handling of Show if any
            doThings2();
            QWidget::event(ev);
            return true;
        }
        // Make sure the rest of events are handled
        return QWidget::event(ev);
    }
};

**See also **installEventFilter(), timerEvent(), QCoreApplication::sendEvent(), and QCoreApplication::postEvent().
参见()、()、()和()。

[virtual]bool QObject::eventFilter(QObject *watched, QEvent *event)

Filters events if this object has been installed as an event filter for the watched object.
如果此对象已作为对象的事件筛选器安装,则筛选事件。

In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
在重新实现此函数时,如果要过滤掉,即停止进一步处理,返回true;否则返回false。

Example: 范例:

class MainWindow : public QMainWindow
{
public:
    MainWindow();

protected:
    bool eventFilter(QObject *obj, QEvent *ev) override;

private:
    QTextEdit *textEdit;
};

MainWindow::MainWindow()
{
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);

    textEdit->installEventFilter(this);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == textEdit) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            qDebug() << "Ate key press" << keyEvent->key();
            return true;
        } else {
            return false;
        }
    } else {
        // pass the event on to the parent class
        return QMainWindow::eventFilter(obj, event);
    }
}

Notice in the example above that unhandled events are passed to the base class’s eventFilter() function, since the base class might have reimplemented eventFilter() for its own internal purposes.
请注意,在上面的示例中,未处理的事件被传递给基类的eventFilter()函数,因为基类可能已经重新实现了eventFilter()以用于其内部目的。

Some events, such as QEvent::ShortcutOverride must be explicitly accepted (by calling accept() on them) in order to prevent propagation.
某些事件,如必须显式接受(通过调用它们),以防止传播。

**Warning: **If you delete the receiver object in this function, be sure to return true. Otherwise, Qt will forward the event to the deleted object and the program might crash.
警告:如果你在这个函数中删除了接收者对象,请确保返回true。否则,Qt会将事件转发到被删除的对象,程序可能会崩溃。

**See also **installEventFilter(). 参见()。

template T QObject::findChild(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

Returns the child of this object that can be cast into type T and that is called name, or nullptr if there is no such object. Omitting the name argument causes all object names to be matched. The search is performed recursively, unless options specifies the option FindDirectChildrenOnly.
返回此对象的子对象,该对象可转换为类型T并被调用,如果没有此类对象,则返回 nullptr 。省略该参数将导致所有对象名称都匹配。除非指定选项FindDirectChildrenOnly,否则将递归执行搜索。

If there is more than one child matching the search, the most direct ancestor is returned. If there are several direct ancestors, it is undefined which one will be returned. In that case, findChildren() should be used.
如果有多个子代与搜索匹配,则返回最直接的祖先。如果有几个直接祖先,则不确定将返回哪一个。在这种情况下,应该使用()。

This example returns a child QPushButton of parentWidget named "button1", even if the button isn’t a direct child of the parent:
这个例子返回一个名为 "button1"parentWidget 的子元素 QPushButton ,即使按钮不是父元素的直接子元素:

QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

This example returns a QListWidget child of parentWidget:
此示例返回 parentWidgetQListWidget 子级:

QListWidget *list = parentWidget->findChild<QListWidget *>();

This example returns a child QPushButton of parentWidget (its direct parent) named "button1":
此示例返回名为 "button1"parentWidget (其直接父级)的子级 QPushButton

QPushButton *button = parentWidget->findChild<QPushButton *>("button1", Qt::FindDirectChildrenOnly);

This example returns a QListWidget child of parentWidget, its direct parent:
此示例返回 parentWidgetQListWidget 子级, parentWidget 是其直接父级:

QListWidget *list = parentWidget->findChild<QListWidget *>(QString(), Qt::FindDirectChildrenOnly);

**See also **findChildren(). 参见()。

template QList QObject::findChildren(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. Omitting the name argument causes all object names to be matched. The search is performed recursively, unless options specifies the option FindDirectChildrenOnly.
返回此对象的所有子级,如果没有这样的对象,则返回一个空列表。省略该参数将导致所有对象名称都匹配。除非指定选项FindDirectChildrenOnly,否则将递归执行搜索。

The following example shows how to find a list of child QWidgets of the specified parentWidget named widgetname:
以下示例显示如何查找指定的名为 widgetnameparentWidget 的子级 QWidget 的列表:

QList widgets = parentWidget.findChildren("widgetname");

This example returns all QPushButtons that are children of parentWidget:
此示例返回作为 parentWidget 的子级的所有 QPushButton

QList allPButtons = parentWidget.findChildren();

This example returns all QPushButtons that are immediate children of parentWidget:
此示例返回作为 parentWidget 的直接子级的所有 QPushButton

QList childButtons = parentWidget.findChildren(QString(), Qt::FindDirectChildrenOnly);

**See also **findChild(). 参见()。

template QList QObject::findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

This function overloads findChildren().
这个函数重载了findChildren()。

Returns the children of this object that can be cast to type T and that have names matching the regular expression re, or an empty list if there are no such objects. The search is performed recursively, unless options specifies the option FindDirectChildrenOnly.
返回此对象的子级,这些子级可以强制转换为类型T,并且具有与正则表达式匹配的名称;如果没有此类对象,则返回空列表。除非指定选项FindDirectChildrenOnly,否则将递归执行搜索。

This function was introduced in Qt 5.0.
该功能在Qt 5.0中引入。

bool QObject::inherits(const char *className) const

Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
如果此对象是继承的类或继承的子类的实例,则返回 true ;否则返回 false

A class is considered to inherit itself.
一个类被认为是继承了它自己。

Example: 范例:

QTimer *timer = new QTimer;         // QTimer inherits QObject
timer->inherits("QTimer");          // returns true
timer->inherits("QObject");         // returns true
timer->inherits("QAbstractButton"); // returns false

// QVBoxLayout inherits QObject and QLayoutItem
QVBoxLayout *layout = new QVBoxLayout;
layout->inherits("QObject");        // returns true
layout->inherits("QLayoutItem");    // returns true (even though QLayoutItem is not a QObject)

If you need to determine whether an object is an instance of a particular class for the purpose of casting it, consider using qobject_cast(object) instead.
如果你需要确定一个对象是否是一个特定类的实例,考虑使用(object)。

**See also **metaObject() and qobject_cast().
参见()和()。

void QObject::installEventFilter(QObject *filterObj)

Installs an event filter filterObj on this object. For example:
在此对象上安装事件筛选器。举例来说:

monitoredObj->installEventFilter(filterObj);

An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false.
事件筛选器是一个对象,它接收发送到此对象的所有事件。过滤器可以停止事件或将其转发到此对象。事件过滤器通过其()函数接收事件。如果要过滤事件,()函数必须返回true(即停止);否则必须返回false。

If multiple event filters are installed on a single object, the filter that was installed last is activated first.
如果在单个对象上安装了多个事件筛选器,则最后安装的筛选器将首先激活。

Here’s a KeyPressEater class that eats the key presses of its monitored objects:
下面是一个 KeyPressEater 类,它会吃掉其监视对象的按键:

class KeyPressEater : public QObject
{
    Q_OBJECT
    ...

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
};

bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        qDebug("Ate key press %d", keyEvent->key());
        return true;
    } else {
        // standard event processing
        return QObject::eventFilter(obj, event);
    }
}

And here’s how to install it on two widgets:
以下是如何在两个小部件上安装它:

KeyPressEater *keyPressEater = new KeyPressEater(this);
QPushButton *pushButton = new QPushButton(this);
QListView *listView = new QListView(this);

pushButton->installEventFilter(keyPressEater);
listView->installEventFilter(keyPressEater);

The QShortcut class, for example, uses this technique to intercept shortcut key presses.
例如,该类使用这种技术来拦截快捷键。

**Warning: **If you delete the receiver object in your eventFilter() function, be sure to return true. If you return false, Qt sends the event to the deleted object and the program will crash.
警告:如果在()函数中删除接收器对象,请确保返回true。如果返回false,Qt会将事件发送给被删除的对象,程序将崩溃。

Note that the filtering object must be in the same thread as this object. If filterObj is in a different thread, this function does nothing. If either filterObj or this object are moved to a different thread after calling this function, the event filter will not be called until both objects have the same thread affinity again (it is not removed).
请注意,筛选对象必须与此对象位于同一线程中。如果在另一个线程中,则此函数不执行任何操作。如果在调用此函数后将此对象或其中之一移动到不同的线程,则将不会调用事件筛选器,直到两个对象再次具有相同的线程关联(不会删除)。

**See also **removeEventFilter(), eventFilter(), and event().
参见()、()和()。

[protected]bool QObject::isSignalConnected(const QMetaMethod &signal) const

Returns true if the signal is connected to at least one receiver, otherwise returns false.
如果连接到至少一个接收器,则返回 true ,否则返回 false

signal must be a signal member of this object, otherwise the behaviour is undefined.
必须是此对象的信号成员,否则行为未定义。

static const QMetaMethod valueChangedSignal = QMetaMethod::fromSignal(&MyObject::valueChanged);
if (isSignalConnected(valueChangedSignal)) {
    QByteArray data;
    data = get_the_value();       // expensive operation
    emit valueChanged(data);
}

As the code snippet above illustrates, you can use this function to avoid emitting a signal that nobody listens to.
如上面的代码片段所示,您可以使用此函数来避免发出无人监听的信号。

**Warning: **This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.
警告:此函数违反了面向对象的模块化原则。但是,当您需要执行昂贵的初始化时,只有当某些东西连接到信号时,它才可能有用。

This function was introduced in Qt 5.0.
该功能在Qt 5.0中引入。

bool QObject::isWidgetType() const

Returns true if the object is a widget; otherwise returns false.
如果对象是小部件,则返回 true ;否则返回 false

Calling this function is equivalent to calling inherits("QWidget"), except that it is much faster.
调用这个函数与调用 inherits("QWidget") 是等价的,只是它的速度更快。

bool QObject::isWindowType() const

Returns true if the object is a window; otherwise returns false.
如果对象是窗口,则返回 true ;否则返回 false

Calling this function is equivalent to calling inherits("QWindow"), except that it is much faster.
调用这个函数与调用 inherits("QWindow") 是等价的,只是它的速度更快。

void QObject::killTimer(int id)

Kills the timer with timer identifier, id.
终止具有计时器标识符的计时器。

The timer identifier is returned by startTimer() when a timer event is started.
计时器标识符在计时器事件启动时由()返回。

**See also **timerEvent() and startTimer().
参见()和()。

[virtual]const QMetaObject *QObject::metaObject() const

Returns a pointer to the meta-object of this object.
返回指向此对象的元对象的指针。

A meta-object contains information about a class that inherits QObject, e.g. class name, superclass name, properties, signals and slots. Every QObject subclass that contains the Q_OBJECT macro will have a meta-object.
元对象包含关于继承的类的信息,例如类名、超类名、属性、信号和插槽。每个包含宏的子类都有一个元对象。

The meta-object information is required by the signal/slot connection mechanism and the property system. The inherits() function also makes use of the meta-object.
信号/插槽连接机制和属性系统需要元对象信息。()函数也使用元对象。

If you have no pointer to an actual object instance but still want to access the meta-object of a class, you can use staticMetaObject. ❓

Example: 范例:

QObject *obj = new QPushButton;
obj->metaObject()->className();             // returns "QPushButton"

QPushButton::staticMetaObject.className();  // returns "QPushButton"

**See also **staticMetaObject. 另见。

void QObject::moveToThread(QThread *targetThread)

Changes the thread affinity for this object and its children. The object cannot be moved if it has a parent. Event processing will continue in the targetThread.
更改此对象及其子对象的线程关联。如果对象具有父对象,则无法移动该对象。事件处理将在中继续。

To move an object to the main thread, use QApplication::instance() to retrieve a pointer to the current application, and then use QApplication::thread() to retrieve the thread in which the application lives. For example:
要将对象移动到主线程,请使用()检索指向当前应用程序的指针,然后使用()检索应用程序所在的线程。举例来说:

myObject->moveToThread(QApplication::instance()->thread());

If targetThread is nullptr, all event processing for this object and its children stops, as they are no longer associated with any thread.
如果为 nullptr ,则停止此对象及其子对象的所有事件处理,因为它们不再与任何线程相关联。

Note that all active timers for the object will be reset. The timers are first stopped in the current thread and restarted (with the same interval) in the targetThread. As a result, constantly moving an object between threads can postpone timer events indefinitely.
请注意,对象的所有活动计时器都将重置。计时器首先在当前线程中停止,然后在。因此,在线程之间不断移动对象可能会无限期地推迟计时器事件。

A QEvent::ThreadChange event is sent to this object just before the thread affinity is changed. You can handle this event to perform any special processing. Note that any new events that are posted to this object will be handled in the targetThread, provided it is not nullptr: when it is nullptr, no event processing for this object or its children can happen, as they are no longer associated with any thread.
就在更改线程关联之前,将事件发送到此对象。您可以处理此事件以执行任何特殊处理。请注意,如果不是 nullptr ,则发送到此对象的任何新事件都将在中处理:当它是 nullptr 时,不能发生针对该对象或其子对象的事件处理,因为它们不再与任何线程相关联。

**Warning: **This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only “push” an object from the current thread to another thread, it cannot “pull” an object from any arbitrary thread to the current thread. There is one exception to this rule however: objects with no thread affinity can be “pulled” to the current thread.
警告:此函数不是线程安全的;当前线程必须与当前线程关联相同。换句话说,这个函数只能将对象从当前线程“推”到另一个线程,它不能将对象从任意线程“拉”到当前线程。但是,这条规则有一个例外:没有线程关联的对象可以被“拉”到当前线程。

**See also **thread(). 参见()。

QObject *QObject::parent() const

Returns a pointer to the parent object.
返回指向父对象的指针。

**See also **setParent() and children().
参见()和()。

QVariant QObject::property(const char *name) const

Returns the value of the object’s name property.
返回对象属性的值。

If no such property exists, the returned variant is invalid.
如果不存在这样的属性,则返回的变量无效。

Information about all available properties is provided through the metaObject() and dynamicPropertyNames().
有关所有可用属性的信息通过()和()提供。

**See also **setProperty(), QVariant::isValid(), metaObject(), and dynamicPropertyNames().
参见()、()、()和()。

[protected]int QObject::receivers(const char *signal) const

Returns the number of receivers connected to the signal.
对象连接的接收器数。

Since both slots and signals can be used as receivers for signals, and the same connections can be made many times, the number of receivers is the same as the number of connections made from this signal.
由于槽和信号都可以用作信号的接收器,并且可以进行多次相同的连接,因此接收器的数量与从该信号进行的连接的数量相同。

When calling this function, you can use the SIGNAL() macro to pass a specific signal:
调用此函数时,可以使用 SIGNAL() 宏传递特定信号:

if (receivers(SIGNAL(valueChanged(QByteArray))) > 0) {
    QByteArray data;
    get_the_value(&data);       // expensive operation
    emit valueChanged(data);
}

**Warning: **This function violates the object-oriented principle of modularity. However, it might be useful when you need to perform expensive initialization only if something is connected to a signal.
警告:此函数违反了面向对象的模块化原则。但是,当您需要执行昂贵的初始化时,只有当某些东西连接到信号时,它才可能有用。

**See also **isSignalConnected(). 参见()。

void QObject::removeEventFilter(QObject *obj)

Removes an event filter object obj from this object. The request is ignored if such an event filter has not been installed.
从此对象中移除事件筛选器对象。如果未安装此类事件过滤器,则忽略该请求。

All event filters for this object are automatically removed when this object is destroyed.
销毁此对象时,将自动移除此对象的所有事件筛选器。

It is always safe to remove an event filter, even during event filter activation (i.e. from the eventFilter() function).
删除事件过滤器始终是安全的,即使在事件过滤器激活期间(即从()函数)。

**See also **installEventFilter(), eventFilter(), and event().
参见()、()和()。

[protected]QObject *QObject::sender() const

Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns nullptr. The pointer is valid only during the execution of the slot that calls this function from this object’s thread context.
如果在由信号激活的槽中调用,则返回指向发送信号的对象的指针;否则返回 nullptr 。该指针仅在执行从此对象的线程上下文调用此函数的槽期间有效。

The pointer returned by this function becomes invalid if the sender is destroyed, or if the slot is disconnected from the sender’s signal.
如果发送方被破坏,或者插槽与发送方的信号断开连接,则此函数返回的指针将无效。

**Warning: **This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
警告:此函数违反了面向对象的模块化原则。但是,当许多信号连接到单个插槽时,访问发送器可能很有用。

**Warning: **As mentioned above, the return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object’s thread. Do not use this function in this type of scenario.
警告:如上所述,当从与此对象的线程不同的线程通过调用槽时,此函数的返回值无效。请勿在此类方案中使用此功能。

**See also **senderSignalIndex(). 参见()。

[protected]int QObject::senderSignalIndex() const

Returns the meta-method index of the signal that called the currently executing slot, which is a member of the class returned by sender(). If called outside of a slot activated by a signal, -1 is returned.
返回调用当前执行槽的信号的元方法索引,该槽是()返回的类的成员。如果在由信号激活的插槽之外调用,则返回-1。

For signals with default parameters, this function will always return the index with all parameters, regardless of which was used with connect(). For example, the signal destroyed(QObject *obj = \nullptr) will have two different indexes (with and without the parameter), but this function will always return the index with a parameter. This does not apply when overloading signals with different parameters.
对于具有默认参数的信号,此函数将始终返回具有所有参数的索引,而不管与()一起使用的是哪个参数。例如,信号 destroyed(QObject *obj = \nullptr) 将有两个不同的索引(带参数和不带参数),但此函数将始终返回带参数的索引。当用不同的参数重载信号时,这不适用。

**Warning: **This function violates the object-oriented principle of modularity. However, getting access to the signal index might be useful when many signals are connected to a single slot.
警告:此函数违反了面向对象的模块化原则。然而,当许多信号连接到单个槽时,访问信号索引可能是有用的。

**Warning: **The return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object’s thread. Do not use this function in this type of scenario.
警告:当从与此对象的线程不同的线程通过调用插槽时,此函数的返回值无效。请勿在此类方案中使用此功能。

This function was introduced in Qt 4.8.
这个函数是在Qt 4.8中引入的。

**See also **sender(), QMetaObject::indexOfSignal(), and QMetaObject::method().
参见()、()和()。

void QObject::setParent(QObject *parent)

Makes the object a child of parent.
使对象成为的子级。

**See also **parent() and children().
参见()和()。

bool QObject::setProperty(const char *name, const QVariant &value)

Sets the value of the object’s name property to value.
将对象属性的值设置为。

If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
如果属性是在类中使用定义的,则成功时返回true,否则返回false。如果属性没有使用定义,因此没有在元对象中列出,它将作为动态属性添加,并返回false。

Information about all available properties is provided through the metaObject() and dynamicPropertyNames().
有关所有可用属性的信息通过()和()提供。

Dynamic properties can be queried again using property() and can be removed by setting the property value to an invalid QVariant. Changing the value of a dynamic property causes a QDynamicPropertyChangeEvent to be sent to the object.
可以使用()再次查询动态属性,并且可以通过将属性值设置为无效来删除动态属性。更改动态属性的值会导致向对象发送。

Note: Dynamic properties starting with “_q_” are reserved for internal purposes.
注意:以“_q_”开头的动态属性保留用于内部目的。

**See also **property(), metaObject(), dynamicPropertyNames(), and QMetaProperty::write().
参见()、()、()和()。

bool QObject::signalsBlocked() const

Returns true if signals are blocked; otherwise returns false.
如果信号被阻塞,则返回 true ;否则返回 false

Signals are not blocked by default.
默认情况下不阻止信号。

**See also **blockSignals() and QSignalBlocker.
参见()和。

int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)

Starts a timer and returns a timer identifier, or returns zero if it could not start a timer.
启动计时器并返回计时器标识符,如果无法启动计时器,则返回零。

A timer event will occur every interval milliseconds until killTimer() is called. If interval is 0, then the timer event occurs once every time there are no more window system events to process.
计时器事件将每毫秒发生一次,直到调用()。如果为0,则计时器事件在每次没有窗口系统事件要处理时发生一次。

The virtual timerEvent() function is called with the QTimerEvent event parameter class when a timer event occurs. Reimplement this function to get timer events.
当计时器事件发生时,使用event参数类调用virtual()函数。重新实现此函数以获取计时器事件。

If multiple timers are running, the QTimerEvent::timerId() can be used to find out which timer was activated.
如果有多个计时器正在运行,则()可用于找出哪个计时器被激活。

Example: 范例:

class MyObject : public QObject
{
    Q_OBJECT

public:
    MyObject(QObject *parent = nullptr);

protected:
    void timerEvent(QTimerEvent *event) override;
};

MyObject::MyObject(QObject *parent)
    : QObject(parent)
{
    startTimer(50);     // 50-millisecond timer
    startTimer(1000);   // 1-second timer
    startTimer(60000);  // 1-minute timer

    using namespace std::chrono;
    startTimer(milliseconds(50));
    startTimer(seconds(1));
    startTimer(minutes(1));

    // since C++14 we can use std::chrono::duration literals, e.g.:
    startTimer(100ms);
    startTimer(5s);
    startTimer(2min);
    startTimer(1h);
}

void MyObject::timerEvent(QTimerEvent *event)
{
    qDebug() << "Timer ID:" << event->timerId();
}

Note that QTimer’s accuracy depends on the underlying operating system and hardware. The timerType argument allows you to customize the accuracy of the timer. See Qt::TimerType for information on the different timer types. Most platforms support an accuracy of 20 milliseconds; some provide more. If Qt is unable to deliver the requested number of timer events, it will silently discard some.
请注意,的准确性取决于底层操作系统和硬件。参数允许您自定义计时器的精度。有关不同计时器类型的信息,请参见。大多数平台支持20毫秒的精度;有些人提供更多。如果Qt无法提供请求数量的计时器事件,它将默默地丢弃一些。

The QTimer class provides a high-level programming interface with single-shot timers and timer signals instead of events. There is also a QBasicTimer class that is more lightweight than QTimer and less clumsy than using timer IDs directly.
该类提供了一个高级编程接口,其中包含单次计时器和计时器信号,而不是事件。还有一个类比直接使用计时器ID更轻量级,也不那么笨拙。

**See also **timerEvent(), killTimer(), and QTimer::singleShot().
参见()、()和()。

int QObject::startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)

This is an overloaded function.
这是一个重载函数。

Starts a timer and returns a timer identifier, or returns zero if it could not start a timer.
启动计时器并返回计时器标识符,如果无法启动计时器,则返回零。

A timer event will occur every time interval until killTimer() is called. If time is equal to std::chrono::duration::zero(), then the timer event occurs once every time there are no more window system events to process.
定时器事件将在每个间隔发生,直到调用()。如果等于 std::chrono::duration::zero() ,则计时器事件在每次没有更多窗口系统事件要处理时发生一次。

The virtual timerEvent() function is called with the QTimerEvent event parameter class when a timer event occurs. Reimplement this function to get timer events.
当计时器事件发生时,使用event参数类调用virtual()函数。重新实现此函数以获取计时器事件。

If multiple timers are running, the QTimerEvent::timerId() can be used to find out which timer was activated.
如果有多个计时器正在运行,则()可用于找出哪个计时器被激活。

Example: 范例:

class MyObject : public QObject
{
    Q_OBJECT

public:
    MyObject(QObject *parent = nullptr);

protected:
    void timerEvent(QTimerEvent *event) override;
};

MyObject::MyObject(QObject *parent)
    : QObject(parent)
{
    startTimer(50);     // 50-millisecond timer
    startTimer(1000);   // 1-second timer
    startTimer(60000);  // 1-minute timer

    using namespace std::chrono;
    startTimer(milliseconds(50));
    startTimer(seconds(1));
    startTimer(minutes(1));

    // since C++14 we can use std::chrono::duration literals, e.g.:
    startTimer(100ms);
    startTimer(5s);
    startTimer(2min);
    startTimer(1h);
}

void MyObject::timerEvent(QTimerEvent *event)
{
    qDebug() << "Timer ID:" << event->timerId();
}

Note that QTimer’s accuracy depends on the underlying operating system and hardware. The timerType argument allows you to customize the accuracy of the timer. See Qt::TimerType for information on the different timer types. Most platforms support an accuracy of 20 milliseconds; some provide more. If Qt is unable to deliver the requested number of timer events, it will silently discard some.
请注意,的准确性取决于底层操作系统和硬件。参数允许您自定义计时器的精度。有关不同计时器类型的信息,请参见。大多数平台支持20毫秒的精度;有些人提供更多。如果Qt无法提供请求数量的计时器事件,它将默默地丢弃一些。

The QTimer class provides a high-level programming interface with single-shot timers and timer signals instead of events. There is also a QBasicTimer class that is more lightweight than QTimer and less clumsy than using timer IDs directly.
该类提供了一个高级编程接口,其中包含单次计时器和计时器信号,而不是事件。还有一个类比直接使用计时器ID更轻量级,也不那么笨拙。

This function was introduced in Qt 5.9.
该功能在Qt 5.9中引入。

**See also **timerEvent(), killTimer(), and QTimer::singleShot().
参见()、()和()。

QThread *QObject::thread() const

Returns the thread in which the object lives.
返回对象所在的线程。

**See also **moveToThread(). 参见()。

[virtual protected]void QObject::timerEvent(QTimerEvent *event)

This event handler can be reimplemented in a subclass to receive timer events for the object.
此事件处理程序可以在子类中重新实现,以接收对象的计时器事件。

QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the event parameter.
提供了定时器功能的高级接口,以及有关定时器的更多常规信息。计时器事件在参数中传递。

**See also **startTimer(), killTimer(), and event().
参见()、()和()。

[static]QString QObject::tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1)

Returns a translated version of sourceText, optionally based on a disambiguation string and value of n for strings containing plurals; otherwise returns QString::fromUtf8(sourceText) if no appropriate translated string is available.
返回的翻译版本,对于包含复数的字符串,可以选择基于字符串和的值;否则,如果没有合适的翻译字符串可用,则返回()。

Example: 范例:

void MainWindow::createActions()
{
    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    ...

If the same sourceText is used in different roles within the same context, an additional identifying string may be passed in disambiguation (nullptr by default). In Qt 4.4 and earlier, this was the preferred way to pass comments to translators.
如果相同的内容在同一个上下文中的不同角色中使用,则可以传入一个额外的标识字符串(默认情况下为 nullptr )。在Qt 4.4和更早的版本中,这是将注释传递给翻译人员的首选方式。

Example: 范例:

MyWindow::MyWindow()
{
    QLabel *senderLabel = new QLabel(tr("Name:"));
    QLabel *recipientLabel = new QLabel(tr("Name:", "recipient"));
    ...

See Writing Source Code for Translation for a detailed description of Qt’s translation mechanisms in general, and the Disambiguation section for information on disambiguation.
请参阅一般的Qt翻译机制的详细描述,以及有关消歧的信息。

**Warning: **This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will probably result in crashes or other undesirable behavior.
警告:只有在调用此方法之前安装了所有转换器,此方法才是可重入的。不支持在执行翻译时安装或删除翻译器。这样做可能会导致崩溃或其他不良行为。

**See also **QCoreApplication::translate() and Internationalization with Qt.
参见()和。

Member Variable Documentation
成员变量文档

const QMetaObject QObject::staticMetaObject

This variable stores the meta-object for the class.
此变量存储类的元对象。

A meta-object contains information about a class that inherits QObject, e.g. class name, superclass name, properties, signals and slots. Every class that contains the Q_OBJECT macro will also have a meta-object.
元对象包含关于继承的类的信息,例如类名、超类名、属性、信号和插槽。每个包含宏的类也将有一个元对象。

The meta-object information is required by the signal/slot connection mechanism and the property system. The inherits() function also makes use of the meta-object.
信号/插槽连接机制和属性系统需要元对象信息。()函数也使用元对象。

If you have a pointer to an object, you can use metaObject() to retrieve the meta-object associated with that object.
如果你有一个指向一个对象的指针,你可以使用()来检索与该对象相关联的元对象。

Example: 范例:

QPushButton::staticMetaObject.className();  // returns "QPushButton"

QObject *obj = new QPushButton;
obj->metaObject()->className();             // returns "QPushButton"

**See also **metaObject(). 参见()。

Related Non-Members 相关非成员

template T qobject_cast(QObject *object)

template T qobject_cast(const QObject *object)

Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns nullptr. If object is nullptr then it will also return nullptr.
如果对象是T类型(或子类),则返回给定的类型转换为T;否则返回 nullptr 。如果是 nullptr ,那么它也会返回 nullptr

The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro.
类T必须继承(直接或间接)并与宏一起声明。

A class is considered to inherit itself.
一个类被认为是继承了它自己。

Example: 范例:

QObject *obj = new QTimer;          // QTimer inherits QObject

QTimer *timer = qobject_cast(obj);
// timer == (QObject *)obj

QAbstractButton *button = qobject_cast(obj);
// button == nullptr

The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn’t require RTTI support and it works across dynamic library boundaries.
()函数的行为类似于标准C++ dynamic_cast() ,优点是它不需要RTTI支持,并且可以跨动态库边界工作。

qobject_cast() can also be used in conjunction with interfaces; see the Plug & Paint example for details.
()也可以与接口结合使用;详情请参见示例。

**Warning: **If T isn’t declared with the Q_OBJECT macro, this function’s return value is undefined.
警告:如果T没有在宏中声明,则此函数的返回值是未定义的。

**See also **QObject::inherits(). 参见()。

typedef QObjectList

Synonym for QList. < *>的同义词。

template QList qFindChildren(const QObject *obj, const QRegExp &regExp)

This function overloads qFindChildren().
此函数重载qFindChildren()。

This function is equivalent to obj->findChildren(regExp).
这个函数相当于->()。

**Note: **This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.
注意:此函数是作为不支持成员模板函数的MSVC 6的解决方案提供的。建议在新代码中使用另一种形式。

**See also **QObject::findChildren(). 参见()。

Macro Documentation 宏文档

QT_NO_NARROWING_CONVERSIONS_IN_CONNECT

Defining this macro will disable narrowing and floating-point-to-integral conversions between the arguments carried by a signal and the arguments accepted by a slot, when the signal and the slot are connected using the PMF-based syntax.
当使用基于PMF的语法连接信号和槽时,定义此宏将禁用信号携带的参数与槽接受的参数之间的收缩和浮点到整数转换。

This function was introduced in Qt 5.8.
此功能在Qt 5.8中引入。

**See also **QObject::connect. 另见。

Q_CLASSINFO(Name, Value)

This macro associates extra information to the class, which is available using QObject::metaObject(). Qt makes only limited use of this feature, in the Active Qt, Qt D-Bus and Qt QML.
这个宏将额外的信息关联到类,可以使用()来获得。Qt仅在、和中有限地使用了此功能。

The extra information takes the form of a Name string and a Value literal string.
额外信息采用字符串和文字字符串的形式。

Example: 范例:

class MyClass : public QObject
{
    Q_OBJECT
    Q_CLASSINFO("Author", "Pierre Gendron")
    Q_CLASSINFO("URL", "http://www.my-organization.qc.ca")

public:
    ...
};

**See also **QMetaObject::classInfo(), QAxFactory, Using Qt D-Bus Adaptors, and Extending QML.
参见()、、和。

Q_DISABLE_COPY(Class)

Disables the use of copy constructors and assignment operators for the given Class.
对象禁用复制构造函数和赋值运算符。

Instances of subclasses of QObject should not be thought of as values that can be copied or assigned, but as unique identities. This means that when you create your own subclass of QObject (director or indirect), you should not give it a copy constructor or an assignment operator. However, it may not enough to simply omit them from your class, because, if you mistakenly write some code that requires a copy constructor or an assignment operator (it’s easy to do), your compiler will thoughtfully create it for you. You must do more.
的子类不应被视为可以复制或分配的值,而应视为唯一标识。这意味着当你创建自己的子类(director或indirect)时,你不应该给予它一个复制构造函数或赋值运算符。然而,仅仅从类中省略它们可能还不够,因为,如果你错误地编写了一些需要复制构造函数或赋值运算符的代码(这很容易做到),你的编译器会为你精心创建它。你必须做得更多。

The curious user will have seen that the Qt classes derived from QObject typically include this macro in a private section:
好奇的用户会看到,从派生的Qt类通常在私有部分包含这个宏:

class MyClass : public QObject
{
private:
    Q_DISABLE_COPY(MyClass)
};

It declares a copy constructor and an assignment operator in the private section, so that if you use them by mistake, the compiler will report an error.
它在private部分声明了一个复制构造函数和一个赋值操作符,这样如果你错误地使用了它们,编译器将报告一个错误。

class MyClass : public QObject
{
private:
    MyClass(const MyClass &) = delete;
    MyClass &operator=(const MyClass &) = delete;
};

But even this might not catch absolutely every case. You might be tempted to do something like this:
但即使这样也不可能完全抓住每一个病例。你可能会被诱惑去做这样的事情:

QWidget w = QWidget();

First of all, don’t do that. Most compilers will generate code that uses the copy constructor, so the privacy violation error will be reported, but your C++ compiler is not required to generate code for this statement in a specific way. It could generate code using neither the copy constructor nor the assignment operator we made private. In that case, no error would be reported, but your application would probably crash when you called a member function of w.
首先,别那么做.大多数编译器将生成使用复制构造函数的代码,因此将报告隐私侵犯错误,但您的C++编译器不需要以特定方式为该语句生成代码。它可以生成既不使用复制构造函数也不使用赋值运算符的代码。在这种情况下,不会报告任何错误,但当您调用 w 的成员函数时,您的应用程序可能会崩溃。

**See also **Q_DISABLE_COPY_MOVE and Q_DISABLE_MOVE. 请参见和。

Q_DISABLE_COPY_MOVE(Class)

A convenience macro that disables the use of copy constructors, assignment operators, move constructors and move assignment operators for the given Class, combining Q_DISABLE_COPY and Q_DISABLE_MOVE.
一个方便的宏,它禁止使用复制构造函数、赋值运算符、移动构造函数和移动赋值运算符。

This function was introduced in Qt 5.13.
这个函数是在Qt 5.13中引入的。

**See also **Q_DISABLE_COPY and Q_DISABLE_MOVE. 请参见和。

Q_DISABLE_MOVE(Class)

Disables the use of move constructors and move assignment operators for the given Class.
对象禁用移动构造函数和移动赋值运算符。

This function was introduced in Qt 5.13.
这个函数是在Qt 5.13中引入的。

**See also **Q_DISABLE_COPY and Q_DISABLE_COPY_MOVE. 请参见和。

Q_EMIT

Use this macro to replace the emit keyword for emitting signals, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism.
当你想使用Qt信号和插槽时,使用这个宏来替换发射信号的 emit 关键字。

The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.
该宏通常在 .pro 文件中的 CONFIG 变量指定了 no_keywords 时使用,但即使没有指定 no_keywords 也可以使用。

Q_ENUM()

This macro registers an enum type with the meta-object system. It must be placed after the enum declaration in a class that has the Q_OBJECT or the Q_GADGET macro. For namespaces use Q_ENUM_NS() instead.
这个宏向元对象系统注册一个枚举类型。在具有或宏的类中,它必须放置在枚举声明之后。对于命名空间,请使用()。

For example: 举例来说:

class MyClass : public QObject
{
    Q_OBJECT

public:
    MyClass(QObject *parent = nullptr);
    ~MyClass();

    enum Priority { High, Low, VeryHigh, VeryLow };
    Q_ENUM(Priority)
    void setPriority(Priority priority);
    Priority priority() const;
};

Enumerations that are declared with Q_ENUM have their QMetaEnum registered in the enclosing QMetaObject. You can also use QMetaEnum::fromType() to get the QMetaEnum.
使用Q_ENUM声明的枚举在封闭的.也可以使用()来获取。

Registered enumerations are automatically registered also to the Qt meta type system, making them known to QMetaType without the need to use Q_DECLARE_METATYPE(). This will enable useful features; for example, if used in a QVariant, you can convert them to strings. Likewise, passing them to QDebug will print out their names.
已注册的枚举也会自动注册到Qt Meta类型系统中,使它们为Qt所知,而不需要使用()。这将启用有用的功能;例如,如果在中使用,则可以将它们转换为字符串。同样,将它们传递给将打印出它们的名字。

Mind that the enum values are stored as signed int in the meta object system. Registering enumerations with values outside the range of values valid for int will lead to overflows and potentially undefined behavior when accessing them through the meta object system. QML, for example, does access registered enumerations through the meta object system.
请注意,枚举值在Meta对象系统中存储为带符号的 int 。使用 int 有效值范围之外的值注册枚举将导致溢出,并在通过Meta对象系统访问枚举时可能导致未定义的行为。例如,QML通过Meta对象系统访问注册的枚举。

This function was introduced in Qt 5.5.
该功能在Qt 5.5中引入。

**See also **Qt’s Property System. 另见。

Q_ENUM_NS()

This macro registers an enum type with the meta-object system. It must be placed after the enum declaration in a namespace that has the Q_NAMESPACE macro. It is the same as Q_ENUM but in a namespace.
这个宏向元对象系统注册一个枚举类型。它必须放置在具有宏的命名空间中的枚举声明之后。它与相同,但在名称空间中。

Enumerations that are declared with Q_ENUM_NS have their QMetaEnum registered in the enclosing QMetaObject. You can also use QMetaEnum::fromType() to get the QMetaEnum.
使用Q_ENUM_NS声明的枚举在封闭的。也可以使用()来获取。

Registered enumerations are automatically registered also to the Qt meta type system, making them known to QMetaType without the need to use Q_DECLARE_METATYPE(). This will enable useful features; for example, if used in a QVariant, you can convert them to strings. Likewise, passing them to QDebug will print out their names.
已注册的枚举也会自动注册到Qt Meta类型系统中,使它们为Qt所知,而不需要使用()。这将启用有用的功能;例如,如果在中使用,则可以将它们转换为字符串。同样,将它们传递给将打印出它们的名字。

Mind that the enum values are stored as signed int in the meta object system. Registering enumerations with values outside the range of values valid for int will lead to overflows and potentially undefined behavior when accessing them through the meta object system. QML, for example, does access registered enumerations through the meta object system.
请注意,枚举值在Meta对象系统中存储为带符号的 int 。使用 int 有效值范围之外的值注册枚举将导致溢出,并在通过Meta对象系统访问枚举时可能导致未定义的行为。例如,QML通过Meta对象系统访问注册的枚举。

This function was introduced in Qt 5.8.
此功能在Qt 5.8中引入。

**See also **Qt’s Property System. 另见。

Q_FLAG()

This macro registers a single flags type with the meta-object system. It is typically used in a class definition to declare that values of a given enum can be used as flags and combined using the bitwise OR operator. For namespaces use Q_FLAG_NS() instead.
这个宏向元对象系统注册一个。它通常在类定义中使用,以声明给定枚举的值可以用作标志,并使用按位OR运算符进行组合。对于命名空间,请使用()。

The macro must be placed after the enum declaration. The declaration of the flags type is done using the Q_DECLARE_FLAGS() macro.
宏必须放在枚举声明之后。flags类型的声明是使用()宏完成的。

For example, in QItemSelectionModel, the SelectionFlags flag is declared in the following way:
例如,在中,标志以以下方式声明:

class QItemSelectionModel : public QObject
{
    Q_OBJECT

public:
    ...
    enum SelectionFlag {
        NoUpdate       = 0x0000,
        Clear          = 0x0001,
        Select         = 0x0002,
        Deselect       = 0x0004,
        Toggle         = 0x0008,
        Current        = 0x0010,
        Rows           = 0x0020,
        Columns        = 0x0040,
        SelectCurrent  = Select | Current,
        ToggleCurrent  = Toggle | Current,
        ClearAndSelect = Clear | Select
    };

    Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
    Q_FLAG(SelectionFlags)
    ...
}

**Note: **The Q_FLAG macro takes care of registering individual flag values with the meta-object system, so it is unnecessary to use Q_ENUM() in addition to this macro.
注意:Q_FLAG宏负责向元对象系统注册各个标志值,因此没有必要在此宏之外使用()。

This function was introduced in Qt 5.5.
该功能在Qt 5.5中引入。

**See also **Qt’s Property System. 另见。

Q_FLAG_NS()

This macro registers a single flags type with the meta-object system. It is used in a namespace that has the Q_NAMESPACE macro, to declare that values of a given enum can be used as flags and combined using the bitwise OR operator. It is the same as Q_FLAG but in a namespace.
这个宏向元对象系统注册一个。它用于具有宏的命名空间中,以声明给定枚举的值可以用作标志并使用按位OR运算符组合。它与相同,但在名称空间中。

The macro must be placed after the enum declaration.
宏必须放在枚举声明之后。

**Note: **The Q_FLAG_NS macro takes care of registering individual flag values with the meta-object system, so it is unnecessary to use Q_ENUM_NS() in addition to this macro.
注意:Q_FLAG_NS宏负责向元对象系统注册各个标志值,因此除了这个宏之外,没有必要使用()。

This function was introduced in Qt 5.8.
此功能在Qt 5.8中引入。

**See also **Qt’s Property System. 另见。

Q_GADGET

The Q_GADGET macro is a lighter version of the Q_OBJECT macro for classes that do not inherit from QObject but still want to use some of the reflection capabilities offered by QMetaObject. Just like the Q_OBJECT macro, it must appear in the private section of a class definition.
Q_GADGET宏是该宏的精简版本,适用于不从继承但仍希望使用提供的某些反射功能的类。就像宏一样,它必须出现在类定义的私有部分。

Q_GADGETs can have Q_ENUM, Q_PROPERTY and Q_INVOKABLE, but they cannot have signals or slots.
Q_GADG可以有、和,但它们不能有信号或插槽。

Q_GADGET makes a class member, staticMetaObject, available. staticMetaObject is of type QMetaObject and provides access to the enums declared with Q_ENUMS.
Q_GADGET使类成员 staticMetaObject 可用。 staticMetaObject 是类型,提供对使用Q_ENUMS声明的枚举的访问。

Q_INTERFACES()

This macro tells Qt which interfaces the class implements. This is used when implementing plugins.
这个宏告诉Qt类实现了哪些接口。这在实现插件时使用。

Example: 范例:

class BasicToolsPlugin : public QObject,
                         public BrushInterface,
                         public ShapeInterface,
                         public FilterInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.BrushInterface" FILE "basictools.json")
    Q_INTERFACES(BrushInterface ShapeInterface FilterInterface)

public:
    ...
};

See the Plug & Paint Basic Tools example for details.
有关详细信息,请参见示例。

**See also **Q_DECLARE_INTERFACE(), Q_PLUGIN_METADATA(), and How to Create Qt Plugins.
参见()、()和。

Q_INVOKABLE

Apply this macro to declarations of member functions to allow them to be invoked via the meta-object system. The macro is written before the return type, as shown in the following example:
将此宏应用于成员函数的声明,以允许通过元对象系统调用它们。宏在返回类型之前写入,如下例所示:

class Window : public QWidget
{
    Q_OBJECT

public:
    Window();
    void normalMethod();
    Q_INVOKABLE void invokableMethod();
};

The invokableMethod() function is marked up using Q_INVOKABLE, causing it to be registered with the meta-object system and enabling it to be invoked using QMetaObject::invokeMethod(). Since normalMethod() function is not registered in this way, it cannot be invoked using QMetaObject::invokeMethod().
使用Q_INVOKABLE标记 invokableMethod() 函数,使其注册到元对象系统,并使其能够使用()调用。由于 normalMethod() 函数没有以这种方式注册,因此无法使用()调用。

If an invokable member function returns a pointer to a QObject or a subclass of QObject and it is invoked from QML, special ownership rules apply. See Data Type Conversion Between QML and C++ for more information.
如果一个可调用的成员函数返回一个指向或子类的指针,并且它是从QML调用的,则应用特殊的所有权规则。有关详细信息,请参见。

Q_NAMESPACE

The Q_NAMESPACE macro can be used to add QMetaObject capabilities to a namespace.
Q_NAMESPACE宏可用于向名称空间添加功能。

Q_NAMESPACEs can have Q_CLASSINFO, Q_ENUM_NS, Q_FLAG_NS, but they cannot have Q_ENUM, Q_FLAG, Q_PROPERTY, Q_INVOKABLE, signals nor slots.
Q_NAMESPACEes可以有、,但不能有、信号或插槽。

Q_NAMESPACE makes an external variable, staticMetaObject, available. staticMetaObject is of type QMetaObject and provides access to the enums declared with Q_ENUM_NS/Q_FLAG_NS.
Q_NAMESPACE使外部变量 staticMetaObject 可用。 staticMetaObject 是类型,提供对使用/声明的枚举的访问。

For example: 举例来说:

namespace test {
Q_NAMESPACE
...

This function was introduced in Qt 5.8.
此功能在Qt 5.8中引入。

**See also **Q_NAMESPACE_EXPORT. 另见。

Q_NAMESPACE_EXPORT(EXPORT_MACRO)

The Q_NAMESPACE_EXPORT macro can be used to add QMetaObject capabilities to a namespace.
Q_NAMESPACE_EXPORT宏可用于向名称空间添加功能。

It works exactly like the Q_NAMESPACE macro. However, the external staticMetaObject variable that gets defined in the namespace is declared with the supplied EXPORT_MACRO qualifier. This is useful if the object needs to be exported from a dynamic library.
它的工作原理与宏完全相同。但是,在命名空间中定义的外部 staticMetaObject 变量是用提供的限定符声明的。如果需要从动态库导出对象,这将非常有用。

For example: 举例来说:

namespace test {
Q_NAMESPACE_EXPORT(EXPORT_MACRO)
...

This function was introduced in Qt 5.14.
该功能在Qt 5.14中引入。

**See also **Q_NAMESPACE and Creating Shared Libraries. 请参见和。

Q_OBJECT

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt’s meta-object system.
Q_OBJECT宏必须出现在声明自己的信号和插槽或使用Qt元对象系统提供的其他服务的类定义的私有部分。

For example: 举例来说:

#include 

class Counter : public QObject
{
    Q_OBJECT

public:
    Counter() { m_value = 0; }

    int value() const { return m_value; }

public slots:
    void setValue(int value);

signals:
    void valueChanged(int newValue);

private:
    int m_value;
};

**Note: **This macro requires the class to be a subclass of QObject. Use Q_GADGET instead of Q_OBJECT to enable the meta object system’s support for enums in a class that is not a QObject subclass.
注意:这个宏要求类是的子类。使用而不是Q_OBLOG启用Meta对象系统对不是子类的类中的枚举的支持。

**See also **Meta-Object System, Signals and Slots, and Qt’s Property System.
另请参见、和。

Q_PROPERTY()

This macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System.
此宏用于在继承的类中声明属性。属性的行为类似于类数据成员,但它们具有可通过。

Q_PROPERTY(type name
           (READ getFunction [WRITE setFunction] |
            MEMBER memberName [(READ getFunction | WRITE setFunction)])
           [RESET resetFunction]
           [NOTIFY notifySignal]
           [REVISION int]
           [DESIGNABLE bool]
           [SCRIPTABLE bool]
           [STORED bool]
           [USER bool]
           [CONSTANT]
           [FINAL]
           [REQUIRED])

The property name and type and the READ function are required. The type can be any type supported by QVariant, or it can be a user-defined type. The other items are optional, but a WRITE function is common. The attributes default to true except USER, which defaults to false.
属性名称和类型以及 READ 函数是必需的。该类型可以是支持的任何类型,也可以是用户定义的类型。其他项目是可选的,但 WRITE 功能是常见的。这些属性默认为true,除了 USER ,它默认为false。

For example: 举例来说:

Q_PROPERTY(QString title READ title WRITE setTitle USER true)

For more details about how to use this macro, and a more detailed example of its use, see the discussion on Qt’s Property System.
有关如何使用此宏的更多详细信息,以及更详细的使用示例,请参阅上的讨论。

**See also **Qt’s Property System. 另见。

Q_REVISION

Apply this macro to declarations of member functions to tag them with a revision number in the meta-object system. The macro is written before the return type, as shown in the following example:
将此宏应用于成员函数的声明,以在元对象系统中用修订号标记它们。宏在返回类型之前写入,如下例所示:

class Window : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int normalProperty READ normalProperty)
    Q_PROPERTY(int newProperty READ newProperty REVISION 1)

public:
    Window();
    int normalProperty();
    int newProperty();
public slots:
    void normalMethod();
    Q_REVISION(1) void newMethod();
};

This is useful when using the meta-object system to dynamically expose objects to another API, as you can match the version expected by multiple versions of the other API. Consider the following simplified example:
这在使用元对象系统向另一个API动态公开对象时非常有用,因为您可以匹配其他API的多个版本所期望的版本。考虑以下简化示例:

    Window window;
    int expectedRevision = 0;
    const QMetaObject *windowMetaObject = window.metaObject();
    for (int i=0; i < windowMetaObject->methodCount(); i++)
        if (windowMetaObject->method(i).revision() <= expectedRevision)
            exposeMethod(windowMetaObject->method(i));
    for (int i=0; i < windowMetaObject->propertyCount(); i++)
        if (windowMetaObject->property(i).revision() <= expectedRevision)
            exposeProperty(windowMetaObject->property(i));

Using the same Window class as the previous example, the newProperty and newMethod would only be exposed in this code when the expected version is 1 or greater.
使用与上一个示例相同的Window类,只有当预期版本为1或更高时,才在此代码中公开newProperty和newMethod。

Since all methods are considered to be in revision 0 if untagged, a tag of Q_REVISION(0) is invalid and ignored.
由于所有方法在未标记的情况下都被视为修订版本0,因此Q_REVISION(0)标记无效并被忽略。

This tag is not used by the meta-object system itself. Currently this is only used by the QtQml module.
元对象系统本身不使用此标记。目前,它仅由模块使用。

For a more generic string tag, see QMetaMethod::tag()
有关更通用的字符串标记,请参见()

**See also **QMetaMethod::revision(). 参见()。

Q_SET_OBJECT_NAME(Object)

This macro assigns Object the objectName “Object”.
此宏指定“对象”。

It doesn’t matter whether Object is a pointer or not, the macro figures that out by itself.
是否是指针并不重要,宏本身就能说明这一点。

This function was introduced in Qt 5.0.
该功能在Qt 5.0中引入。

**See also **QObject::objectName(). 参见()。

Q_SIGNAL

This is an additional macro that allows you to mark a single function as a signal. It can be quite useful, especially when you use a 3rd-party source code parser which doesn’t understand a signals or Q_SIGNALS groups. ❓

Use this macro to replace the signals keyword in class declarations, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism. ❓

The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.
该宏通常在 .pro 文件中的 CONFIG 变量指定了 no_keywords 时使用,但即使没有指定 no_keywords 也可以使用。

Q_SIGNALS

Use this macro to replace the signals keyword in class declarations, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism.
使用这个宏来替换类声明中的 signals 关键字,当你想使用Qt信号和插槽。

The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.
该宏通常在 .pro 文件中的 CONFIG 变量指定了 no_keywords 时使用,但即使没有指定 no_keywords 也可以使用。

Q_SLOT

This is an additional macro that allows you to mark a single function as a slot. It can be quite useful, especially when you use a 3rd-party source code parser which doesn’t understand a slots or Q_SLOTS groups.
这是一个额外的宏,允许您将单个函数标记为插槽。它可能非常有用,尤其是当你使用第三方源代码解析器时,它不理解 slotsQ_SLOTS 组。

Use this macro to replace the slots keyword in class declarations, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism.
使用这个宏来替换类声明中的 slots 关键字,当你想使用Qt信号和插槽。

The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.
该宏通常在 .pro 文件中的 CONFIG 变量指定了 no_keywords 时使用,但即使没有指定 no_keywords 也可以使用。

Q_SLOTS

Use this macro to replace the slots keyword in class declarations, when you want to use Qt Signals and Slots with a 3rd party signal/slot mechanism.
使用这个宏来替换类声明中的 slots 关键字,当你想使用Qt信号和插槽。

The macro is normally used when no_keywords is specified with the CONFIG variable in the .pro file, but it can be used even when no_keywords is not specified.
该宏通常在 .pro 文件中的 CONFIG 变量指定了 no_keywords 时使用,但即使没有指定 no_keywords 也可以使用。

你可能感兴趣的:(Qt5)