How to copy highlighted text from QTextEdit, PyQt6
-
I have an application designed in QtDesigner and in it, there's a QTextEdit in which I put data from a query. I want to allow the user to select and copy text using the shortcut ctrl+c, but I can't seem to get it to work. The quick search I've done only shows examples on how to do it using buttons.
Can I enable the copying using the shortcut? And if yes, how would I do it?
Any answer is appreciated.
-
@WorldTeacher
It should be working as-is, no "examples". You should be able to select text, press Ctrl+C, and then paste (Ctrl+V) text back into theQTextEdit
or elsewhere since it is on your clipboard.Assuming you are using a platform where Ctrl+C is the "copy shortcut".
-
@WorldTeacher For me copying text via Strg-C from QTextEdit work just fine out of the box (Linux).
-
@JonB I thought it might have something to do with me running the application directly from the .py file, so I built the application into an .exe, but copying still doesn't work. Here's how the QTextEdit is configured
is there anything I need to change? -
@WorldTeacher
Every property you see whose name is in bold means it is set to something other than the default.You might try:
- focusPolicy: change off NoFocus?
- inputMethodHints: change off ImhNone
But these are literally wild guesses.
You can see in textInteractionFlags that it's set for "text selectable by mouse or keyboard*, which looks right. Instead of keyboard Ctrl+C did you try right-click on selected text, does context menu offer Copy?
Try creating a brand new
QTextEdit
and don't change anything at all on it. Does that work? -
@JonB said in How to copy highlighted text from QTextEdit, PyQt6:
focusPolicy: change off NoFocus?
Agree
@WorldTeacher If the widget can't have focus then shortcuts will not work... -