[SOLVED] QWidget - Disable child focus



  • I was wondering if there is an attribute that can be set to disable child focus on a QWidget.

    Let me explain, I would like my childs (QLabel, QPushButton, etc.) not to keep focus after they received it. For example, if I click a QPushButton, I want it to loose focus right after.
    Reason : My main QWidget has "hotkeys" that are only fired when it has focus.

    http://forum.maximumtrainer.com:4567/topic/31/workout-controls

    Code for hotkeys (event filter set on QDialog) :

      bool WorkoutDialog::eventFilter(QObject *watched, QEvent *event) {
    
        Q_UNUSED(watched);
        if(event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return ) {
                start_or_pause_workout();
                return true; // mark the event as handled
            }
            else if (keyEvent->key() == Qt::Key_Up) {
                emit increaseDifficulty();
                return true;
            }
            else if (keyEvent->key() == Qt::Key_Down) {
                emit decreaseDifficulty();
                return true;
            }
            else if (keyEvent->key() == Qt::Key_Backspace) {
                lapButtonPressed();
                return true;
            }
        }
        return false;
    }


  • Solved by setting this on all child I don't want focus
    Works for now. Probably an easier way i'm missing?
    e.g : QWidget.childs->setFocusPolicy(Qt::NoFocus);

    ui->pushButton_calibrateFEC->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_calibratePM->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_config->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_minimize->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_expand->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_start->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_lap->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_exit->setFocusPolicy(Qt::NoFocus);

  • Lifetime Qt Champion

    Hi,

    What about findChildren of type QPushButton ? You can then use a loop to set the focus policy


Log in to reply
 

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