How to Make a Command Prompt Using Signals and Slots?



  • Hi all,

    I am very new to Qt development. I've started working on an application and I would like one Widget within my application to function similar to a command prompt (but for custom commands in my application only, not an actual command prompt). Using a QLineEdit and a QPlainTextEdit I made something that approximates a command prompt. The user can type something in to the LineEdit, and when he hits enter I can parse the value and then append something to the PlainTextEdit. For example if the user types "help" I can append a help message and everything works fine.

    I'm stumped on how to handle more complex cases, specifically if the command requires a signal to be emitted and a response to handled in a slot. For example if the user types "process data" I want to call the function processData() which emits a signal, and it's output is returned in a slot. That output needs to be formatted then appended to the command prompt. How would you recommend I do this?

    Should I be looking into synchronous signals and slots? Thanks in advance, I appreciate it.



  • @psoberoi A command prompt is often by nature synchronous. You type a command and it gives output before you can type another command. If it's asynchronous you can type new commands before the previous ones are processed and the output may come in random order. Which one do you want? If you want synchronous, you can write normal procedural code which just calls functions and returns the answer. I don't see how signals and slots would help there at all. I mean, if you don't need signals and slots for some external reasons (for example there's some event driven code also in your application which already uses those signals and slots) you don't need it for synchronous operation.

    But if you do need them, it should be straightforward in most basic cases where there's no threads and one sender per one receiver and vice versa. When you create a basic connection with "connect(sender, signal, receiver, slot)" it will be synchronous. When you use emit it calls the slot as if it were a normal function call. If you have coded the signal, slot and emit yourself you could replace it with a normal function call.



  • Thank you for the quick reply. I agree with you that a command prompt should be synchronous to the user. My issue is some of the commands I want to support require emitting signals and receiving the response via a slot. For example I support one command which spawns a new process, and I receive it's output via a signal.

    How would you recommend making a signal\slot appear synchronous to the user? I've been trying to follow this thread: https://forum.qt.io/topic/23550/making-asynchronous-calls-work-like-synchronous-calls/4. I'm not sure if that's the correct way for me to proceed as I need to wait for a particular slot to be called.

    How would I support a command that requires multiple signals/slots to run before posting it's final output? I realize I'm in over my head. I appreciate any advice you can offer. Thanks again.


  • Moderators

    @psoberoi I would just add finished() signal to all supported commands. If the user starts such a command, then the command prompt is blocked until finished() signal is emitted. No need to wait explicitly for anything. In addition your commands can have other signals, but as long as finished() is not emitted the command is not considered to be finished.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.