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. Automatize QDialog actions

Automatize QDialog actions

Scheduled Pinned Locked Moved Solved General and Desktop
automationkey eventexec
7 Posts 3 Posters 2.3k 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.
  • D Offline
    D Offline
    danividanivi
    wrote on last edited by danividanivi
    #1

    Hello,

    I am adapting an existing software to be embedded.
    It is developed with C++ and Qt.

    From the main user interface, a Dialog opens, I have to click down arrow and then Enter to select an item from a list.
    As no keyboard or mouse will be available, I have to automatize those two button clicks.

    My try was to this code:

    df.exec();
    QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
    QCoreApplication::postEvent (QApplication::focusWidget(), event);
    
    QKeyEvent *event2 = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
    QCoreApplication::postEvent (QApplication::focusWidget(), event2);
    

    I tried replacing QApplication::focusWidget() with QApplication::activeModalWidget() and some other things but without any luck, dialog opens but nothing is pressed, it keeps waiting for manual intervention.

    What is the correct way of doing this?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      the df.exec() is blocking. meaning it stay there until ok/cancel pressed and dialog closes so
      the postEvent will run after :)

      I was wondering if u cant directly get the Action and trigger it when dialog opens ?
      Else, you can make a timer and send dialog a signal to make it select and trigger some action.

      D 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        the df.exec() is blocking. meaning it stay there until ok/cancel pressed and dialog closes so
        the postEvent will run after :)

        I was wondering if u cant directly get the Action and trigger it when dialog opens ?
        Else, you can make a timer and send dialog a signal to make it select and trigger some action.

        D Offline
        D Offline
        danividanivi
        wrote on last edited by
        #3

        @mrjj I understand, so code under df.exec() will never run.
        "I was wondering if u cant directly get the Action and trigger it when dialog opens ?"
        I tried inserting the code in the dialog constructor but it is also not working, so I don't think so.
        Could you explain the timer workaround?
        Thank you for your help

        mrjjM 1 Reply Last reply
        0
        • D danividanivi

          @mrjj I understand, so code under df.exec() will never run.
          "I was wondering if u cant directly get the Action and trigger it when dialog opens ?"
          I tried inserting the code in the dialog constructor but it is also not working, so I don't think so.
          Could you explain the timer workaround?
          Thank you for your help

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @danividanivi
          Hi
          Well postEvent will run after exec() terminates. ( its local event loop)
          The code will not likely work in constructor as dialog is not visible yet.

          You can try ( in ctor of dialog)
          QTimer::singleShot(1000, {
          QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
          QCoreApplication::postEvent (QApplication::focusWidget(), event);

          QKeyEvent *event2 = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
          QCoreApplication::postEvent (QApplication::focusWidget(), event2);
          } );

          But im wondering how you have the QActions ?
          You cant directly find it and run its trigger() ?

          D 1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            To add to @mrjj, did you consider using QDialog::open ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • mrjjM mrjj

              @danividanivi
              Hi
              Well postEvent will run after exec() terminates. ( its local event loop)
              The code will not likely work in constructor as dialog is not visible yet.

              You can try ( in ctor of dialog)
              QTimer::singleShot(1000, {
              QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
              QCoreApplication::postEvent (QApplication::focusWidget(), event);

              QKeyEvent *event2 = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
              QCoreApplication::postEvent (QApplication::focusWidget(), event2);
              } );

              But im wondering how you have the QActions ?
              You cant directly find it and run its trigger() ?

              D Offline
              D Offline
              danividanivi
              wrote on last edited by
              #6

              @mrjj the timer singleShot solution is working like a charm!

              QTimer::singleShot(1000, []() {
                    QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
                    QKeyEvent *event2 = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                    QCoreApplication::postEvent (QApplication::focusWidget(), event);
                    QCoreApplication::postEvent (QApplication::focusWidget(), event2);} );
              

              The code for QActions is complex so I am glad I don't have to find things there and run the triggers.
              @SGaist I thought about that but it was creating an empty dialog, maybe I didn't implement it properly.

              In any case now this is working as expected, thank you both very much.

              mrjjM 1 Reply Last reply
              0
              • D danividanivi

                @mrjj the timer singleShot solution is working like a charm!

                QTimer::singleShot(1000, []() {
                      QKeyEvent *event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
                      QKeyEvent *event2 = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
                      QCoreApplication::postEvent (QApplication::focusWidget(), event);
                      QCoreApplication::postEvent (QApplication::focusWidget(), event2);} );
                

                The code for QActions is complex so I am glad I don't have to find things there and run the triggers.
                @SGaist I thought about that but it was creating an empty dialog, maybe I didn't implement it properly.

                In any case now this is working as expected, thank you both very much.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @danividanivi
                Ok super.
                To sleep good at night, i think i would check that correct widget have focus
                before sending enter to avoid some day it might find the close dialog button or something later on :)

                1 Reply Last reply
                1

                • Login

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