2016-12-04 4 views
2

다른 화면 크기에 맞게 웹 크기를 테스트하는 프로그램을 작성하고 싶습니다. QWebEngineView를 사용하여 윈도우를 만들고 사용자 마우스를 클릭하여 웹 페이지의 드롭 요소를 드래그하여 시뮬레이트해야합니다. qApp-> sendEvent 및 qApp-> postEvent 시도했지만 작동하지 않습니다.QWebEngineView에 대한 마우스 클릭 시뮬레이션

QMouseEvent *event1 = new QMouseEvent (QEvent::MouseButtonPress, point, 
Qt::LeftButton, 
Qt::LeftButton, 
Qt::NoModifier); 

qDebug() << qApp->sendEvent (driver, static_cast <QEvent *> (event1)); 

QMouseEvent *event2 = new QMouseEvent (QEvent::MouseButtonRelease, point, 
Qt::LeftButton, 
Qt::LeftButton, 
Qt::NoModifier); 

qDebug() << qApp->sendEvent (driver, static_cast <QEvent *> (event2)); 

두 클래스 모두 true를 반환하지만 웹 페이지의 이벤트는 트리거되지 않습니다. 먼저 초점을 맞추어야합니까? 그것은 부모가 없으며 창입니다. 더 이상 그래픽 요소가 사용되지 않습니다. 도와 주시겠습니까?

Ps : javacript는 beacause를 사용하고 싶지 않습니다. javascript 시뮬레이션 된 클릭을 사용하여 입력에 집중할 수 없으며 드래그 앤 드롭을 위해 캔버스를 테스트 할 좌표가 없습니다.

답변

1

당신이 QWebEngineView의 자식 개체에 액세스하고 좋았지는 다음과 같은 이벤트를 보내야 할 것 같다은 마우스 왼쪽의 구현 click.You가 드래그를 시뮬레이션 릴리스를 누르십시오에 대한 postions에게 clickPos을 변경할 수있다 . 다음 답변에서

void LeftMouseClick(QWidget* eventsReciverWidget, QPoint clickPos) 
{ 
    QMouseEvent *press = new QMouseEvent(QEvent::MouseButtonPress, 
              clickPos, 
              Qt::LeftButton, 
              Qt::MouseButton::NoButton, 
              Qt::NoModifier); 
    QCoreApplication::postEvent(eventsReciverWidget, press); 
    // Some delay 
    QTimer::singleShot(300, [clickPos, eventsReciverWidget]() { 
     QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease, 
               clickPos, 
               Qt::LeftButton, 
               Qt::MouseButton::NoButton, 
               Qt::NoModifier); 
     QCoreApplication::postEvent(eventsReciverWidget, release); 
    })); 
} 
QWebEngineView webView = new QWebEngineView(); 
// You need to find the first child widget of QWebEngineView. It can accept user input events. 
QWidget* eventsReciverWidget = nullptr; 
foreach(QObject* obj, webView->children()) 
{ 
    QWidget* wgt = qobject_cast<QWidget*>(obj); 
    if (wgt) 
    { 
     eventsReciverWidget = wgt; 
     break; 
    } 
} 
QPoint clickPos(100, 100); 
LeftMouseClick(eventsReciverWidget, clickPos); 

차입 Qt WebEngine simulate Mouse Event How to send artificial QKeyEvent to QWebEngineView?

+1

감사합니다! 귀하의 솔루션은 완벽하게 작동합니다. –

관련 문제