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 QString?
QtWS25 Last Chance

[Solved] Copy text from QString?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.7k Views
  • 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    Hi,
    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.
    Thank You.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SetBetterPass
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

        Hi SetBetterPass,
        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?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          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

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SetBetterPass
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            1
            • CAD_codingC Offline
              CAD_codingC Offline
              CAD_coding
              wrote on last edited by
              #6

              @Code_ReaQtor Thank You for that information!
              @SetBetterPass ohhh I didnt know that.
              Maybe it was foolish of me to forget such an obvious thing :P
              Thanks!

              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