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 to catch Ctrl+C on a widget?
QtWS25 Last Chance

How to catch Ctrl+C on a widget?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 22.5k 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.
  • M Offline
    M Offline
    milot.shala
    wrote on 13 Aug 2010, 11:36 last edited by
    #2

    Have you tried overriding keyPressEvent, the code below works for me on a QMainWindow:

    @void MainWindow::keyPressEvent(QKeyEvent *e) {
    if(e->type() == QKeyEvent::KeyPress) {
    if(e->matches(QKeySequence::Copy)) {
    setWindowTitle("aa"); // when Ctrl+C is pressed window title changes to aa (for demo purposes)
    } else {
    setWindowTitle("bb"); // when other keys are pressed, window title changes to bb (for demo purposes)
    }
    }
    }@

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ixSci
      wrote on 13 Aug 2010, 11:55 last edited by
      #3

      you can try QList<QKeySequence> QKeySequence::keyBindings ( StandardKey key ) function to retrieve key bindings appropriate to your platform. Maybe your platform has only (ctrl+ins) binding.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kibsoft
        wrote on 13 Aug 2010, 12:05 last edited by
        #4

        I have tried keyBindings function and it returns Ctrl+C and Ctrl+Ins. Milot Shala, I did it, but the result was the same as with eventFilter :(

        P.S. In headerview Ctrl+C works, but Ctrl+Ins doesn't works. I don't understand what is it..

        1 Reply Last reply
        0
        • M Offline
          M Offline
          milot.shala
          wrote on 13 Aug 2010, 12:48 last edited by
          #5

          [quote author="kibsoft" date="1281701124"]I have tried keyBindings function and it returns Ctrl+C and Ctrl+Ins. Milot Shala, I did it, but the result was the same as with eventFilter :(

          P.S. In headerview Ctrl+C works, but Ctrl+Ins doesn't works. I don't understand what is it..[/quote]

          Where are you overriding the keyPressEvent virtual method?

          I modified mine when I press Ctrl+C to set the Window title to currently selected tree view node such as in picture below (I am using QFileSystemModel):

          !http://web7.twitpic.com/img/144954784-8f19fe179f40f269283c021d8028ac42.4c6541d0-full.png(window)!

          and the code is almost the same:

          @void MainWindow::keyPressEvent(QKeyEvent *e) {
          if(e->type() == QKeyEvent::KeyPress) {
          if(e->matches(QKeySequence::Copy)) {
          QString a = ui->treeView->currentIndex().model()->data(ui->treeView->currentIndex()).toString();
          setWindowTitle(a);
          e->accept();
          }
          }
          QMainWindow::keyPressEvent(e);
          }@

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kibsoft
            wrote on 13 Aug 2010, 13:05 last edited by
            #6

            I have just tried to create new app and it works correct there. So this bug somewhere in the current project. I'll look. Thank you Milot :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              milot.shala
              wrote on 13 Aug 2010, 13:23 last edited by
              #7

              You are welcome kibsoft :)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kibsoft
                wrote on 13 Aug 2010, 13:39 last edited by
                #8

                KeyEvent's modifiers() returns Qt::ControlModifier and key() returns Qt::Key_Control when I push Ctrl+C..It is so strange..

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dcortesi
                  wrote on 15 Mar 2012, 21:01 last edited by
                  #9

                  bq. KeyEvent’s modifiers() returns Qt::ControlModifier and key() returns Qt::Key_Control when I push Ctrl+C..It is so strange..

                  Just in case anyone finds this old post in a search (as I did), probably what confused kibsoft was that a keyPressEvent handler gets multiple events for a multi-key input. When the user hits control-x, the handler sees the above code meaning "control was pressed". Then immediately after comes another event with ControlModifier set and a key value of x.

                  If the input is ctl-alt-x, then you would see three events as the user mashes the keys. This is why to test the event with matches() and ignore all that don't match.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    paulrichards321
                    wrote on 26 Dec 2020, 22:15 last edited by paulrichards321
                    #10

                    I stumbled on this topic doing a google search about how to catch Ctrl-c on a gui app (for instance a gui program simulating a Ctrl-c interrupt like a terminal emulater), and after doing some research came up with this for Qt5 which is a bit different because Ctrl-c on a Mac is not equal to: event->matches(QKeySequence::Copy).

                    If you are simulating a standard "Copy" content key (like word processor/text editor cut/copy/paste) combo then do indeed use event->matches(QKeySequence::Copy) but if you are trying to detect an actual Ctrl-c (like Ctrl-c terminal program interrupt) and you want something portable between Mac OS and something else (tested on both Mac OS High Sierra and Linux/Ubuntu 18.04):

                    // Respond to Ctrl-c:
                    void SomeWidget::keyPressEvent(QKeyEvent *event)
                    {
                        // the MetaModifier is actually the control button on Mac OS X (at least on recent Mac OSes. Tested on High Sierra)
                        #ifdef Q_OS_MAC  
                        // if compiling on Mac OS...
                        if (event->key()==Qt::Key_C && (QGuiApplication::keyboardModifiers() & Qt::MetaModifier))
                        #else 
                        // If compiling on Linux (tested on Ubuntu 18.04), Windows, everything else...
                        if (event->key()==Qt::Key_C && (QGuiApplication::keyboardModifiers() & Qt::ControlModifier))
                        #endif
                        {
                            // Ctrl-c hit, do something here to process the key combo
                        }
                    }
                    
                    1 Reply Last reply
                    1
                    • H Offline
                      H Offline
                      hooooo
                      wrote on 28 Dec 2022, 09:06 last edited by
                      #11

                      I have a QGraphicsView that I bind on a copied GraphicsItem using ctrl_c. When I try to use ctrl_c in QTreeWidget, it seems that it cannot be used because it is occupied. Does anyone know how to make the ctrl_c of QTreeWidget take effect?

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        icy_moez
                        wrote on 27 Dec 2024, 08:17 last edited by
                        #12

                        Maybe it's because the toolbar of the software has shortcuts like ‘ctrl+c,ctrl+e’ or something like that, where you need to press ctrl+c first and then press other keys to make it work.

                        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