2

What is the easiest way to allow user to input a key combination? It would basically look like QLineEdit field but it would accept key combinations only.

For example, there is something already in the Qt Designer where you can assign a shortcut for an Action. My requirement is exactly the same. TIA

Edit Action - assigning a shortcut

NG_
  • 6,895
  • 7
  • 45
  • 67
warunanc
  • 2,221
  • 1
  • 23
  • 40

3 Answers3

2

Qt Creator is actually open source. So if you want exactly what that dialog does, you can peek in and see how they do it.

The implementation of that "Edit Action" dialog is in a library called "QtTools". Here's the header and the source file:

actioneditor_p.h

actioneditor.cpp

...but it looks to be a bit indirect, and bootstrapped onto the form engine itself. You'd have to dig a while to get to the part where it actually captures the key sequence. Casual browsing sans debugger suggests it is QtKeySequenceEdit implemented in these files:

qtpropertybrowserutils_p.h

qtpropertybrowserutils.cpp

2

From Qt 5.2 onwards, you can use QKeySequenceEdit for this purpose

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
0

I found a few alternative ways to achieve my requirement. I want a QLineEdit like input field, which will capture key combinations that adhere to following formats:

  • Alt+Ctrl+Shift+X
  • Alt+Ctrl+X
  • Alt+Shift+X
  • Ctrl+Shift+X

The easiest way to do this is by sub-classing QLineEdit& re-implementing keyPressEvent( QKeyEvent * event ) function. My header file and cpp file look like this. Anyone can manipulate the logic inside keyPressEvent to suit their requirement.

QLineEditHotKey.h

#include <QLineEdit>

class QLineEditHotKey: public QLineEdit
{
public:
    QLineEditHotKey( QWidget* pParent = NULL);
    ~QLineEditHotKey(){}
protected:
    void keyPressEvent ( QKeyEvent * event );
};

QLineEditHotKey.cpp

QLineEditHotKey::QLineEditHotKey(QWidget* pParent):QLineEdit(pParent)
{
}

void QLineEditHotKey::keyPressEvent( QKeyEvent * event )
{
    int keyInt = event->key(); 
    Qt::Key key = static_cast<Qt::Key>(keyInt); 

    // Handle unknown keys
    if( key == Qt::Key_unknown ) 
        return; 

    // Pressing Esc or Backspace will clear the content
    if( key == Qt::Key_Escape || key == Qt::Key_Backspace )
    {  
        setText(NULL);
        return;
    } 

    // Empty means a special key like F5, Delete, Home etc
    if( event->text().isEmpty() )
        return;

    // Checking for key combinations
    Qt::KeyboardModifiers modifiers = event->modifiers(); 

    if(modifiers.testFlag(Qt::NoModifier)) 
        return;
    if(modifiers.testFlag(Qt::ShiftModifier)) 
        keyInt += Qt::SHIFT; 
    if(modifiers.testFlag(Qt::ControlModifier)) 
        keyInt += Qt::CTRL; 
    if(modifiers.testFlag(Qt::AltModifier)) 
        keyInt += Qt::ALT; 

    setText( QKeySequence(keyInt).toString(QKeySequence::NativeText) );
}

This question was very helpful in finding the solution.

Community
  • 1
  • 1
warunanc
  • 2,221
  • 1
  • 23
  • 40