+ 1

C++ Qt Console App — How to read user input in event driven program state?

Today I began making a console app in C++ w/ Qt. At first I am just making the app monitor a file and detect when the file is modified. I created a slot to catch when that signal triggers. At first I had a while () loop in the main { } section to catch user input (had a command so it would display what files are configured to watch) but once I got the signal and slot connection working I noticed that the entire console app is actually in an event driven flow state, not a sequential flow state like normal C++ console app. So when it was in the while loop to monitor user input, the messages from the slot when the file would be modified wouldn't occur until after the while loop ended, which was at program exit. I commented out the while loop, and indeed, the app is running in an event driven state, ie: the main {} block executes and can have no loop in it but the app remains running. It sits listening for events to react to. How can I make Qt accept user input in the event driven state?

18th Jan 2021, 12:21 AM
Nathan Stanley
Nathan Stanley - avatar
2 ответов
+ 1
Can I have a look at your source code(or another minimum working example) ?
18th Jan 2021, 3:00 AM
Arsenic
Arsenic - avatar
0
I will give you some code later as I'm heading out for the day. Basically you can tell Qt Creator to make a Qt Console App and that's it. It's an event driven console app already in that state. If you create QObject, or use ones in Qt you can connect signals and slots to catch and handle events when they occur. Usually in C++ tutorials, to deal with taking user input we use con or readline. When I did this, I think it was taking control of the program thread so event handling wouldn't occur until after user input. I've worked on my code since I asked this, but I'll see if I can make a minimal example as I'm still interested to know of ways to have console user input prompt.
19th Jan 2021, 7:44 PM
Nathan Stanley
Nathan Stanley - avatar