Case study of simulating mouse events on the QT Platform

Source: Internet
Author: User

QT PlatformUpperSimulate mouseEvent cases are the content of this article, mainly to understandQT PlatformOnSimulate mouseThe application of the event. The implementation of the specific content is described in this article. QTest4.lib needs to be imported; otherwise, a connection error may occur.

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

Simulate mouse buttons on QT Platform

Similar to the analog keyboard buttons, this function is implemented by sending corresponding events. Install the corresponding event listener and send specific events:

 
 
  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);       

The main analysis functions are:

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

Parameter Analysis:

 
 
  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. 

Note:

The position of the pos here is a local position inside the widget that accepts the mouse event. That is to say, the mouse button creation point is: first through QApplication: sendEvent (QWidget: focusWidget (), mEvnPress); // That is to say, it indicates the widget to which it is passed, then, based on mEventPress, modify the positioning in the widget and the specific buttons.

 
 
  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_button= QWidget::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);  

Summary:QT PlatformUpperSimulate mouseThe content of the event case has been introduced.QT PlatformMediumSimulate mouseContent learning is helpful to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.