📄 qobject.3qt
字号:
private:.br QPushButton *aButton;.br };.br.br MyWidget::MyWidget().br {.br aButton = new QPushButton( this );.br connect( aButton, SIGNAL(clicked()), SIGNAL(myUsefulSignal()) );.br }.br.fi.PPIn this example, the MyWidget constructor relays a signal from a private member variable, and makes it available under a name that relates to MyWidget..PPA signal can be connected to many slots and signals. Many signals can be connected to one slot..PPIf a signal is connected to several slots, the slots are activated in an arbitrary order when the signal is emitted..PPThe function returns TRUE if it successfully connects the signal to the slot. It will return FALSE if it cannot create the connection, for example, if QObject is unable to verify the existence of either \fIsignal\fR or \fImember\fR, or if their signatures aren't compatible..PPSee also disconnect()..PPExamples:.)l action/main.cpp, addressbook/main.cpp, application/main.cpp, extension/main.cpp, iconview/main.cpp, mdi/main.cpp, and t2/main.cpp..SH "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..PPConnects \fIsignal\fR from the \fIsender\fR object to this object's \fImember\fR..PPEquivalent to: \fCQObject::connect(sender, signal, this, member)\fR..PPSee also disconnect()..SH "void QObject::connectNotify ( const char * signal )\fC [virtual protected]\fR"This virtual function is called when something has been connected to \fIsignal\fR in this object..PP\fBWarning:\fR 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..PPSee also connect() and disconnectNotify()..SH "void QObject::customEvent ( QCustomEvent * )\fC [virtual protected]\fR"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 "User" item of the QEvent::Type enum, and is typically a QCustomEvent or QCustomEvent subclass..PPSee also event() and QCustomEvent..SH "void QObject::deleteLater ()\fC [slot]\fR"Performs a deferred deletion of this object..PPInstead of an immediate deletion this function schedules a deferred delete event for processing when Qt returns to the main event loop..SH "void QObject::destroyed ()\fC [signal]\fR"This signal is emitted when the object is being destroyed..PPNote that the signal is emitted by the QObject destructor, so the object's virtual table is already degenerated at this point, and it is not safe to call any functions on the object emitting the signal..PPAll the objects's children are destroyed immediately after this signal is emitted..SH "void QObject::destroyed ( QObject * obj )\fC [signal]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThis signal is emitted immediately before the object \fIobj\fR is destroyed..PPAll the objects's children are destroyed immediately after this signal is emitted..SH "bool QObject::disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member )\fC [static]\fR"Disconnects \fIsignal\fR in object \fIsender\fR from \fImember\fR in object \fIreceiver\fR..PPA signal-slot connection is removed when either of the objects involved are destroyed..PPdisconnect() is typically used in three ways, as the following examples demonstrate. <ol type=1>.IP 1Disconnect everything connected to an object's signals:.IP.nf.br disconnect( myObject, 0, 0, 0 );.br.fiequivalent to the non-static overloaded function.IP.nf.br myObject->disconnect();.br.fi.IP 2Disconnect everything connected to a specific signal:.IP.nf.br disconnect( myObject, SIGNAL(mySignal()), 0, 0 );.br.fiequivalent to the non-static overloaded function.IP.nf.br myObject->disconnect( SIGNAL(mySignal()) );.br.fi.IP 3Disconnect a specific receiver:.IP.nf.br disconnect( myObject, 0, myReceiver, 0 );.br.fiequivalent to the non-static overloaded function.IP.nf.br myObject->disconnect( myReceiver );.br.fi.PP0 may be used as a wildcard, meaning "any signal", "any receiving object", or "any slot in the receiving object", respectively..PPThe \fIsender\fR may never be 0. (You cannot disconnect signals from more than one object in a single call.).PPIf \fIsignal\fR is 0, it disconnects \fIreceiver\fR and \fImember\fR from any signal. If not, only the specified signal is disconnected..PPIf \fIreceiver\fR is 0, it disconnects anything connected to \fIsignal\fR. If not, slots in objects other than \fIreceiver\fR are not disconnected..PPIf \fImember\fR is 0, it disconnects anything that is connected to \fIreceiver\fR. If not, only slots named \fImember\fR will be disconnected, and all other slots are left alone. The \fImember\fR must be 0 if \fIreceiver\fR is left out, so you cannot disconnect a specifically-named slot on all objects..PPSee also connect()..SH "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..PPDisconnects \fIsignal\fR from \fImember\fR of \fIreceiver\fR..PPA signal-slot connection is removed when either of the objects involved are destroyed..SH "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..PPDisconnects all signals in this object from \fIreceiver\fR's \fImember\fR..PPA signal-slot connection is removed when either of the objects involved are destroyed..SH "void QObject::disconnectNotify ( const char * signal )\fC [virtual protected]\fR"This virtual function is called when something has been disconnected from \fIsignal\fR in this object..PP\fBWarning:\fR This function violates the object-oriented principle of modularity. However, it might be useful for optimizing access to expensive resources..PPSee also disconnect() and connectNotify()..SH "void QObject::dumpObjectInfo ()"Dumps information about signal connections, etc. for this object to the debug output..PPThis function is useful for debugging, but does nothing if the library has been compiled in release mode (i.e. without debugging information)..SH "void QObject::dumpObjectTree ()"Dumps a tree of children to the debug output..PPThis function is useful for debugging, but does nothing if the library has been compiled in release mode (i.e. without debugging information)..SH "bool QObject::event ( QEvent * e )\fC [virtual]\fR"This virtual function receives events to an object and should return TRUE if the event \fIe\fR was recognized and processed..PPThe event() function can be reimplemented to customize the behavior of an object..PPSee also installEventFilter(), timerEvent(), QApplication::sendEvent(), QApplication::postEvent(), and QWidget::event()..PPReimplemented in QWidget..SH "bool QObject::eventFilter ( QObject * watched, QEvent * e )\fC [virtual]\fR"Filters events if this object has been installed as an event filter for the \fIwatched\fR object..PPIn your reimplementation of this function, if you want to filter the event \fIe\fR, out, i.e. stop it being handled further, return TRUE; otherwise return FALSE..PPExample:.PP.nf.br class MyMainWindow : public QMainWindow.br {.br public:.br MyMainWindow( QWidget *parent = 0, const char *name = 0 );.br.br protected:.br bool eventFilter( QObject *obj, QEvent *ev );.br.br private:.br QTextEdit *textEdit;.br };.br.br MyMainWindow::MyMainWindow( QWidget *parent, const char *name ).br : QMainWindow( parent, name ).br {.br textEdit = new QTextEdit( this );.br setCentralWidget( textEdit );.br textEdit->installEventFilter( this );.br }.br.br bool MyMainWindow::eventFilter( QObject *obj, QEvent *ev ).br {.br if ( obj == textEdit ) {.br if ( e->type() == QEvent::KeyPress ) {.br qDebug( "Ate key press %d", k->key() );.br return TRUE;.br } else {.br return FALSE;.br }.br } else {.br // pass the event on to the parent class.br return QMainWindow::eventFilter( obj, ev );.br }.br }.br.fi.PPNotice 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..PP\fBWarning:\fR 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..PPSee also installEventFilter()..PPReimplemented in QAccel, QScrollView, and QSpinBox..SH "bool QObject::highPriority () const"Returns TRUE if the object is a high-priority object, or FALSE if it is a standard-priority object..PPHigh-priority objects are placed first in QObject's list of children on the assumption that they will be referenced very often..SH "bool QObject::inherits ( const char * clname ) const"Returns TRUE if this object is an instance of a class that inherits \fIclname\fR, and \fIclname\fR inherits QObject; otherwise returns FALSE..PPA class is considered to inherit itself..PPExample:.PP.nf.br QTimer *t = new QTimer; // QTimer inherits QObject.br t->inherits( "QTimer" ); // returns TRUE.br t->inherits( "QObject" ); // returns TRUE.br t->inherits( "QButton" ); // returns FALSE.br.br // QScrollBar inherits QWidget and QRangeControl.br QScrollBar *s = new QScrollBar( 0 );.br s->inherits( "QWidget" ); // returns TRUE.br s->inherits( "QRangeControl" ); // returns FALSE.br.fi.PP(QRangeControl is not a QObject.).PPSee also isA() and metaObject()..PPExamples:.)l table/statistics/statistics.cpp, themes/metal.cpp, and themes/wood.cpp..SH "void QObject::insertChild ( QObject * obj )\fC [virtual]\fR"Inserts an object \fIobj\fR into the list of child objects..PP\fBWarning:\fR This function cannot be used to make one widget the child widget of another widget. Child widgets can only be created by setting the parent widget in the constructor or by calling QWidget::reparent()..PPSee also removeChild() and QWidget::reparent()..SH "void QObject::installEventFilter ( const QObject * filterObj )"Installs an event filter \fIfilterObj\fR on this object. For example:.PP.nf.br monitoredObj->installEventFilter( filterObj );.br.fi.PPAn 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 \fIfilterObj\fR 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..PPIf multiple event filters are installed on a single object, the filter that was installed last is activated first..PPHere's a \fCKeyPressEater\fR class that eats the key presses of its monitored objects:.PP.nf.br class KeyPressEater : public QObject.br {.br ....br protected:.br bool eventFilter( QObject *o, QEvent *e );.br };.br.br bool KeyPressEater::eventFilter( QObject *o, QEvent *e ).br {.br if ( e->type() == QEvent::KeyPress ) {.br // special processing for key press.br QKeyEvent *k = (QKeyEvent *)e;.br qDebug( "Ate key press %d", k->key() );.br return TRUE; // eat event
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -