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. QKeyPress - Simulating key press

QKeyPress - Simulating key press

Scheduled Pinned Locked Moved Solved General and Desktop
qkeyeventqt 5.7
4 Posts 3 Posters 3.5k Views
  • 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.
  • S Offline
    S Offline
    sayan275
    wrote on last edited by sayan275
    #1

    Re: [SOLVED] QKeyPress - Simulating key press

    I tried the same approach, but not able to get written on the lineEdit widget

    ui->lineEdit->setFocus();
        QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
    QApplication::sendEvent(ui->lineEdit, key_press);
    

    Alternately

    QApplication::postEvent(ui->lineEdit, key_press);
    

    also didn't succeed.

    I tried the below also and didn't get any result.

    QKeyEvent key(QEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
    QApplication::sendEvent(ui->lineEdit, &key);
    if (key.isAccepted()) {
          qDebug()<<"everything is ok";
    } else {
          qDebug()<<"something wrong";
    }
    

    Please suggest what am I missing.

    Regards,
    Sayan

    S Z 2 Replies Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      For my keyboard i do like this

      auto FOCUSOBJ = QGuiApplication::focusObject();
      QString keyStr(QKeySequence(key).toString()); // key is int with keycode
      QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, keyStr);
      QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
      QCoreApplication::sendEvent(FOCUSOBJ, &pressEvent);
      QCoreApplication::sendEvent(FOCUSOBJ, &releaseEvent);

      which makes keyboard type into editlines etc.

      1 Reply Last reply
      4
      • S sayan275

        Re: [SOLVED] QKeyPress - Simulating key press

        I tried the same approach, but not able to get written on the lineEdit widget

        ui->lineEdit->setFocus();
            QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
        QApplication::sendEvent(ui->lineEdit, key_press);
        

        Alternately

        QApplication::postEvent(ui->lineEdit, key_press);
        

        also didn't succeed.

        I tried the below also and didn't get any result.

        QKeyEvent key(QEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
        QApplication::sendEvent(ui->lineEdit, &key);
        if (key.isAccepted()) {
              qDebug()<<"everything is ok";
        } else {
              qDebug()<<"something wrong";
        }
        

        Please suggest what am I missing.

        Regards,
        Sayan

        S Offline
        S Offline
        sayan275
        wrote on last edited by
        #3

        Hi,
        got it done.

        ui->lineEdit->setFocus();
        QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier, "x");
        QApplication::sendEvent(ui->lineEdit, key_pre..ss);
        

        the last parameter "x"

        1 Reply Last reply
        1
        • S sayan275

          Re: [SOLVED] QKeyPress - Simulating key press

          I tried the same approach, but not able to get written on the lineEdit widget

          ui->lineEdit->setFocus();
              QKeyEvent *key_press = new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
          QApplication::sendEvent(ui->lineEdit, key_press);
          

          Alternately

          QApplication::postEvent(ui->lineEdit, key_press);
          

          also didn't succeed.

          I tried the below also and didn't get any result.

          QKeyEvent key(QEvent::KeyPress, Qt::Key_X, Qt::NoModifier);
          QApplication::sendEvent(ui->lineEdit, &key);
          if (key.isAccepted()) {
                qDebug()<<"everything is ok";
          } else {
                qDebug()<<"something wrong";
          }
          

          Please suggest what am I missing.

          Regards,
          Sayan

          Z Offline
          Z Offline
          Zonghan Gan
          wrote on last edited by
          #4

          @sayan275 Dear Sayan I think you may know the answer to my problem. I'm struggling to send a self-defined event from main.cpp to the ui->boardView in mainwindow.cpp, as the ui->boardView is only defined in mainwindow.cpp. Yet the eventfilter can only be installed on the ui->boardView. It seems you've done similar sth similar. Could you please tell me how to realize such feature? (there are also detailed code about this which I put on stackoverflow https://stackoverflow.com/questions/60424216/how-to-use-eventfilter-under-child-widget-to-catch-self-define-event)

          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