[SOLVED] Copy text from a lineEdit and a label to the clipboard
-
Sorry I meant to say copy to the clipboard (updated original post).
bq. QLineEdit::copy [qt-project.org] copies to the clipboard. QLabel does not support a copy method.
So why is my code above not working for the lineEdit, it is not copying the text to the clipboard when the button is clicked, why?
So there is no way to copy text in a label to the clipboard?
Thanks a lot for your reply.
-
Of course there is a way to copy text into the clipboard, even the text that is set on label. Did you look at [[doc:QClipBoard]]?
-
bq. Of course there is a way to copy text into the clipboard, even the text that is set on label. Did you look at QClipBoard?
No I didn't, I will take a look at it?
But for a simple lineEdit the code in my original post should work with the copy() method, no?
-
Yes, for the lineEdit, the copy() method should suffice to copy the selected text in the line edit to the clipboard.
From the documentation:
[quote]Copies the selected text to the clipboard, if there is any, and if echoMode() is Normal.[/quote]
If you want to copy the whole contents, you'll either have to first make sure it's all selected, or simply use QClipBoard yourself.Edit:
Also note that in your original code (if it would have been correct), the copy of the label would have overwritten the copy of the line edit, making copying the line edit useless to begin with. -
For some reason it was not enough to do just...
@void MainWindow::on_pushButton_clicked()
{
ui->lineEdit->copy();
}@
for the lineEdit, I had to selectAll first, but didn't work by manually selecting the text.
@void MainWindow::on_pushButton_clicked()
{
ui->lineEdit->selectAll();
ui->lineEdit->copy();
}@But for a textEdit and plainTextEdit the copy() method was enough and it did work when selecting the text manually.
-
I'm afraid to ask this question but I thing that's the only way I will start understanding how things work.
I was reading about the QClipboard class and it says that you first set the text to the clipboard using the setText() method and than your retrieve it with the text() method but I cannot make it work.
Pretending that I want to use QClipboard to copy the text in a label with in ID of myLabel to the clipboard, how would this need to be done using the sample below? What goes in "originalText" and "newText"?
@QClipboard *clipboard = QApplication::clipboard();
QString originalText = clipboard->text();clipboard->setText(newText);@
I know I know but I really need an example.
Thanks a lot for your help!
-
Why are you first reading what is currently on the clipboard? Is that relevant at all?
-
That's one of the reasons why I'm asking because the example doesn't make sense, this code is from the documentations of the QClipboard class.
-
The documentation just shows you how to get and how to set the text. That does not mean you need both as well. At least, not in the same piece of code.
-
I did it, thanks a lot for your help, I know you didn't want to give me the answer to make me think but believe me I'm having a hard time reading the documentation, I think we had a conversation about it a long time ago and I haven't practice since than.
Sorry for the headaches I'm causing here!
@void MainWindow::on_pushButton_clicked()
{
QClipboard *clip = QApplication::clipboard();
QString input = ui->myLabel->text();
clip->setText(input);
}@ -
Glad that you managed to solve your issue. Please modify the first posting you made and add a [SOLVED] tag to your topic title.