Qt Signal Slot Reference

admin

Example - qt signal slot reference. Before signal slot called after signal 予想. Qt Version State New Features Download Online Installers (All Downloads) Linux (32 bit) Linux (64 bit) Mac Windows Qt 4.8.7 End-of-Life (as of December 2015) Go to download directory: Qt 5.12 Long Term Support Release: New Features in Qt 5.12 - Qt 5.15 Standard Support. Long Term Support Release New Features in Qt 5.15 - Qt 6.0 Development.

Qt5 alpha has been released. One of the features which I have been working on is a new syntax for signals and slot.This blog entry will present it.

Here is how you would connect a signal to a slot:

What really happens behind the scenes is that the SIGNAL and SLOT macros will convert their argument to a string. Then QObject::connect() will compare those strings with the introspection data collected by the moc tool.

What's the problem with this syntax?

While working fine in general, we can identify some issues:

  • No compile time check: All the checks are done at run-time by parsing the strings. That means if you do a typo in the name of the signal or the slot, it will compile but the connection will not be made, and you will only notice a warning in the standard output.
  • Since it operates on the strings, the type names of the slot must match exactly the ones of the signal. And they also need to be the same in the header and in the connect statement. This means it won't work nicely if you want to use typedef or namespaces

In the upcoming Qt5, an alternative syntax exist. The former syntax will still work. But you can now also use this new way of connecting your signals to your slots:

Which one is the more beautiful is a matter of taste. One can quickly get used to the new syntax.

So apart from the aesthetic point of view, let us go over some of the things that it brings us:

Compile-time checking

You will get a compiler error if you misspelled the signal or slot name, or if the arguments of your slot do not match those from the signal.
This might save you some time while you are doing some re-factoring and change the name or arguments of signals or slots.

An effort has been made, using static_assert to get nice compile errors if the arguments do not match or of you miss a Q_OBJECT

Arguments automatic type conversion

Not only you can now use typedef or namespaces properly, but you can also connect signalsto slots that take arguments of different types if an implicit conversion is possible

In the following example, we connect a signal that has a QString as a parameter to a slot that takes a QVariant. It works because QVariant has an implicit constructor that takes a QString

Connecting to any function

As you might have seen in the previous example, the slot was just declared as publicand not as slot. Qt will indeed call directly the function pointer of the slot, andwill not need moc introspection anymore. (It still needs it for the signal)

But what we can also do is connecting to any function or functor:

This can become very powerful when you associate that with boost or tr1::bind.

C++11 lambda expressions

Everything documented here works with the plain old C++98. But if you use compiler that supportsC++11, I really recommend you to use some of the language's new features.Lambda expressions are supportedby at least MSVC 2010, GCC 4.5, clang 3.1. For the last two, you need to pass -std=c++0x asa flag.

You can then write code like:

This allows you to write asynchronous code very easily.

Update: Also have a look what other C++11 features Qt5 offers.

It is time to try it out. Check out the alpha and start playing. Don't hesistate to report bugs.

Home All Classes Main Classes Annotated Grouped Classes Functions

The QObject class is the base class of all Qt objects.More...

All the functions in this class are reentrant when Qt is built with thread support.

Qt Signal Slot Reference

#include <qobject.h>

Inherits Qt.

Inherited by QAccel, QAccessibleObject, QAction, QApplication, QAssistantClient, QDataPump, QAxObject, QAxScript, QAxScriptManager, QWidget, QCanvas, QStyle, QClipboard, QCopChannel, QDns, QLayout, QDragObject, QEditorFactory, QEventLoop, QFileIconProvider, QNetworkProtocol, QWSKeyboardHandler, QNetworkOperation, QNPInstance, QObjectCleanupHandler, QProcess, QServerSocket, QSessionManager, QSignal, QSignalMapper, QSocket, QSocketNotifier, QSound, QSqlDatabase, QSqlDriver, QSqlForm, QStyleSheet, QTimer, QToolTipGroup, QTranslator, QUrlOperator, and QValidator.

Public Members

  • QObject ( QObject * parent = 0, const char * name = 0 )
  • virtual ~QObject ()
  • virtual const char * className () const
  • virtual QMetaObject * metaObject () const
  • virtual bool event ( QEvent * e )
  • virtual bool eventFilter ( QObject * watched, QEvent * e )
  • bool isA ( const char * clname ) const
  • bool inherits ( const char * clname ) const
  • const char * name () const
  • const char * name ( const char * defaultName ) const
  • virtual void setName ( const char * name )
  • bool isWidgetType () const
  • bool highPriority () const
  • bool signalsBlocked () const
  • void blockSignals ( bool block )
  • int startTimer ( int interval )
  • void killTimer ( int id )
  • void killTimers ()
  • QObject * child ( const char * objName, const char * inheritsClass = 0, bool recursiveSearch = TRUE )
  • const QObjectList * children () const
  • QObjectList * queryList ( const char * inheritsClass = 0, const char * objName = 0, bool regexpMatch = TRUE, bool recursiveSearch = TRUE ) const
  • virtual void insertChild ( QObject * obj )
  • virtual void removeChild ( QObject * obj )
  • void installEventFilter ( const QObject * filterObj )
  • void removeEventFilter ( const QObject * obj )
  • bool connect ( const QObject * sender, const char * signal, const char * member ) const
  • bool disconnect ( const char * signal = 0, const QObject * receiver = 0, const char * member = 0 )
  • bool disconnect ( const QObject * receiver, const char * member = 0 )
  • void dumpObjectTree ()
  • void dumpObjectInfo ()
  • virtual bool setProperty ( const char * name, const QVariant & value )
  • virtual QVariant property ( const char * name ) const
  • QObject * parent () const

Public Slots

  • void deleteLater ()

Signals

  • void destroyed ()
  • void destroyed ( QObject * obj )

Static Public Members

  • QString tr ( const char * sourceText, const char * comment )
  • QString trUtf8 ( const char * sourceText, const char * comment )
  • const QObjectList * objectTrees ()
  • bool connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member )
  • bool disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member )

Properties

  • QCString name - the name of this object

Protected Members

  • const QObject * sender ()
  • virtual void timerEvent ( QTimerEvent * )
  • virtual void childEvent ( QChildEvent * )
  • virtual void customEvent ( QCustomEvent * )
  • virtual void connectNotify ( const char * signal )
  • virtual void disconnectNotify ( const char * signal )
  • virtual bool checkConnectArgs ( const char * signal, const QObject * receiver, const char * member )

Static Protected Members

  • QCString normalizeSignalSlot ( const char * signalSlot )
Slot

Related Functions

  • void * qt_find_obj_child ( QObject * parent, const char * type, const char * name )

Detailed Description

The QObject class is the base class of all Qt objects.

QObject is the heart of the Qt object model. The central feature in this model is a very powerfulmechanism for seamless object communication called signals and slots. You canconnect a signal to a slot with connect() and destroy theconnection with disconnect(). To avoid never ending notificationloops you can temporarily block signals with blockSignals(). Theprotected functions connectNotify() and disconnectNotify() make itpossible to track connections.

QObjects organize themselves in object trees. When you create aQObject with another object as parent, the object willautomatically do an insertChild() on the parent and thus show upin the parent's children() list. The parent takes ownership of theobject i.e. it will automatically delete its children in itsdestructor. You can look for an object by name and optionally typeusing child() or queryList(), and get the list of tree roots usingobjectTrees().

Every object has an object name() and can report its className()and whether it inherits() another class in the QObject inheritancehierarchy.

When an object is deleted, it emits a destroyed() signal. You cancatch this signal to avoid dangling references to QObjects. TheQGuardedPtr class provides an elegant way to use this feature.

QObjects can receive events through event() and filter the eventsof other objects. See installEventFilter() and eventFilter() fordetails. A convenience handler, childEvent(), can be reimplementedto catch child events.

Last but not least, QObject provides the basic timer support inQt; see QTimer for high-level support for timers.

Notice that the Q_OBJECT macro is mandatory for any object thatimplements signals, slots or properties. You also need to run themoc program (Meta Object Compiler) on thesource file. We strongly recommend the use of this macro in allsubclasses of QObject regardless of whether or not they actuallyuse signals, slots and properties, since failure to do so may leadcertain functions to exhibit undefined behaviour.

