QT 模拟鼠标事件

模拟鼠标按键

和模拟键盘按键类似,也是通过发送相应的事件来实现的,安装相应的事件监听器,具体发送事件:
QPoint pos;
pos.setX(0);
pos.setY(0);
QMouseEvent *mEvnPress;
QMouseEvent *mEvnRelease;
mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);
mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);      


主要的分析函数是:     
QMouseEvent::QMouseEvent ( Type type, const QPoint & position, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
参数分析:
The type parameter must be one of QEvent::MouseButtonPress, QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick, or QEvent::MouseMove.
The position is the mouse cursor's position relative to the receiving widget. 
The button that caused the event is given as a value from the Qt::MouseButton enum. If the event type is MouseMove, the appropriate button for this event is Qt::NoButton. 
The mouse and keyboard states at the time of the event are specified by buttons and modifiers.
The globalPos() is initialized to QCursor::pos(), which may not be appropriate. Use the other constructor to specify the global position explicitly.
主要说明:
这里的pos的位置是接受鼠标事件的widget的内部的一个局部位置。也就是说他的鼠标按键的产生点是:先通过
QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);//也就是说先是指明了传递给哪个widget
然后再根据mEventPress来进行具体的再改widget中的定位,以及具体的按键是什么。

[cpp]  view plain copy
  1. bool MainWidget::eventFilter(QObject *target, QEvent *event)  
  2. {  
  3.     if(event->type()==QEvent::FocusIn){//event->type()==QEvent::Enter ||  
  4.         static_cast<QPushButton*>(target)->setStyleSheet("background-color: rgb(129, 129,129)");  
  5.         QPalette pal;  
  6.         pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::red);          
  7.         static_cast<QPushButton*>(target)->setPalette(pal);  
  8.     }  
  9.     else if(event->type()==QEvent::FocusOut){//event->type()==QEvent::Leave ||  
  10.         static_cast<QPushButton*>(target)->setStyleSheet("");  
  11.         QPalette pal;  
  12.         pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::black);  
  13.         static_cast<QPushButton*>(target)->setPalette(pal);  
  14.     }  
  15.     else if(event->type()== QEvent::KeyPress){  
  16.         QPoint pos;  
  17.         QWidget *now_button= QWidget::focusWidget();  
  18.         QKeyEvent *k = (QKeyEvent *)event;  
  19.         QLayoutItem *next_button;  
  20.         switch (k->key()){  
  21.   
  22.         case Qt::Key_Up:  
  23.             next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+8)%12);  
  24.             next_button->widget()->setFocus();  
  25.             break;  
  26.         case Qt::Key_Down:  
  27.             next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+4)%12);  
  28.             next_button->widget()->setFocus();  
  29. #ifdef COURSE  
  30.             QCursor::setPos(pos);  
  31. #endif  
  32.             break;  
  33.         case Qt::Key_Left:  
  34.             next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)-1+12)%12);  
  35.             next_button->widget()->setFocus();  
  36. #ifdef COURSE  
  37.             QCursor::setPos(pos);  
  38. #endif  
  39.             break;  
  40.         case Qt::Key_Right:  
  41.             next_button= ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+1)%12);  
  42.             next_button->widget()->setFocus();  
  43.             break;  
  44.         case Qt::Key_Period:  
  45.             pos = now_button->pos();  
  46.             pos.setX( 20 + pos.x()+(now_button->width())/2 );  
  47.             pos.setY( 20 + pos.y()+(now_button->height())/2 );  
  48.             //printf("/n***%d1 %d***/n",pos.x(),pos.y());  
  49. #ifdef COURSE  
  50.             QCursor::setPos(pos);  
  51. #endif  
  52.             pos.setX(88);  
  53.             pos.setY(58);  
  54.             QMouseEvent *mEvnPress;  
  55.             QMouseEvent *mEvnRelease;  
  56.             mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);  
  57.             QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);  
  58.             mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);  
  59.             QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);  
  60.             next_button = ui->gridLayout->itemAt(ui->gridLayout->indexOf(QWidget::focusWidget()));  
  61.             break;  
  62.         default:  
  63.             return QWidget::eventFilter(target,event);  
  64.         }  
  65.   
  66.         if(next_button){  
  67.             pos.setX( 20 + next_button->geometry().x() + (next_button->geometry().width()) / 2 );  
  68.             pos.setY( 20 + next_button->geometry().y() + (next_button->geometry().height()) / 2);  
  69. #ifdef COURSE  
  70.             QCursor::setPos(pos);  
  71. #endif  
  72.         }  
  73.         return true;  
  74.     }  
  75.     return QWidget::eventFilter(target,event);  
  76. }  

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值