Trying to redirect input from a QInputDialog to a std::cin
-
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
-
@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.
-
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.
-
@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?
-
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 ? -
@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.
-
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:
- Show Dialog.
- Dialog gives back string.
- Pipe the string through stringstream.
- Write it to the variable
Program I'm making is open source so can PM me if interested in looking at the source code
-