How to catch Ctrl+C on a widget?



  • 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?



  • 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)
    }
    }
    }@



  • 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.



  • 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 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);
    }@



  • 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 :)



  • You are welcome kibsoft :)



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.