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. How can I replace the default action for QTextEdit's Ctrl + Z shortcut?
Forum Updated to NodeBB v4.3 + New Features

How can I replace the default action for QTextEdit's Ctrl + Z shortcut?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 750 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.
  • Z Offline
    Z Offline
    ZeHgS
    wrote on last edited by ZeHgS
    #1

    Hi!

    I want to replace my QTextEdit's default Ctrl + Z action with one of my own. For some reason overriding the function did not work.

    If I simply add a shortcut as I would normally do if it didn't already exist, it works for, say, "Ctrl+M" but not "Ctrl+Z". I am assuming this is because it is already assigned. How can I disable it?

    Thanks a lot!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which function did you override ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZeHgS
        wrote on last edited by ZeHgS
        #3

        Thanks a lot for replying!

        "undo". Is that not the right one?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That method is not virtual so you can't do that.

          Depending on what you wan't to do, you can re-implement the keyPressEvent method and catch there the QKeySequence::Undo.

          Just take into account that your users will likely be very surprised if undo does not do its job in a standard fashion.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZeHgS
            wrote on last edited by ZeHgS
            #5

            Thanks for the advice. This will only be used internally!

            Do I just do

            if event.key() == QKeySequence.Undo:
            

            or do I check for Z and the Ctrl modifier?

                    if event.key() == Qt.Key_Z:
                        modifiers = QApplication.keyboardModifiers()
            
                        if modifiers == Qt.Key_Control:
                            print('here')
            

            Neither one worked, unfortunately

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Even if internal, your coworkers may come at you with pitchforks and torches ;-)

              You should use the matches function.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZeHgS
                wrote on last edited by ZeHgS
                #7

                Unfortunately the following did not work either. The two key checks below work just fine:

                    def key_pressed_event(self, event):
                        if event.matches(QKeySequence.Undo):
                            print('here')
                
                        if event.key() == Qt.Key_Shift:
                            if self.underMouse():
                                self.toggle_paragraph_select()
                
                        elif event.key() == Qt.Key_Alt:
                            if self.underMouse():
                                self.toggle_clicked_word()
                

                key_pressed_event is called by keyPressEvent in my main window.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You should implement that in a subclass of QTextEdit.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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