- 论坛徽章:
- 0
|
为了避免双击时触发单击事件,在单击处理函数clicked()中启动一timer,延时 qApp->doubleClickInterval(),而在此timer的timeout()中处理单击事件,在双击处理函数停止此 timer,一个完整的例子代码如下:(for Qt3,Qt4的也差不多,省略)- #include <qapplication.h>
- #include <qtimer.h>
- #include <qlistbox.h>
- class Test :public QListBox {
- Q_OBJECT
- public:
- Test( QWidget* parent, const char* name = 0 )
- : QListBox( parent, name ) {
- connect( this, SIGNAL( clicked( QListBoxItem* ) ),
- this, SLOT( seeingSingleClick() ) );
- connect( this, SIGNAL( doubleClicked( QListBoxItem* ) ),
- this, SLOT( handleDoubleClick() ) );
- _timer = new QTimer( this );
- connect( _timer, SIGNAL( timeout() ),
- this, SLOT( handleSingleClick() ) );
- insertStringList( QStringList() << "Item 1" << "Item 2"
- << "Item 3" << "Item 4" );
- }
- protected slots:
- void seeingSingleClick() {
- _timer->start( qApp->doubleClickInterval(), true );
- }
- void handleSingleClick() {
- qDebug("This was a single click!");
- }
- void handleDoubleClick() {
- qDebug("This was a double click!");
- _timer->stop();
- }
- private:
- QTimer* _timer;
- };
- int main( int argc, char** argv ) {
- QApplication app( argc, argv );
- Test* test = new Test(0);
- test->show();
- QObject::connect( qApp, SIGNAL( lastWindowClosed() ),
- qApp, SLOT( quit() ) );
- return app.exec();
- }
- #include "main.moc"
复制代码 |
|