QShortCut and QSpinBox conflict

I am writing an application, I use my own shortcut. It looks like this:

myShortcut= new QShortcut(Qt::SHIFT + Qt::Key_B,this);
connect(myShortcut, SIGNAL(activated()), this, SLOT(setCameraBack()));

I defined it in the constructor of the main widget, and it works fine until I click on one of the scroll buttons, which are also located on the main widget. After that, my shortcut stops working and it doesn't work until I click a button or checkbox. When I do this, everything is fine again. I would like to add that after I press the spinbox button, it seems to be “active” (because the cursor is still “blinking” on it) until I press one of the other buttons. Do you know what is wrong? Is this some kind of process or event problem? Thanks for all the answers ~ Marwroc

+5
source share
3 answers

"" Qt , .

QSpinBox , QShortcut . , QSpinBox. , Qt:: WidgetWithChildrenShortcut Qt:: ApplicationShortcut QShortcut:: setContext QShortcut.

+4

, ShortcutOverride. , , .

: https://wiki.qt.io/ShortcutOverride

Qt

QAbstractSpinBox::event(QEvent *event)
{
    Q_D(QAbstractSpinBox);
    switch (event->type()) {
    ...
    case QEvent::ShortcutOverride:
        if (d->edit->event(event))
            return true;
        break;
    ...
    }
    return QWidget::event(event);
}

QAbstractSpinBox . QLineEdit QLineControl. qt/src/gui/widgets/qlinecontrol.cpp

    case QEvent::ShortcutOverride:{
        if (isReadOnly())
            return false;
        QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
        if (ke == QKeySequence::Copy
            || ke == QKeySequence::Paste
            || ke == QKeySequence::Cut
            || ke == QKeySequence::Redo
            || ke == QKeySequence::Undo
            || ke == QKeySequence::MoveToNextWord
            || ke == QKeySequence::MoveToPreviousWord
            || ke == QKeySequence::MoveToStartOfDocument
            || ke == QKeySequence::MoveToEndOfDocument
            || ke == QKeySequence::SelectNextWord
            || ke == QKeySequence::SelectPreviousWord
            || ke == QKeySequence::SelectStartOfLine
            || ke == QKeySequence::SelectEndOfLine
            || ke == QKeySequence::SelectStartOfBlock
            || ke == QKeySequence::SelectEndOfBlock
            || ke == QKeySequence::SelectStartOfDocument
            || ke == QKeySequence::SelectAll
            || ke == QKeySequence::SelectEndOfDocument) {
            ke->accept();
        } else if (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier
                   || ke->modifiers() == Qt::KeypadModifier) {
            if (ke->key() < Qt::Key_Escape) {
                ke->accept();
            } else {
                switch (ke->key()) {
                case Qt::Key_Delete:
                case Qt::Key_Home:
                case Qt::Key_End:
                case Qt::Key_Backspace:
                case Qt::Key_Left:
                case Qt::Key_Right:
                    ke->accept();
                default:
                    break;
                }
            }
        }
    }

, .

, .

bool MySpinBox::event(QEvent *event)
{
    if( event->type() == QEvent::ShortcutOverride && !isReadOnly() )
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        // Ignore 'B' shortcuts
        if( keyEvent->key() == Qt::Key_B )
        {
            Q_ASSERT( !event->isAccepted() );
            return true;
    }
    return QSpinBox::event(event);
}
+2

Have you tried MySpinBox -> setFocusPolicy (Qt::NoFocus)?

+1
source

All Articles