Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make a button press on a point (x, y) in QWidget
Qt 6.11 is out! See what's new in the release blog

How to make a button press on a point (x, y) in QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 710 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    I am using QWebEngineView to make a web view to my project so first I add a widget in the UI form and promoted it to QWebEngineView it works fine now I want to make a button press inside this widget on a point x,y (which is a button inside the website viewed in the widget)
    so I make a keypress when I press on S from the keyboard it save the point of the mouse hover inside this widget (I hover exactly on the button inside the website)
    I use this code to save the points

    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_S) {
                p = ui->widget->mapFromGlobal(QCursor::pos()); //QPoint varible I use it to save the x,y inside the widget
                qDebug() << "point inside the widget = " << p.x() << " " << p.y();
            }
        }
        return QObject::eventFilter(watched, event);
    }
    

    then I have button when i press it should make a button press inside the QWebEngineView widget I use this code inside my button

    QTest::mouseClick(ui->widget, Qt::LeftButton, Qt::KeyboardModifier::NoModifier, p);
    //p  saved the location of the button in the webview widget
    

    but it didn't press that button or make this button press ? so what is the problem of my code which make it not press on the button inside the webview

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by hskoglund
      #2

      Hi, the QWebEngineView doesn't like mouse events, instead we have to find the first child widget inside it, here's some code to try before QTest::mouseClick():

      // find the kid inside QWebEngineView that will accept mouse events
      QWidget* childThatAcceptsInput = nullptr;
      for (auto w : ui->widget->children())
      {
          QWidget* child = qobject_cast<QWidget*>(w);
          if (child)
          {
              childThatAcceptsInput = child;
              break;
          }
      }
      
      // ok, mouseClick the kid
      QTest::mouseClick(childThatAcceptsInput, Qt::LeftButton, Qt::KeyboardModifier::NoModifier, p);
      1 Reply Last reply
      2
      • AmrCoderA Offline
        AmrCoderA Offline
        AmrCoder
        wrote on last edited by
        #3

        Thank you that works fine

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved