Shortcut QLineEdit



  • I need created shortcut grabber, such as used in Qt Creator keyboard mapping. Can any body tell, if something already exist in free use. Or point me where to find, source of Qt Creator shortcut edit class grab ?



  • Create a class which inherits from QLineEdit.
    And then , reimplement keyPressEvent()

    This is a quick exemple !
    @
    GrabLineEdit::GrabLineEdit(QWidget *parent) :
    QLineEdit(parent)
    {
    }

    void GrabLineEdit::keyPressEvent(QKeyEvent *event)
    {
    QString grab;
    switch ( event->modifiers())
    {
    case Qt::ShiftModifier : grab.append("Shift+");break;
    case Qt::ControlModifier : grab.append("Ctrl+");break;
    case Qt::AltModifier : grab.append("Alt+");break;
    }

        grab.append(event->text());
        setText(grab);
    
    // QLineEdit::keyPressEvent(event);
    

    }
    @



  • ho ho ))) great quick example... thx



  • But some thing strange comes, when Ctrl pressed no key text
    returned by QKeyEvent::text()



  • solve problem with Ctrl and modify keyPressEvent function
    @
    void keyPressEvent(QKeyEvent *event)
    {
    QString grab;
    int modifiers = event->modifiers();
    if(modifiers & Qt::ControlModifier)
    grab.append("Ctrl+");
    if(modifiers & Qt::ShiftModifier)
    grab.append("Shift+");
    if(modifiers & Qt::AltModifier)
    grab.append("Alt+");
    grab.append(event->key());
    setText(grab);
    // QLineEdit::keyPressEvent(event);
    }
    @

    Edit: please use @ tags around code sections; Andre



  • Sorry for digging it out :)

    Is there any way to solve this issue without inheriting QLineEdit?

    P.S. I already built my ui so reimplementing a new class will be hard to me.


  • Lifetime Qt Champion

    eventFilter comes to mind if you don't want to subclass


Log in to reply
 

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