Main windows suddenly stops accepting certian keystrokes



  • Here's the code:

    Main:

    #include <QApplication>
    #include "mainwindow.h"
    #include "treemapmodel.h"
    #include <QTreeView>
    #include "graph.h"
    #include <QHBoxLayout>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        Graph *originNode = new Graph("Root");
        TreeMapModel *model = new TreeMapModel(originNode);
    
        QTreeView treeView;
        treeView.setModel(model);
        w.shortCuts.setTreeMapModel(model);
        w.shortCuts.setTreeView(&treeView);
    
        w.setCentralWidget(&treeView);
    
        return a.exec();
    }
    

    MainWindow:

    #include "mainwindow.h"
    #include "treemapmodel.h"
    #include "ui_mainwindow.h"
    #include <QKeyEvent>
    #include <QTreeView>
    #include "hotkeymanager.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
        shortCuts.HandleKeyPress(event);
    }
    
    void MainWindow::keyReleaseEvent(QKeyEvent *event)
    {
        shortCuts.HandleKeyRelease(event);
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    The keyPressEvent is where I set the breakpoint, and it doesn't enter for Qt::Key_Q, but it does for Qt::Key_Alt

    Any ideas?


  • Qt Champions 2017

    Hi
    Maybe something else get focus and get the keys ?



  • Yes, but what could be doing that? That's the highest level of code. Wouldn't the code that takes the focus have to be there? Is there something QBuild is generating that I'm not aware of?


  • Qt Champions 2017

    @Trayon
    Any Widget in mainwindow that can receive keyboard focus , could steal it.
    What do you have in UI ?



  • Before, some garbage. But I removed everything until now only the central widget remains, and I'm getting the same behavior.


  • Qt Champions 2017

    @Trayon
    hmm that is odd then as mainwindow should be the only widget.
    Are you using
    http://doc.qt.io/qt-5/qshortcut.html
    in shortCuts ?



  • No, I created my own shortcut class. Wasn't aware of this one. These are all single key strokes. I may upgrade in the future.


  • Qt Champions 2017

    @Trayon
    Have you clean the build folder?
    If i make a default GUI project and do

    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
        qDebug() << "KEY";
    }
    
    

    It gets all keys so you must have something that like keys around :)



  • I'm thinking maybe there's a hidden delegate in the TreeView that's taking the inputs, because I did your test, and it seems that almost all non-text input gets registered.

    Can you see the project I uploaded?

    [0_1513715161369_Monstrocity.tar.gz](Uploading 100%)


  • Qt Champions 2017

    @Trayon
    Nope, upload is broken. sorry.
    Yes a treeview does take keys unless you set FocusPolicy to Nofocus
    alt text

    But what are you trying ?
    Any key goes to active control and some might not reach MainWindow at all.
    This is pr default.

    KeyPress in MainWindow is only called if it has focus. Its not a grabAll place.



  • How won't the keys reach MainWindow? Also, if "Key_Alt" works, why isn't 'q' working? Shouldn't it have focus for "Key_Alt" to work?

    As for what I'm trying to do, think emacs. Just trying to make a bunch of shortcuts so that in the future, the majority of the program runs with keys instead of mouse clicks.

    One last thing. I set the TreeView in code. I tried:

    treeView.setFocus(Qt::FocusPolicy::StrongFocus);
    

    Which doesn't work. What's the proper way to use it?


  • Qt Champions 2017

    @Trayon
    Its how events propagate in the system.
    Please see
    http://doc.qt.io/qt-5/eventsandfilters.html

    I think you will find that
    http://doc.qt.io/qt-5/qshortcut.html
    works much better for a key driven application.

    treeView.setFocus(Qt::FocusPolicy::StrongFocus);
    This is the default setting. if to disable it can have focus then
    treeView.setFocus(Qt::FocusPolicy::NoFocus);



  • Thanks, I'll give that a shot. Let's hope it behaves better.


  • Qt Champions 2017

    @Trayon
    Well QShortcut are hooked into the event system and
    can get keys even other widgets have focus so it will work far
    better than KeyPress in mainwindow as it only take 1 lineEdit to steal all keys from mainwindow.



  • Not sure if I should open a new thread for this, but for the sake of maintaining relevance, I think I'll follow up here.

    I have this new function that establishes a 'connect' with the new syntax so that I can use a lambda. Problem is there isn't much information on the web about this 'triggered' function:

    void HotKeyManager::setShortcuts(QWidget *parent)
    {
        QShortcut *keyQ = new QShortcut(parent);
    
        QObject::connect(keyQ, &QAction::triggered, [this](){
            QModelIndex selection = treeview->selectionModel()->currentIndex();
            model->clearChildCopies(selection);
        });
    }
    

    Could you please explain how I should be using this trigger?


  • Qt Champions 2017

    Hi
    The triggered is just like a buttons cliked().
    The signal that is emitted when its keys is being pressed.
    You can connect to any kind of slot like normally.

    So for a key driven app, i woul dmost like connect it to slots in mainwindow .


Log in to reply
 

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