[Solved] Copy text from QString?
I want to copy the text from a QString (if possible along with the formating?) so that it can be pasted outside my application.
For example, when the string is copied through code then I can paste the string to say notepad.
How do I do that?
I tried by creating a QLineEdit object, setting the text from QString & then QLineEdit::copy();
But it does not work.
QLineEdit::copy() copies content to clipboard. If you want use this you have to manually paste it to notepad as notepad isn't part of your application.
Take a look "here":https://qt-project.org/doc/qt-4.8/intro-to-dbus.html and "here":https://qt-project.org/doc/qt-4.8/qtdbus.html if you want work with inter-process communication. But this is unix only I don't know what libs are for windows.
I wanted to copy from a QString not a QLineEdit in the first place.
Since I could not find any function in QString which could help me copy to clipboard.
I thought of a workaround of creating a QLineEdit object & then using copy from it.
However since I had created the object statically which went out of scope after the copy call.
That is why I think it could was not available while pasting to notepad.
Is this the standard way of copying to clipboard in Qt?
So do I have to main a dummy QLineEdit in heap memory all throughout the Application just for the purpose of copying to clipboard?
Check out "QClipboard":http://qt-project.org/doc/qt-4.8/qclipboard.html
It can be done using "QClipboard::setText()":http://qt-project.org/doc/qt-4.8/qclipboard.html#setText
Ah sorry I misunderstood your problem, I thought you want to copy it directly to notepad. So if you want copy it to clipboard just check doc posted by Code_ReaQtor.
By the way to copy from QLineEdit you have to use QLineEdit::selectAll() and then you can copy it. QLineEdit::copy() copies only selected text not all text, so you have to select it.