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

[SOLVED] QKeyPress - Simulating key press



  • I'm trying to simulate a key press on a lineEdit, but nothing is writing inside the lineEdit..

    I call this slot with a button press :

    locationEdit->setFocus();
    
    QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_A, Qt::NoModifier);
    QApplication::sendEvent(locationEdit, key_press);
    
    QKeyEvent *key_release = new QKeyEvent(QKeyEvent::KeyRelease, Qt::Key_A, Qt::NoModifier);
    QApplication::sendEvent(locationEdit, key_release);
    

    Solved with :
    locationEdit->setFocus();
    QTest::keyClick(locationEdit, Qt::Key_A);

    Awesome QTest lib, !



  • i try

    mainwindow.h
    #include<QtTest/QtTest>
    
    my_project.pro
    Qt += testlib
    

    then i get include __cdecl ... exception.


Log in to reply