QT平台上模拟鼠标事件案例
来源:互联网 发布:简单ssm框架源码下载 编辑:程序博客网 时间:2024/05/16 08:53
本文来自:
http://mobile.51cto.com/symbian-290064.htm
QT平台上模拟鼠标事件案例是本文要介绍的内容,主要是来了解QT平台上的模拟鼠标事件的应用,具体内容的实现来看本文详解。需要导入QTest4.lib 否则会有连接时错误
- #include<QtTest/QTest>
- QTest::mouseClick(ui.mainPlayer,Qt::LeftButton,0,pos(),-1);
QT平台模拟鼠标按键
和模拟键盘按键类似,也是通过发送相应的事件来实现的,安装相应的事件监听器,具体发送事件:
- QPoint pos;
- pos.setX(88);
- pos.setY(58);
- 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::KeyboardModifiersmodifiers )
参数分析:
- 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::MouseButtonenum.
- If the event type is MouseMove, the appropriate button for this event isQt::NoButton.
- The mouse and keyboard states at the time of the event are specified by buttonsand 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中的定位,以及具体的按键是什么。
- bool MainWidget::eventFilter(QObject*target, QEvent *event)
- {
- if(event->type()==QEvent::FocusIn){//event->type()==QEvent::Enter||
- static_cast<QPushButton*>(target)->setStyleSheet("background-color:rgb(129, 129,129)");
- QPalette pal;
- pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::red);
- static_cast<QPushButton*>(target)->setPalette(pal);
- }
- elseif(event->type()==QEvent::FocusOut){//event->type()==QEvent::Leave ||
- static_cast<QPushButton*>(target)->setStyleSheet("");
- QPalette pal;
- pal.setColor(QPalette::Active,QPalette::ButtonText,Qt::black);
- static_cast<QPushButton*>(target)->setPalette(pal);
- }
- else if(event->type()== QEvent::KeyPress){
- QPoint pos;
- QWidget *now_button= QWidget::focusWidget();
- QKeyEvent *k = (QKeyEvent *)event;
- QLayoutItem *next_button;
- switch (k->key()){
- case Qt::Key_Up:
- next_button=ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+8)%12);
- next_button->widget()->setFocus();
- break;
- case Qt::Key_Down:
- next_button=ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+4)%12);
- next_button->widget()->setFocus();
- #ifdef COURSE
- QCursor::setPos(pos);
- #endif
- break;
- case Qt::Key_Left:
- next_button=ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)-1+12)%12);
- next_button->widget()->setFocus();
- #ifdef COURSE
- QCursor::setPos(pos);
- #endif
- break;
- case Qt::Key_Right:
- next_button=ui->gridLayout->itemAt((ui->gridLayout->indexOf(now_button)+1)%12);
- next_button->widget()->setFocus();
- break;
- case Qt::Key_Period:
- pos = now_button->pos();
- pos.setX( 20 +pos.x()+(now_button->width())/2 );
- pos.setY( 20 +pos.y()+(now_button->height())/2 );
- //printf("/n***%d1%d***/n",pos.x(),pos.y());
- #ifdef COURSE
- QCursor::setPos(pos);
- #endif
- pos.setX(88);
- pos.setY(58);
- QMouseEvent *mEvnPress;
- QMouseEvent *mEvnRelease;
- mEvnPress = newQMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
- QApplication::sendEvent(QWidget::focusWidget(),mEvnPress);
- mEvnRelease = newQMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
- QApplication::sendEvent(QWidget::focusWidget(),mEvnRelease);
- next_button =ui->gridLayout->itemAt(ui->gridLayout->indexOf(QWidget::focusWidget()));
- break;
- default:
- returnQWidget::eventFilter(target,event);
- }
- if(next_button){
- pos.setX( 20 +next_button->geometry().x() + (next_button->geometry().width()) / 2 );
- pos.setY( 20 +next_button->geometry().y() + (next_button->geometry().height()) / 2);
- #ifdef COURSE
- QCursor::setPos(pos);
- #endif
- }
- return true;
- }
- return QWidget::eventFilter(target,event);
- }
- QT平台上模拟鼠标事件案例
- QT 模拟鼠标事件
- QT 模拟鼠标事件
- QT 模拟鼠标事件
- QT模拟鼠标事件
- linux 模拟鼠标事件 qt
- Qt模拟鼠标按键事件
- QT中模拟鼠标点击事件
- Qt使用Windows Api模拟鼠标单击双击滚轮事件
- Qt for Mac模拟鼠标以及键盘事件
- 鼠标事件案例
- Qt 模拟鼠标点击
- 模拟鼠标点击事件
- 模拟鼠标键盘事件
- C# 模拟鼠标事件
- c++模拟鼠标事件
- 鼠标键盘事件模拟
- c++--模拟鼠标事件
- Metasploit攻击Oracle的环境搭建(Failed to load the OCI library: no such file to load -- oci8)
- 应用程序配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这问题
- Unable to compile class for JSP
- Android 电话流程:接电话/挂电话/拨电话
- asp.net中使用ckfinder2选择图片并返回图片文件的路径的代码
- QT平台上模拟鼠标事件案例
- 精选2008年最佳设计素材资源&灵感
- C#解决主窗体隐藏
- SQL Server 2008 连接字符串
- 线程同步的方法
- ORACLE基础知识
- 编写的exe在别的机器上运行的时候总是报错 "找不到MSVCR100.dll 请重新安装",编译的机器上又不会报,这是怎么回事啊?是不是因为编译的机器上有特殊的配置?
- YX360与新华教育集团达成人才招聘合作意向
- 给软件工程师们的建议