Qt模拟鼠标按键事件

QT平台模拟鼠标事件案例是本文要介绍的内容,主要是来了解QT平台上的模拟鼠标事件的应用,具体内容的实现来看本文详解。需要导入QTest4.lib 否则会有连接时错误

 
 
  1. #include<QtTest/QTest> 
  2. QTest::mouseClick(ui.mainPlayer,Qt::LeftButton,0,pos(),-1); 

QT平台模拟鼠标按键

和模拟键盘按键类似,也是通过发送相应的事件来实现的,安装相应的事件监听器,具体发送事件:

 
 
  1. QPoint pos;  
  2. pos.setX(88);  
  3. pos.setY(58);  
  4. QMouseEvent *mEvnPress;  
  5. QMouseEvent *mEvnRelease;  
  6. mEvnPress = new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton,Qt::LeftButton, Qt::NoModifier);  
  7. QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);  
  8. mEvnRelease = new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton,Qt::LeftButton, Qt::NoModifier);  
  9. QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);       

主要的分析函数是:    

 
 
  1. QMouseEvent::QMouseEvent ( Type type, const QPoint & position,Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiersmodifiers ) 

参数分析:

 
 
  1. The type parameter must be one of QEvent::MouseButtonPress,QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick, or QEvent::MouseMove.  
  2. The position is the mouse cursor's position relative to the receiving widget.   
  3. The button that caused the event is given as a value from the Qt::MouseButtonenum.   
  4.       If the event type is MouseMove, the appropriate button for this event isQt::NoButton.   
  5. The mouse and keyboard states at the time of the event are specified by buttonsand modifiers.  
  6. 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中的定位,以及具体的按键是什么。

 
 
  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.  
  6.        QPalette pal;  
  7.      pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::red);          
  8.        static_cast<QPushButton*>(target)->setPalette(pal);  
  9.    }  
  10.    elseif(event->type()==QEvent::FocusOut){//event->type()==QEvent::Leave ||  
  11.        static_cast<QPushButton*>(target)->setStyleSheet("");  
  12.        QPalette pal;  
  13.        pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::black);  
  14.        static_cast<QPushButton*>(target)->setPalette(pal);  
  15.    }  
  16.    else if(event->type()== QEvent::KeyPress){  
  17.        QPoint pos;  
  18.        QWidget *now_buttonQWidget::focusWidget();  
  19.        QKeyEvent *k = (QKeyEvent *)event;  
  20.        QLayoutItem *next_button;  
  21.        switch (k->key()){  
  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 = newQMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);  
  57.            QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);  
  58.             mEvnRelease = newQMouseEvent(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.             returnQWidget::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);  

小结:QT平台模拟鼠标事件案例的内容介绍完了,希望通过QT平台模拟鼠标内容的学习能对你有所帮助!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值