How to copy highlighted text from QTextEdit, PyQt6
-
wrote on 28 Sept 2022, 10:45 last edited by
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.
-
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.
wrote on 28 Sept 2022, 10:53 last edited by JonB@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".
-
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 For me copying text via Strg-C from QTextEdit work just fine out of the box (Linux).
-
@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".
wrote on 28 Sept 2022, 11:00 last edited by@JonB I'm on Windows 10 and currently, whenever I press ctrl+c, it doesn't copy the text I've marked.
I'll do a quick test to see if a hunch I have is correct.
-
@JonB I'm on Windows 10 and currently, whenever I press ctrl+c, it doesn't copy the text I've marked.
I'll do a quick test to see if a hunch I have is correct.
wrote on 28 Sept 2022, 11:20 last edited by@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? -
@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?wrote on 28 Sept 2022, 11:26 last edited by JonB@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? -
@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... -
@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...wrote on 28 Sept 2022, 11:31 last edited by@jsulm said in How to copy highlighted text from QTextEdit, PyQt6:
If the widget can't have focus then shortcuts will not work...
Ah ha! :)
-
@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?wrote on 28 Sept 2022, 11:34 last edited by
1/9