Important: Please read the Qt Code of Conduct -

[SOLVED] Copy text from a lineEdit and a label to the clipboard

  • Hi,

    What would be the best way to copy text from a lineEdit and a label to the clipboard?

    I have tried this...

    @void MainWindow::on_pushButton_clicked()

    but it doesn't work.

    Can some one please show me how to do this?


  • Where so you like to copy the text to?

    "QLineEdit::copy": copies to the clipboard. QLabel does not support a copy method.

    If you like to copy from the line edit to the label, you might want to have a look to "QLabel::setText":
    @void MainWindow::on_pushButton_clicked()
    QString txt = ui->lineEdit->text();

  • Sorry I meant to say copy to the clipboard (updated original post).

    bq. QLineEdit::copy [] 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.

    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()
    for the lineEdit, I had to selectAll first, but didn't work by manually selecting the text.
    @void MainWindow::on_pushButton_clicked()

    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();


    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();

  • Glad that you managed to solve your issue. Please modify the first posting you made and add a [SOLVED] tag to your topic title.

Log in to reply