Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt 4.8.4 - Problem generating click - coordinates used by QPoint



  • Hello guys,

    I am working on an embedded linux project using Qt 4.8.4 and I want to generate click to a given position.

    First I will give you a sum-up of my code and then explains the problem.

    main.cpp : Declare a new QApplication

    @int main( int argc, char *argv[] )
    {
    main_app = new QApplication( argc, argv ) ;

    [....]

    }@

    mainwindow.h : header of the mainwindow

    @class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public :

    void generate_click( int coord_x, int coord_y ) ;

    [...]
    }@

    mainwindow.cpp: definition of my function

    @void MainWindow::generate_click(int x, int y)
    {

    printf("Enter generate_click : (%d/%d)\n", x, y) ;

    QPoint _pos_click( x, y ) ;
    QWidget *receiver = QApplication::widgetAt( _pos_click ) ;

    if(receiver == 0)
    printf("No element at the position (%d, %d)\n", x, y) ;
    else
    {
    QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    main_app->postEvent(receiver, event);
    QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
    main_app->postEvent(receiver, release);
    }
    }@

    This way it is NOT working, even if the coordinates are good in my first printf. I never see the error "receiver == 0 ). If we modify the Qt library to add a log in the function PostEvent we see that x or y (randomly) equal 0 or 1 so that's why there is no click.

    If I modify x and y this way (give a value to x / y ) it WORKS all the time :

    @void MainWindow::generate_click(int x, int y)
    {

    x = 20 ;
    y = 80 ;

    printf("Enter generate_click : (%d/%d)\n", x, y) ;

    QPoint _pos_click( x, y ) ;
    QWidget *receiver = QApplication::widgetAt( _pos_click ) ;

    if(receiver == 0)
    printf("No element at the position (%d, %d)\n", x, y) ;
    else
    {
    QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    main_app->postEvent(receiver, event);
    QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
    main_app->postEvent(receiver, release);
    }
    }@

    If I add an addition to the coordinate I receive, it also WORKS !

    @void MainWindow::generate_click(int x, int y)
    {

    x = x + 5;
    y = y + 5;

    printf("Enter generate_click : (%d/%d)\n", x, y) ;

    QPoint _pos_click( x, y ) ;
    QWidget *receiver = QApplication::widgetAt( _pos_click ) ;

    if(receiver == 0)
    printf("No element at the position (%d, %d)\n", x, y) ;
    else
    {
    QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    main_app->postEvent(receiver, event);
    QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease, receiver->mapFromGlobal(_pos_click), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
    main_app->postEvent(receiver, release);
    }
    }@

    Even if the Qt documentation says that QPoint is expecting an integer, I think that the addition or the fact that we give fixed coordinates will cast them in a type expected by QPoint.

    Are you able to tell me why the first example is not working ?

    I hope my problem is clear and let me know if you need more information.
    Thanks in advance.

    Gaël


Log in to reply