免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5067 | 回复: 2

Qt 双击时不触发单击事件 [复制链接]

论坛徽章:
0
发表于 2011-02-14 14:35 |显示全部楼层
为了避免双击时触发单击事件,在单击处理函数clicked()中启动一timer,延时 qApp->doubleClickInterval(),而在此timer的timeout()中处理单击事件,在双击处理函数停止此 timer,一个完整的例子代码如下:(for Qt3,Qt4的也差不多,省略)
  1. #include <qapplication.h>
  2. #include <qtimer.h>
  3. #include <qlistbox.h>

  4. class Test :public QListBox {
  5.     Q_OBJECT

  6. public:
  7.     Test( QWidget* parent, const char* name = 0 )
  8.         : QListBox( parent, name ) {
  9.         connect( this, SIGNAL( clicked( QListBoxItem* ) ),
  10.                  this, SLOT( seeingSingleClick() ) );
  11.         connect( this, SIGNAL( doubleClicked( QListBoxItem* ) ),
  12.                  this, SLOT( handleDoubleClick() ) );

  13.         _timer = new QTimer( this );
  14.         connect( _timer, SIGNAL( timeout() ),
  15.                  this, SLOT( handleSingleClick() ) );

  16.         insertStringList( QStringList() << "Item 1" << "Item 2"
  17.                           << "Item 3" << "Item 4" );
  18.     }

  19. protected slots:
  20.     void seeingSingleClick() {
  21.         _timer->start( qApp->doubleClickInterval(), true );
  22.     }
  23.     void handleSingleClick() {
  24.         qDebug("This was a single click!");
  25.     }
  26.     void handleDoubleClick() {
  27.         qDebug("This was a double click!");
  28.         _timer->stop();
  29.     }
  30. private:
  31.     QTimer* _timer;
  32. };


  33. int main( int argc, char** argv ) {
  34.     QApplication app( argc, argv );

  35.     Test* test = new Test(0);
  36.     test->show();

  37.     QObject::connect( qApp, SIGNAL( lastWindowClosed() ),
  38.                       qApp, SLOT( quit() ) );
  39.     return app.exec();
  40. }

  41. #include "main.moc"
复制代码

论坛徽章:
0
发表于 2011-02-22 10:31 |显示全部楼层
GUI编程界面定制

论坛徽章:
0
发表于 2011-02-25 17:13 |显示全部楼层
这样会影响单击事件吧 ...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP