Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QWidget - Disable child focus
QtWS25 Last Chance

[SOLVED] QWidget - Disable child focus

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 6.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    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;
    }
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by maximus
      #2

      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);
      

      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved