Search code examples
c++qtqt5qkeyeventqkeysequence

Limit QKeySequence/QKeySequenceEdit to only one shortcut


Is it possible to limit QKeySequence to show only one shortcut in QKeySequenceEdit? Currently now it supports up to 4 shortcuts. My application supports key sequences of only one shortcut, e.g. Ctrl+A or Ctrl+C and not e.g. Ctrl+A, D or Ctrl+C, X, Z.

Is it possible to limit QKeySequence or QKeySequenceEdit to just one key sequence?


Solution

  • Solved it, not the best solution but quick...If you want something more customize, I think you have to build it yourself...

    customkeysequenceedit.h:

    #ifndef CUSTOMKEYSEQUENCEEDIT_H
    #define CUSTOMKEYSEQUENCEEDIT_H
    
    #include <QKeySequenceEdit>
    
    class QKeyEvent;
    
    class CustomKeySequenceEdit : public QKeySequenceEdit
    {
        Q_OBJECT
    
    public:
        explicit CustomKeySequenceEdit(QWidget *parent = 0);
        ~CustomKeySequenceEdit();
    
    protected:
        void keyPressEvent(QKeyEvent *pEvent);
    };
    
    #endif // CUSTOMKEYSEQUENCEEDIT_H
    

    customkeysequenceedit.cpp:

    #include "customkeysequenceedit.h"
    
    #include <QKeyEvent>
    
    CustomKeySequenceEdit::CustomKeySequenceEdit(QWidget *parent) : QKeySequenceEdit(parent) { }
    
    CustomKeySequenceEdit::~CustomKeySequenceEdit() { }
    
    void CustomKeySequenceEdit::keyPressEvent(QKeyEvent *pEvent)
    {
        QKeySequenceEdit::keyPressEvent(pEvent);
    
        QKeySequence seq(QKeySequence::fromString(keySequence().toString().split(", ").first()));
        setKeySequence(seq);
    
    }