Important: Please read the Qt Code of Conduct -

What does "copy()" do in signal / slot ?

  • Here is a simple question .
    Using QtDesigner "Edit signal / sots" drag and drop GUI.

    How do I find out what the actual "pressed" & "copy" do ?
    "pressed" is obvious, but what does "copy " do ?

    What document to read ?

    " 035a0040-123c-4453-a45f-d7fdeacec83d-image.png

  • @AnneRanch hi
    you are connecting "pressed" signal of the button to the "copy" slot of the plainTextEdit

  • Moderators


    What document to read ?

    The documentation of the classes you connect.
    In this case, as @LeLev said, you 're connecting a signal of QPushButton, so the docs are here:

    pressed() in particular is an inherited signal, so you can go to the list of all members (including inherited ones) through a link near the top on that doc page and you'll land here:

    Same with the copy() slot. You go to the related class, QPlainTextEdit in this case:

    Unrelated tip - buttons have a pressed(), released() and clicked() signals. It is most common to connect actions to clicked() signal, which is triggered after a press and following release on the button. You usually don't want to trigger actions directly on press. On most platforms it's uncommon or down right frowned upon. For example it should be possible to press, move the mouse out of the button while holding mouse pressed and then release outside to cancel the action. This is handled by connecting to clicked() signal, which won't be triggered in such case. If you connect to pressed() instead you're executing the action right away. In some scenarios it can also result in focus transfer problems when you're showing dialog windows as the release happens when the focus is already transferred to the new window so it's just safer to avoid that problem and connect to clicked().

  • Thanks,
    exactly what I was looking for.

    Now for the real
    This is what the doc saiz about this copy function

    Copies any selected text to the clipboard.

    So prior to invoking the "connect" what would be a sample user's way selecting the text so the "connect" will copy it to clipboard ?

    Perhaps not so simple as "highlight status bar text" ?

    Or "selecting text" in code ?

    As far as using "pressed" instead of "click" - no particular reason doing so. Perhaps "faster" response...

  • Moderators

    Sorry, I'm not sure I understand the question? Are you asking how to programmatically select and copy some text?

    QPlainTextEdit already implements by default the standard "text field" behavior i.e. it has text selection and copies it to the clipboard (by invoking copy()) when you press a standard copy shortcut of your platform (ctrl+c on Windows/Linux). In addition to that you can connect the copy() slot to any other ui element's signals e.g. a copy action in a menu, on a toolbar or to a button like you have above.

  • @AnneRanch said in What does "copy()" do in singal / slot ?:

    Copies any selected text to the clipboard.

    I am questioning the "selected text" part.
    If I highlight anything in this forum frame , doI perform such selection?
    I am not sure if that works in Qt "text' widget .
    I can add text to "plain text edit " object , but I woudl not call it "selected text". However, can I highlight it by using mouse WITHOUT additional code ?

    If so - then will I have " any selected text" to "copy" to clipboard , per doc?
    I realize this is pretty academical to ask, but cannot come up with better example.

  • Moderators

    If I highlight anything in this forum frame , do I perform such selection?


    I am not sure if that works in Qt "text' widget .

    Yes, it works exactly like that.

    can I highlight it by using mouse WITHOUT additional code

    Yes, you can highlight it with mouse or keyboard, just like you would in a notepad app, in a browser or any other typical text control. It's built-in. No additional code is needed:



    Just QPlainTextEdit without any additional code. The "selected text" is the part highlighted in blue. Standard copy, paste, cut, select all and even undo actions with their shortcuts work out of the box. There's also a right click menu with all these actions built-in. No additional code.

Log in to reply