All Qt widgets inherit QObject. The convenience functionisWidgetType() returns whether an object is actually a widget. Itis much faster than inherits( 'QWidget' ).

Some QObject functions, e.g. children(), objectTrees() andqueryList() return a QObjectList. A QObjectList is a QPtrList ofQObjects. QObjectLists support the same operations as QPtrListsand have an iterator class, QObjectListIt.

See also Object Model.

Member Function Documentation

QObject::QObject ( QObject * parent = 0, const char * name = 0 )

Constructs an object called name with parent object, parent.

The parent of an object may be viewed as the object's owner. Forinstance, a dialog box is the parent of the'OK' and 'Cancel' buttons it contains.

The destructor of a parent object destroys all child objects.

Setting parent to 0 constructs an object with no parent. If theobject is a widget, it will become a top-level window.

The object name is some text that can be used to identify aQObject. It's particularly useful in conjunction with Qt Designer. You can find anobject by name (and type) using child(). To find several objectsuse queryList().

See also parent(), name, child(), and queryList().

QObject::~QObject () [virtual]

Destroys the object, deleting all its child objects.

All signals to and from the object are automatically disconnected.

Warning: All child objects are deleted. If any of these objectsare on the stack or global, sooner or later your program willcrash. We do not recommend holding pointers to child objects fromoutside the parent. If you still do, the QObject::destroyed()signal gives you an opportunity to detect when an object isdestroyed.

Warning: Deleting a QObject while pending events are waiting to bedelivered can cause a crash. You must not delete the QObjectdirectly from a thread that is not the GUI thread. Use theQObject::deleteLater() method instead, which will cause the eventloop to delete the object after all pending events have beendelivered to the object.

void QObject::blockSignals ( bool block )

Blocks signals if block is TRUE, or unblocks signals if block

Qt Signal Slot Example

is FALSE.

Emitted signals disappear into hyperspace if signals are blocked.Note that the destroyed() signals will be emitted even if the signalsfor this object have been blocked.

Examples: rot13/rot13.cpp and simple/main.cpp.

bool QObject::checkConnectArgs ( const char * signal, const QObject * receiver, const char * member ) [virtual protected]

Returns TRUE if the signal and the member arguments arecompatible; otherwise returns FALSE. (The receiver argument iscurrently ignored.)

Warning: We recommend that you use the default implementation anddo not reimplement this function.

QObject * QObject::child ( const char * objName, const char * inheritsClass = 0, bool recursiveSearch = TRUE )

Searches the children and optionally grandchildren of this object,and returns a child that is called objName that inherits inheritsClass. If inheritsClass is 0 (the default), any classmatches.

If recursiveSearch is TRUE (the default), child() performs adepth-first search of the object's children.

If there is no such object, this function returns 0. If there aremore than one, the first one found is retured; if you need all ofthem, use queryList().

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

This event handler can be reimplemented in a subclass to receivechild events.

Child events are sent to objects when children are inserted orremoved.

Note that events with QEvent::type() QEvent::ChildInserted areposted (with QApplication::postEvent()) to make sure that thechild's construction is completed before this function is called.

If a child is removed immediately after it is inserted, the ChildInserted event may be suppressed, but the ChildRemovedevent will always be sent. In such cases it is possible that therewill be a ChildRemoved event without a corresponding ChildInserted event.

If you change state based on ChildInserted events, callQWidget::constPolish(), or do in functions that depend on the state. One notable example isQWidget::sizeHint().

See also event() and QChildEvent.

Reimplemented in QMainWindow and QSplitter.

const QObjectList * QObject::children () const

Returns a list of child objects, or 0 if this object has nochildren.

The QObjectList class is defined in the qobjectlist.h headerfile.

The first child added is the firstobject in the list and the last child added is the last object in the list, i.e. newchildren 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 objectin the list, and a widget that is lowered becomes the first objectin the list.

See also child(), queryList(), parent(), insertChild(), and removeChild().

const char * QObject::className () const [virtual]

Returns the class name of this object.

This function is generated by the Meta Object Compiler.

Warning: This function will return the wrong name if the classdefinition lacks the Q_OBJECT macro.

See also name, inherits(), isA(), and isWidgetType().

Example: sql/overview/custom1/main.cpp.

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member ) [static]

Connects signal from the sender object to member in objectreceiver, and returns TRUE if the connection succeeds; otherwisereturns FALSE.

You must use the SIGNAL() and SLOT() macros when specifying the signaland the member, for example:

This example ensures that the label always displays the currentscroll bar value. Note that the signal and slots parameters must notcontain any variable names, only the type. E.g. the following wouldnot work and return FALSE:QObject::connect( scroll, SIGNAL(valueChanged(int v)),label, SLOT(setNum(int v)) );

A signal can also be connected to another signal:

In this example, the MyWidget constructor relays a signal from aprivate member variable, and makes it available under a name thatrelates to MyWidget.

A signal can be connected to many slots and signals. Many signalscan be connected to one slot.

If a signal is connected to several slots, the slots are activatedin an arbitrary order when the signal is emitted.

The function returns TRUE if it successfully connects the signalto the slot. It will return FALSE if it cannot create theconnection, for example, if QObject is unable to verify theexistence of either signal or member, or if their signaturesaren't compatible.

A signal is emitted for every connection you make, so if youduplicate a connection, two signals will be emitted. You canalways break a connection using disconnect().

See also disconnect().

Examples: action/main.cpp, application/main.cpp, extension/main.cpp, iconview/main.cpp, network/archivesearch/main.cpp, regexptester/main.cpp, and t2/main.cpp.

