Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Copy text from a lineEdit and a label to the clipboard
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 20.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    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()
    {
    ui->lineEdit->copy();
    ui->label->copy();
    }@

    but it doesn't work.

    Can some one please show me how to do this?

    Thanks

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Where so you like to copy the text to?

      "QLineEdit::copy":http://qt-project.org/doc/qt-4.8/qlineedit.html#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":http://qt-project.org/doc/qt-4.8/qlabel.html#text-prop
      @void MainWindow::on_pushButton_clicked()
      {
      QString txt = ui->lineEdit->text();
      ui->label->setText(txt);
      }@

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fs_tigre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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]]?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fs_tigre
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fs_tigre
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                1
                • F Offline
                  F Offline
                  fs_tigre
                  wrote on last edited by
                  #8

                  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!

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Why are you first reading what is currently on the clipboard? Is that relevant at all?

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fs_tigre
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fs_tigre
                          wrote on last edited by
                          #12

                          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);
                          }@

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved