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. Trying to redirect input from a QInputDialog to a std::cin
Forum Updated to NodeBB v4.3 + New Features

Trying to redirect input from a QInputDialog to a std::cin

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 329 Views 3 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 22 Oct 2023, 00:35 last edited by A Former User
    #1

    Hello, so I'm trying to redirect input from the frontend obtained via a QInputDialog to a std::cin >> variable line in the C++ backend each time a button is clicked (let's call it a "Pass value from the front end to back end" button) using a streambuf and I'm not sure on if it's even possible to come up with a solution to this niche problem that has these constraints in mind:

    • Need to display a QInputDialog when we "need" data => So when the button is clicked
    • Need to make the result of that QInputDialog what std::cin then reads
    • Need std::cin to block while the QInputDialog is open but not otherwise
    • Need to signal that the entire sequence of data for the current 'read' has been read, returning eof seems to do that, but it also puts the stream into the eof state and thereby the fail state by setting the eofbit and failbit.

    Anybody know if this is even possible to do?

    Thanks for any input on this

    1 Reply Last reply
    0
    • ? A Former User
      22 Oct 2023, 11:16

      @jeremy_k Hello Jeremy, thanks for getting back to me

      If by 'process' you mean operating system process, then yes, it is within the same process.

      Why must the call to std::cin be in a different thread?

      Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

      J Offline
      J Offline
      jeremy_k
      wrote on 22 Oct 2023, 19:22 last edited by
      #5

      @Publically-visible-name said in Trying to redirect input from a QInputDialog to a std::cin:

      Why must the call to std::cin be in a different thread?

      @Publically-visible-name said in Trying to redirect input from a QInputDialog to a std::cin:

      Need std::cin to block while the QInputDialog is open but not otherwise

      If the thread is blocking on read access to std::cin, it is not executing the Qt event loop. Without a running event loop, the dialog will not be responsive.

      Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

      https://stackoverflow.com/questions/3797280/injecting-string-to-cin presents a solution by replacing cin's rdbuf. It also mentions using clear() to reset eof.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jeremy_k
        wrote on 22 Oct 2023, 02:53 last edited by
        #2

        Is this all in a single process?

        If so, the blocking read from std::cin will need to occur in a separate thread from the QInputDialog. That means that whatever is replacing the stdin std::cin needs to be thread safe.

        https://en.cppreference.com/w/cpp/io/cin

        The 'c' in the name refers to "character" (stroustrup.com FAQ);

        Any additional structure is up to the programmer to establish.

        The description sounds possible, but convoluted.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        ? 1 Reply Last reply 22 Oct 2023, 11:16
        0
        • J jeremy_k
          22 Oct 2023, 02:53

          Is this all in a single process?

          If so, the blocking read from std::cin will need to occur in a separate thread from the QInputDialog. That means that whatever is replacing the stdin std::cin needs to be thread safe.

          https://en.cppreference.com/w/cpp/io/cin

          The 'c' in the name refers to "character" (stroustrup.com FAQ);

          Any additional structure is up to the programmer to establish.

          The description sounds possible, but convoluted.

          ? Offline
          ? Offline
          A Former User
          wrote on 22 Oct 2023, 11:16 last edited by A Former User
          #3

          @jeremy_k Hello Jeremy, thanks for getting back to me

          If by 'process' you mean operating system process, then yes, it is within the same process.

          Why must the call to std::cin be in a different thread?

          Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

          S J 2 Replies Last reply 22 Oct 2023, 19:05
          0
          • ? A Former User
            22 Oct 2023, 11:16

            @jeremy_k Hello Jeremy, thanks for getting back to me

            If by 'process' you mean operating system process, then yes, it is within the same process.

            Why must the call to std::cin be in a different thread?

            Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Oct 2023, 19:05 last edited by
            #4

            Hi,

            Something is not clear, you wrote that you have one single application and within that application, you want to have something that uses std::cin and Qt at the same time ?

            Why not replace std::cin with your input dialog ?

            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 23 Oct 2023, 01:56
            0
            • ? A Former User
              22 Oct 2023, 11:16

              @jeremy_k Hello Jeremy, thanks for getting back to me

              If by 'process' you mean operating system process, then yes, it is within the same process.

              Why must the call to std::cin be in a different thread?

              Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

              J Offline
              J Offline
              jeremy_k
              wrote on 22 Oct 2023, 19:22 last edited by
              #5

              @Publically-visible-name said in Trying to redirect input from a QInputDialog to a std::cin:

              Why must the call to std::cin be in a different thread?

              @Publically-visible-name said in Trying to redirect input from a QInputDialog to a std::cin:

              Need std::cin to block while the QInputDialog is open but not otherwise

              If the thread is blocking on read access to std::cin, it is not executing the Qt event loop. Without a running event loop, the dialog will not be responsive.

              Not sure how the idea of "having it be blocking on another thread' would work. Are we emitting some signal in a thread-safe way or something so that the main thread shows the dialog and then that will put it into a stringstream that had its buffer swapped with cin in a thread safe way?

              https://stackoverflow.com/questions/3797280/injecting-string-to-cin presents a solution by replacing cin's rdbuf. It also mentions using clear() to reset eof.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • S SGaist
                22 Oct 2023, 19:05

                Hi,

                Something is not clear, you wrote that you have one single application and within that application, you want to have something that uses std::cin and Qt at the same time ?

                Why not replace std::cin with your input dialog ?

                ? Offline
                ? Offline
                A Former User
                wrote on 23 Oct 2023, 01:56 last edited by A Former User
                #6

                Yeah with future programs, I'll build it from the ground up better so as to avoid having to do funny business)).

                It's possible. Got it working. Found it easier to work with a pointer to a DialogInput than std::cin though.Followed this process:

                1. Show Dialog.
                2. Dialog gives back string.
                3. Pipe the string through stringstream.
                4. Write it to the variable

                Program I'm making is open source so can PM me if interested in looking at the source code

                1 Reply Last reply
                0
                • System has marked this topic as solved on 23 Oct 2023, 02:00

                1/6

                22 Oct 2023, 00:35

                • Login

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