I have a QTextEdit, which contains the text:
It's test.
I want to select this text and copy it to my clipboard using Ctrl+C, but I want to replace "test" with "good" in my clipboard.
I mean: I want to get this in my clipboard after copying original text from QTextEdit
:
It's good.
Note: I want to replace clipboard when I have copied text from QTextEdit only, I don't want to replace clipboard when any copy action done.
Thanks.
It would be better to use Signals/Slots to synchronize what to change in clipboard with what you are actually doing in QTextEdit field to avoid undefined behaviors and accidentally modifying things outside the scope of your task. in order to do that catch a
signal
emitted when you highlight this particular QTextEdit field, that signal insures you you can copy the highlighted textQTextEdit::copyAvailable(bool yes)
..yes
indicates availability of a highlighted text.Most importantly, make sure you are accessing global clipboard only when you CTRL+C the highlighted text from your QTextEdit field, by attaching to the signal
QClipboard::dataChanged
which indicates that you copied the text ... then only modify the text.Example: class files can look like this:
Class
.cpp
Assuming you have a pointer to a
QClipboard
calledclipboard
:This uses the functions
QString::replace
to modify the text of the clipboard (Accessed fromQClipboard::text
) andQClipboard::setText
to set the new text for the clipboard.