Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    How to catch Ctrl+C on a widget?

    General and Desktop
    6
    11
    19774
    Loading More Posts
    • 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.
    • K
      kibsoft last edited by

      Hi everyone! I have a treeview and I wrote an eventfilter that catches Ctrl+C combination:

      @ if (event->type() == QEvent::KeyPress) {
      QKeyEvent *ke = static_cast<QKeyEvent *>(event);

      if (ke->matches(QKeySequence::Copy)) {
      if (copyCurrentValue()) {
      event->accept();
      } e.t.c.@

      When I push Ctrl+Ins it works nice, but if I push Ctrl+C then ke->key() returns 16777249 keycode.

      Any ideas?

      1 Reply Last reply Reply Quote 0
      • M
        milot.shala last edited by

        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 Reply Quote 0
        • I
          ixSci last edited by

          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 Reply Quote 0
          • K
            kibsoft last edited by

            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 Reply Quote 0
            • M
              milot.shala last edited by

              [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 Reply Quote 0
              • K
                kibsoft last edited by

                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 Reply Quote 0
                • M
                  milot.shala last edited by

                  You are welcome kibsoft :)

                  1 Reply Last reply Reply Quote 0
                  • K
                    kibsoft last edited by

                    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 Reply Quote 0
                    • D
                      dcortesi last edited by

                      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 Reply Quote 0
                      • P
                        paulrichards321 last edited by paulrichards321

                        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 Reply Quote 1
                        • H
                          hooooo last edited by

                          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 Reply Quote 0
                          • First post
                            Last post