bool QObject::connect ( const QObject * sender, const char * signal, const char * member ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Connects signal from the sender object to this object's member.

Equivalent to: QObject::connect(sender, signal, this, member).

See also disconnect().

void QObject::connectNotify ( const char * signal ) [virtual protected]

This virtual function is called when something has been connectedto signal in this object.

Warning: This function violates the object-oriented principle ofmodularity. However, it might be useful when you need to performexpensive initialization only if something is connected to asignal.

See also connect() and disconnectNotify().

void QObject::customEvent ( QCustomEvent * ) [virtual protected]

This event handler can be reimplemented in a subclass to receivecustom events. Custom events are user-defined events with a typevalue at least as large as the 'User' item of the QEvent::Typeenum, and is typically a QCustomEvent or QCustomEvent subclass.

See also event() and QCustomEvent.

void QObject::deleteLater () [slot]

Performs a deferred deletion of this object.

Instead of an immediate deletion this function schedules adeferred delete event for processing when Qt returns to the mainevent loop.

Example: table/bigtable/main.cpp.

void QObject::destroyed () [signal]

This signal is emitted when the object is being destroyed.

Note that the signal is emitted by the QObject destructor, sothe object's virtual table is already degenerated at this point,and it is not safe to call any functions on the object emittingthe signal. This signal can not be blocked.

All the objects's children are destroyed immediately after thissignal is emitted.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This signal is emitted immediately before the object obj isdestroyed, and can not be blocked.

All the objects's children are destroyed immediately after thissignal is emitted.

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

Disconnects signal in object sender from member in objectreceiver.

A signal-slot connection is removed when either of the objectsinvolved are destroyed.

disconnect() is typically used in three ways, as the followingexamples demonstrate.

  1. Disconnect everything connected to an object's signals: equivalent to the non-static overloaded function
  2. Disconnect everything connected to a specific signal: equivalent to the non-static overloaded function
  3. Disconnect a specific receiver: equivalent to the non-static overloaded function

0 may be used as a wildcard, meaning 'any signal', 'any receivingobject', or 'any slot in the receiving object', respectively.

The sender may never be 0. (You cannot disconnect signals frommore than one object in a single call.)

If signal is 0, it disconnects receiver and member fromany signal. If not, only the specified signal is disconnected.

If receiver is 0, it disconnects anything connected to signal. If not, slots in objects other than receiver are notdisconnected.

If member is 0, it disconnects anything that is connected to receiver. If not, only slots named member will be disconnected,and all other slots are left alone. The member must be 0 if receiver is left out, so you cannot disconnect aspecifically-named slot on all objects.

See also connect().

bool QObject::disconnect ( const char * signal = 0, const QObject * receiver = 0, const char * member = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Disconnects signal from member of receiver.

A signal-slot connection is removed when either of the objectsinvolved are destroyed.

bool QObject::disconnect ( const QObject * receiver, const char * member = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Disconnects all signals in this object from receiver's member.

A signal-slot connection is removed when either of the objectsinvolved are destroyed.

void QObject::disconnectNotify ( const char * signal ) [virtual protected]

This virtual function is called when something has beendisconnected from signal in this object.

Warning: This function violates the object-oriented principle ofmodularity. However, it might be useful for optimizing access toexpensive resources.

See also disconnect() and connectNotify().

void QObject::dumpObjectInfo ()

Dumps information about signal connections, etc. for this objectto the debug output.

This function is useful for debugging, but does nothing if thelibrary has been compiled in release mode (i.e. without debugginginformation).

void QObject::dumpObjectTree ()

Dumps a tree of children to the debug output.

This function is useful for debugging, but does nothing if thelibrary has been compiled in release mode (i.e. without debugginginformation).

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

This virtual function receives events to an object and shouldreturn TRUE if the event e was recognized and processed.

The event() function can be reimplemented to customize thebehavior of an object.

See also installEventFilter(), timerEvent(), QApplication::sendEvent(), QApplication::postEvent(), and QWidget::event().

Reimplemented in QWidget.

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

Filters events if this object has been installed as an eventfilter for the watched object.

In your reimplementation of this function, if you want to filterthe event e, out, i.e. stop it being handled further, returnTRUE; otherwise return FALSE.

Example:

Notice in the example above that unhandled events are passed tothe base class's eventFilter() function, since the base classmight have reimplemented eventFilter() for its own internalpurposes.

Warning: If you delete the receiver object in this function, besure to return TRUE. Otherwise, Qt will forward the event to thedeleted object and the program might crash.

See also installEventFilter().

Reimplemented in QAccel, QScrollView, and QSpinBox.

bool QObject::highPriority () const

Returns TRUE if the object is a high-priority object, or FALSE ifit is a standard-priority object.

High-priority objects are placed first in QObject's list ofchildren on the assumption that they will be referenced veryoften.

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

Returns TRUE if this object is an instance of a class thatinherits clname, and clname inherits QObject; otherwisereturns FALSE.

A class is considered to inherit itself.

Example:

(QRangeControl is not a QObject.)

See also isA() and metaObject().

Examples: table/statistics/statistics.cpp, themes/metal.cpp, and themes/wood.cpp.

void QObject::insertChild ( QObject * obj ) [virtual]

Inserts an object obj into the list of child objects.

Warning: This function cannot be used to make one widget the childwidget of another widget. Child widgets can only be created bysetting the parent widget in the constructor or by callingQWidget::reparent().

See also removeChild() and QWidget::reparent().

void QObject::installEventFilter ( const QObject * filterObj )

Installs an event filter filterObj on this object. For example:

An event filter is an object that receives all events that aresent to this object. The filter can either stop the event orforward it to this object. The event filter filterObj receivesevents via its eventFilter() function. The eventFilter() functionmust return TRUE if the event should be filtered, (i.e. stopped);otherwise it must return FALSE.

If multiple event filters are installed on a single object, thefilter that was installed last is activated first.

Here's a KeyPressEater class that eats the key presses of itsmonitored objects:

And here's how to install it on two widgets:

The QAccel class, for example, uses this technique to interceptaccelerator key presses.

Warning: If you delete the receiver object in your eventFilter()function, be sure to return TRUE. If you return FALSE, Qt sendsthe event to the deleted object and the program will crash.

See also removeEventFilter(), eventFilter(), and event().

bool QObject::isA ( const char * clname ) const

Returns TRUE if this object is an instance of the class clname;otherwise returns FALSE.

Example:

See also inherits() and metaObject().

bool QObject::isWidgetType () const

Returns TRUE if the object is a widget; otherwise returns FALSE.

Calling this function is equivalent to callinginherits('QWidget'), except that it is much faster.

void QObject::killTimer ( int id )

Kills the timer with timer identifier, id.

The timer identifier is returned by startTimer() when a timerevent is started.

See also timerEvent(), startTimer(), and killTimers().

void QObject::killTimers ()

Kills all timers that this object has started.

Warning: Using this function can cause hard-to-find bugs: it killstimers started by sub- and superclasses as well as those startedby you, which is often not what you want. We recommend using aQTimer or perhaps killTimer().

See also timerEvent(), startTimer(), and killTimer().

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

Returns a pointer to the meta object of this object.

A meta object contains information about a class that inheritsQObject, e.g. class name, superclass name, properties, signals andslots. Every class that contains the Q_OBJECT macro will also havea meta object.

The meta object information is required by the signal/slotconnection mechanism and the property system. The functions isA()and inherits() also make use of the meta object.

const char * QObject::name () const

Returns the name of this object.See the 'name' property for details.

const char * QObject::name ( const char * defaultName ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the name of this object, or defaultName if the objectdoes not have a name.

QCStringQObject::normalizeSignalSlot ( const char * signalSlot ) [static protected]

Normlizes the signal or slot definition signalSlot by removingunnecessary whitespace.

const QObjectList * QObject::objectTrees () [static]

Returns a pointer to the list of all object trees (their rootobjects), or 0 if there are no objects.

The QObjectList class is defined in the qobjectlist.h headerfile.

The most recent root object created is the first object in the list and the first root object addedis the last object in the list.

See also children(), parent(), insertChild(), and removeChild().

QObject * QObject::parent () const

Returns a pointer to the parent object.

See also children().

QVariantQObject::property ( const char * name ) const [virtual]

Returns the value of the object's name property.

If no such property exists, the returned variant is invalid.

Information about all available properties are provided throughthe metaObject().

See also setProperty(), QVariant::isValid(), metaObject(), QMetaObject::propertyNames(), and QMetaObject::property().

Example: qutlook/centralwidget.cpp.

QObjectList * QObject::queryList ( const char * inheritsClass = 0, const char * objName = 0, bool regexpMatch = TRUE, bool recursiveSearch = TRUE ) const

Searches the children and optionally grandchildren of this object,and returns a list of those objects that are named or that matchobjName and inherit inheritsClass. If inheritsClass is 0(the default), all classes match. If objName is 0 (thedefault), all object names match.

If regexpMatch is TRUE (the default), objName is a regular expression that the objects's names must match. The syntax is thatof a QRegExp. If regexpMatch is FALSE, objName is a stringand object names must match it exactly.

Note that inheritsClass uses single inheritance from QObject,the way inherits() does. According to inherits(), QMenuBarinherits QWidget but not QMenuData. This does not quite matchreality, but is the best that can be done on the wide variety ofcompilers Qt supports.

Finally, if recursiveSearch is TRUE (the default), queryList()searches nth-generation as well as first-generation children.

If all this seems a bit complex for your needs, the simplerchild() function may be what you want.

This somewhat contrived example disables all the buttons in thiswindow:

The QObjectList class is defined in the qobjectlist.h headerfile.

Warning: Delete the list as soon you have finished using it. Thelist contains pointers that may become invalid at almost any timewithout notice (as soon as the user closes a window you may havedangling pointers, for example).

See also child(), children(), parent(), inherits(), name, and QRegExp.

void QObject::removeChild ( QObject * obj ) [virtual]

Removes the child object obj from the list of children.

Warning: This function will not remove a child widget from thescreen. It will only remove it from the parent widget's list ofchildren.

See also insertChild() and QWidget::reparent().

void QObject::removeEventFilter ( const QObject * obj )

Removes an event filter object obj from this object. Therequest is ignored if such an event filter has not been installed.

All event filters for this object are automatically removed whenthis object is destroyed.

It is always safe to remove an event filter, even during eventfilter activation (i.e. from the eventFilter() function).

See also installEventFilter(), eventFilter(), and event().

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

Returns a pointer to the object that sent the signal, if called ina slot activated by a signal; otherwise it returns 0. The pointeris valid only during the execution of the slot that calls thisfunction.

The pointer returned by this function becomes invalid if thesender is destroyed, or if the slot is disconnected from thesender's signal.

Warning: This function violates the object-oriented principle ofmodularity. However, getting access to the sender might be usefulwhen many signals are connected to a single slot. The sender isundefined if the slot is called as a normal C++ function.

void QObject::setName ( const char * name ) [virtual]

Sets the object's name to name.

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

Sets the value of the object's name property to value.

Returns TRUE if the operation was successful; otherwise returnsFALSE.

Information about all available properties is provided through themetaObject().

See also property(), metaObject(), QMetaObject::propertyNames(), and QMetaObject::property().

Example: qutlook/centralwidget.cpp.

bool QObject::signalsBlocked () const

Returns TRUE if signals are blocked; otherwise returns FALSE.

Signals are not blocked by default.

See also blockSignals().

int QObject::startTimer ( int interval )

Starts a timer and returns a timer identifier, or returns zero ifit could not start a timer.

A timer event will occur every interval milliseconds untilkillTimer() or killTimers() is called. If interval is 0, thenthe timer event occurs once every time there are no more windowsystem events to process.

The virtual timerEvent() function is called with the QTimerEventevent parameter class when a timer event occurs. Reimplement thisfunction to get timer events.

If multiple timers are running, the QTimerEvent::timerId() can beused to find out which timer was activated.

Example:

Note that QTimer's accuracy depends on the underlying operatingsystem and hardware. Most platforms support an accuracy of 20 ms;some provide more. If Qt is unable to deliver the requestednumber of timer clicks, it will silently discard some.

The QTimer class provides a high-level programming interface withone-shot timers and timer signals instead of events.

See also timerEvent(), killTimer(), killTimers(), QEventLoop::awake(), and QEventLoop::aboutToBlock().

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

This event handler can be reimplemented in a subclass to receivetimer events for the object.

QTimer provides a higher-level interface to the timerfunctionality, and also more general information about timers.

See also startTimer(), killTimer(), killTimers(), and event().

Examples: biff/biff.cpp, dclock/dclock.cpp, forever/forever.cpp, grapher/grapher.cpp, qmag/qmag.cpp, and xform/xform.cpp.

QStringQObject::tr ( const char * sourceText, const char * comment ) [static]

Returns a translated version of sourceText, or sourceTextitself if there is no appropriate translated version. Thetranslation context is QObject with comment (0 by default).All QObject subclasses using the Q_OBJECT macro automatically havea reimplementation of this function with the subclass name ascontext.

Warning: This method is reentrant only if all translators areinstalled before calling this method. Installing or removingtranslators while performing translations is not supported. Doingso will probably result in crashes or other undesirable behavior.

See also trUtf8(), QApplication::translate(), and Internationalization with Qt.

Example: network/networkprotocol/view.cpp.

QStringQObject::trUtf8 ( const char * sourceText, const char * comment ) [static]

Returns a translated version of sourceText, orQString::fromUtf8(sourceText) if there is no appropriateversion. It is otherwise identical to tr(sourceText, comment).

Warning: This method is reentrant only if all translators areinstalled before calling this method. Installing or removingtranslators while performing translations is not supported. Doingso will probably result in crashes or other undesirable behavior.

See also tr() and QApplication::translate().

Property Documentation

QCStringname

This property holds the name of this object.

You can find an object by name (and type) using child(). You canfind a set of objects with queryList().

The object name is set by the constructor or by the setName()function. The object name is not very useful in the currentversion of Qt, but will become increasingly important in thefuture.

If the object does not have a name, the name() function returns'unnamed', so printf() (used in qDebug()) will not be asked tooutput a null pointer. If you want a null pointer to be returnedfor unnamed objects, you can call name( 0 ).

See also className(), child(), and queryList().

Set this property's value with setName() and get this property's value with name().

Related Functions

void * qt_find_obj_child ( QObject * parent, const char * type, const char * name )

Returns a pointer to the object named name that inherits type and with a given parent.

Returns 0 if there is no such child.

This file is part of the Qt toolkit.Copyright © 1995-2007Trolltech. All Rights Reserved.