Input trough QPlainTextEdit
-
Hello I am a QT beginner and I'm still learning cpp. I really have no idea how to implement a feature and have already asked a few ppl and I got answers that I didn't understand so it would mean a lot if someone could thoroughly explain a solution. I have a QPlainTextEdit that is readonly and I would like to make it receive mouse and keyboard input(Key presses, mouse position, mouse buttons) but only when it is in focus(is the last widget that was clicked on). I really have no idea how to implement this but if its possible I would prefer to work like those signals that when an input is detected it calls a function with the input as its parameter. Also I already have a lot of code using this QPlainTextEdit so I really don't want to make a new one with classes or something. But please give me any solution that might work. Thank you!
-
@Grand_Titan
Please share a bit more about the use case: What is supposed to happen with those mouse and keyboard events? -
@Grand_Titan why did you delete your other thread about this subject ? It would have given @Axel-Spoerl some information to get started.
-
@SGaist cuz it was going nowhere and I figured it would be better making another one and explaining my issue in one message which it seems I have failed.
@Axel-Spoerl so I'm making an emulator of an old processor and I have this QPlainTextEdit called plainTextDisplay it basically displays ascii from a buffer in the memory. Now I don't want to allow users to change the display text so I just set readonly to true. Now I want to add a way to interact with that QPlainTextEdit so that when it is in focus to act like its own little pc so that when I press keys on my keyboard they get sent to another input buffer. Now I'll know how to do that but I have no idea how to capture the keys or use them. If its possible I would like to have a onKeyPressed function or something with the key as its parameter. And then just have a basic check to see if the plainTextDisplay is in focus if it isn't then just do nothing with that function and if it is then do something. -
@Grand_Titan said in Input trough QPlainTextEdit:
If its possible I would like to have a onKeyPressed function or something with the key as its parameter. And then just have a basic check to see if the plainTextDisplay is in focus if it isn't then just do nothing with that function and if it is then do something.
Use an Event Filter to your TextEdit should be a solution.
Search the doc and this forum about installEventFilter() to learn how it works. -
@mpergand tried for the last 20mins barely found anything and I'm not sure how it works but from what I understood doesn't that interfere with other plainTextEdits like I cant really filter the event on a specific widget. Can you please just make me an example on my scenario?
-
@Grand_Titan
Adding to @mpergand: you may want to start with the event filter just debugging every incoming event on the plain text edit.
That way you will see, if the events you expect are delivered.
Without having looked at the details, it should actually receive mouse can key events. Even if read only, you can still copy text. -
I will just throw one other possibility in. Maybe you are saying you would get what you want if you left the
QPlainTextEdit
editable, not readonly? Then do your own "readonly" by rejecting/ignoring/removing new characters going into the text edit while having all its editing events (keys, mouses)? Just a thought. -