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. Override default copy/paste from QTextEdit
QtWS25 Last Chance

Override default copy/paste from QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
qtexteditqeventshortcutoverridcopyshortcut
4 Posts 2 Posters 3.4k 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.
  • Z Offline
    Z Offline
    zespy
    wrote on 5 Dec 2018, 16:01 last edited by zespy 12 May 2018, 16:09
    #1

    Hello,

    I have a main window that has a QTextEdit and 3 QAction (copy, cut and paste), mapped on the default shortcuts (respectively ctrl+c, ctrl+x and ctrl+v). The goal of the actions is to copy/paste the contents of the QTextEdit, but with extra-work (the contents is copied as text/plain and application/json MIME types).

    However, QTextEdit already implements its own copy/paste functions, with the same shortcuts. When I hit ctrl+c, it is always the QTextEdit copy function that is used, instead of mine (so there is no application/json in the clipboard...).

    How can I change this behavior, and use my functions instead? I saw that there is a QEvent::ShortcutOverride event, but I don't know how to use it... can I use it in an QObject::eventFilter() function? Or should I inherit from QTextEdit and override its QWidget::keyPressEvent() method?

    The best for me would be to totally disconnect the default copy/paste from the QTextEdit and use only mine. I can inherit and try to mess up with the class (but how?), but the best solution would be that it works even with polymorphism (but the slots are not virtual).

    Thanks for your help :)

    edit: QTextEdit::createMimeDataFromSelection() is not an option for me as I also have a QLineEdit and I'd like the same behavior with it (to not use its default copy/paste functions).

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 5 Dec 2018, 16:33 last edited by
      #2

      You can use QTextEdit::actions to retrieve the list of actions already with the widget, then you can use QTextEdit::removeAction/QTextEdit::addAction to remove the old one and add your custom ones

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • Z Offline
        Z Offline
        zespy
        wrote on 5 Dec 2018, 19:02 last edited by
        #3

        Bien vu !

        I missed this function during my search... I'll try tomorrow and update this thread accordingly.

        Thanks :)

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zespy
          wrote on 6 Dec 2018, 09:15 last edited by
          #4

          Unfortunately, it doesn't seems to work... actions() returns me an empty list.

          I think I'll go for QTextEdit::createMimeDataFromSelection() because otherwise I won't be able to deal with drag & drop (I didn't think about it :/ )

          Thanks anyway!

          1 Reply Last reply
          0

          2/4

          5 Dec 2018, 16:33

          • Login

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