I am working on a text based game in PyQt. I have a QTextDisplay for the “window” and a QLineEdit for the actual prompt. They are hooked up so that when the user hits the return key in the line edit, it appears in the text display. Unfortunately, this needs to be a GUI application and not a simple command program because there needs to be pictures (not my decision).
I would like the execution to pause and wait for user input into the line edit (much like input() works in a command application). QInputDialog behaves in this way, except launches a dialog box. This is pretty cumbersome. I have tried while loops, but it doesn’t look like PyQt likes them (it hangs and doesn’t actually hit the app.exec_()).
On a side note, because of the dislike of the while loop, how would I go about writing a game loop in Qt? Should I use a QThread?
I think you can do what you want with just signal and slots. Qt runs it’s own event loop, you should not try to write a second one on top of it.
In the class that handles interpreating the input add a slot
deal_with_inputand then connect the signaleditingFinishedto it.ex:
You can quible over the exact class design (maybe you don’t want this logic embeded in you text display widget), but the logic will hold.
Basically, Qt will sit there, doing nothing, until the user pokes it, and then it will process the keyboard/mouse events resize, whatever. When the user hits return in the
QLineEditwidget, the widget emits the signaleditingFinished(doc). We connect that to aSlotwhich we created, which basically just waits for a signal to trigger it (signal and slots are just a well thought out type-safe callback scheme). Once the slot recives the signal, it executes it’s function and then goes back to waiting. (In this case, the function grabs the text from the line edit box, clears the box, and then does what ever your game code needs to do).