QT平台上模拟鼠标事件案例

移动开发
QT平台上模拟鼠标事件案例是本文要介绍的内容,主要是来了解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平台模拟鼠标内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-29 11:25:29

QTWebKit鼠标

2012-06-12 09:43:34

微软Linux服务

2010-06-09 17:46:53

2009-01-16 09:10:39

JavaCRM系统企业应用

2013-05-14 11:08:23

AIR Android触摸事件鼠标事件

2009-12-30 10:44:38

Silverlight

2011-06-23 14:05:32

Qt 事件机制

2011-06-22 10:20:11

QT 鼠标 拖放

2017-11-06 08:52:59

Linux终端模拟器Java 9

2011-07-28 15:07:23

iOS猜数游戏

2017-03-20 17:20:35

iOSTensorFlow

2013-08-27 10:31:05

Headless模式Java SE设计模式

2019-11-26 14:52:40

Linux工具写作者

2017-02-23 13:51:05

2016-12-28 09:30:37

Andriod安卓平台依赖注入

2010-01-04 14:06:35

Silverlight

2009-09-02 18:11:24

C#鼠标

2012-04-25 14:27:03

JavaScala

2012-08-22 11:12:11

Ubuntu

2010-05-19 16:53:31

MySQL代码
点赞
收藏

51CTO技术栈公众号