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.
- #include<QtTest/QTest>
- 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:
- 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);
The main analysis functions are:
- QMouseEvent::QMouseEvent ( Type type, const QPoint & position,Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiersmodifiers )
Parameter Analysis:
- 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.
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.
- 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);
- }
Summary:QT PlatformUpperSimulate mouseThe content of the event case has been introduced.QT PlatformMediumSimulate mouseContent learning is helpful to you!