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 simulate user input to a native QFileDialog in Windows?
QtWS25 Last Chance

How to simulate user input to a native QFileDialog in Windows?

Scheduled Pinned Locked Moved Solved General and Desktop
qfiledialogsimulationinputwindows
7 Posts 4 Posters 952 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
    dave2
    wrote on 15 May 2023, 16:01 last edited by
    #1

    In Windows, I need to simulate user input to a QFileDialog for opening a file. More specifically, I need to:

    1. enter the name of the file to open
    2. accept the dialog

    The QFileDialog was opened in native mode (which is the default on Windows), something which I can't change.

    findChildren<QWidget*>() returns an empty list on such a native dialog, so I can't programmatically send QKeyEvent's to a QLineEdit (in order to simulate entry of the file name), or send a QMouseEvent to the "Open" button (in order to simulate acceptance of the dialog).

    I tried the following:

    fileDialog->selectFile("C:/foo/bar.txt"); // (1)
    static_cast<QDialog*>(fileDialog)->accept(); // (2)
    

    Note about (2): This weird cast is necessary, because QDialog::accept() is public, but QFileDialog::accept() is protected. Since accept() is virtual, (2) calls QFileDialog::accept(), as intended.

    After (1), I can see C:/foo/bar.txt entered in the edit field of the dialog.

    (2), however, does not work: the dialog remains open. When debugging into QFileDialog::accept(), I can see the reason of accept()'s failure: the call to QFileDialog::selectedUrls() (qfiledialog.cpp, line 2700) returns an empty list. This, in turn, is due to the fact that QFileDialogPrivate::userSelectedFiles() (qfiledialog.cpp, line 1216) also returns an empty list.

    So, is there a way to do that with the Qt API, or should I dive into the (ugly!) Windows API?

    My environment: Windows 10, Visual Studio 2017, Qt 6.1

    M 1 Reply Last reply 15 May 2023, 18:02
    0
    • S SGaist
      15 May 2023, 18:31

      Hi,

      Can you explain your use case ? Are you implementing tests ?

      A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 15 May 2023, 19:54 last edited by
      #4

      @dave2
      Synthesizing Qt key and mouse events with a native file dialog will not work, I am afraid. The native dialog doesn't know about Qt.

      You can either send Qt events to a non-native dialog, as proposed by @mpergand. Or you dive into the Windows API to simulate what needs to be simulated.

      Software Engineer
      The Qt Company, Oslo

      D 1 Reply Last reply 16 May 2023, 18:48
      3
      • D dave2
        15 May 2023, 16:01

        In Windows, I need to simulate user input to a QFileDialog for opening a file. More specifically, I need to:

        1. enter the name of the file to open
        2. accept the dialog

        The QFileDialog was opened in native mode (which is the default on Windows), something which I can't change.

        findChildren<QWidget*>() returns an empty list on such a native dialog, so I can't programmatically send QKeyEvent's to a QLineEdit (in order to simulate entry of the file name), or send a QMouseEvent to the "Open" button (in order to simulate acceptance of the dialog).

        I tried the following:

        fileDialog->selectFile("C:/foo/bar.txt"); // (1)
        static_cast<QDialog*>(fileDialog)->accept(); // (2)
        

        Note about (2): This weird cast is necessary, because QDialog::accept() is public, but QFileDialog::accept() is protected. Since accept() is virtual, (2) calls QFileDialog::accept(), as intended.

        After (1), I can see C:/foo/bar.txt entered in the edit field of the dialog.

        (2), however, does not work: the dialog remains open. When debugging into QFileDialog::accept(), I can see the reason of accept()'s failure: the call to QFileDialog::selectedUrls() (qfiledialog.cpp, line 2700) returns an empty list. This, in turn, is due to the fact that QFileDialogPrivate::userSelectedFiles() (qfiledialog.cpp, line 1216) also returns an empty list.

        So, is there a way to do that with the Qt API, or should I dive into the (ugly!) Windows API?

        My environment: Windows 10, Visual Studio 2017, Qt 6.1

        M Offline
        M Offline
        mpergand
        wrote on 15 May 2023, 18:02 last edited by
        #2

        @dave2
        Seems to work with non native dialog:

        QApplication app(argc, argv);
        
        QFileDialog fileDialog;
        fileDialog.setOption(QFileDialog::DontUseNativeDialog);
        fileDialog.selectFile( your file );
        fileDialog.show();
        
        QTimer::singleShot(2000, [&fileDialog]()
        {
        	QKeyEvent press(QEvent::KeyPress, Qt::Key_Return,0);
        	qApp->sendEvent(&fileDialog,&press);
        
        	QKeyEvent release(QEvent::KeyRelease, Qt::Key_Return,0);  // seems optional
        	qApp->sendEvent(&fileDialog,&release);
        
        	qDebug()<<fileDialog.selectedFiles();
        });
        
        D 1 Reply Last reply 16 May 2023, 18:48
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 May 2023, 18:31 last edited by
          #3

          Hi,

          Can you explain your use case ? Are you implementing tests ?

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

          A D 2 Replies Last reply 15 May 2023, 19:54
          0
          • S SGaist
            15 May 2023, 18:31

            Hi,

            Can you explain your use case ? Are you implementing tests ?

            A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote on 15 May 2023, 19:54 last edited by
            #4

            @dave2
            Synthesizing Qt key and mouse events with a native file dialog will not work, I am afraid. The native dialog doesn't know about Qt.

            You can either send Qt events to a non-native dialog, as proposed by @mpergand. Or you dive into the Windows API to simulate what needs to be simulated.

            Software Engineer
            The Qt Company, Oslo

            D 1 Reply Last reply 16 May 2023, 18:48
            3
            • M mpergand
              15 May 2023, 18:02

              @dave2
              Seems to work with non native dialog:

              QApplication app(argc, argv);
              
              QFileDialog fileDialog;
              fileDialog.setOption(QFileDialog::DontUseNativeDialog);
              fileDialog.selectFile( your file );
              fileDialog.show();
              
              QTimer::singleShot(2000, [&fileDialog]()
              {
              	QKeyEvent press(QEvent::KeyPress, Qt::Key_Return,0);
              	qApp->sendEvent(&fileDialog,&press);
              
              	QKeyEvent release(QEvent::KeyRelease, Qt::Key_Return,0);  // seems optional
              	qApp->sendEvent(&fileDialog,&release);
              
              	qDebug()<<fileDialog.selectedFiles();
              });
              
              D Offline
              D Offline
              dave2
              wrote on 16 May 2023, 18:48 last edited by
              #5

              @mpergand I trust that the code you propose works for a non native dialog. But since I am dealing with a native dialog, it doesn't really help.

              1 Reply Last reply
              0
              • S SGaist
                15 May 2023, 18:31

                Hi,

                Can you explain your use case ? Are you implementing tests ?

                D Offline
                D Offline
                dave2
                wrote on 16 May 2023, 18:48 last edited by
                #6

                @SGaist I am implementing a demo, which goes through a typical usage scenario of our application.

                1 Reply Last reply
                0
                • A Axel Spoerl
                  15 May 2023, 19:54

                  @dave2
                  Synthesizing Qt key and mouse events with a native file dialog will not work, I am afraid. The native dialog doesn't know about Qt.

                  You can either send Qt events to a non-native dialog, as proposed by @mpergand. Or you dive into the Windows API to simulate what needs to be simulated.

                  D Offline
                  D Offline
                  dave2
                  wrote on 16 May 2023, 18:48 last edited by
                  #7

                  @Axel-Spoerl That's what I was fearing, but thanks for confirming.

                  1 Reply Last reply
                  0
                  • D dave2 has marked this topic as solved on 16 May 2023, 18:49

                  3/7

                  15 May 2023, 18:31

                  • Login

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