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. Focus Jumps To Next Widget When Disabling The Current Widget
Forum Updated to NodeBB v4.3 + New Features

Focus Jumps To Next Widget When Disabling The Current Widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 338 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    AbhiVarma
    wrote on last edited by
    #1

    Hi, I have created a sample app where in the main window it has a treewidget with 2 columns namely, name and button. In this treeWidget i have added two combobox items and there is a push button in my mainwindow. What I'm doing is first selecting the first item so that focus is set to that item. Then on clicking push button it will set the 1st combobox to disabled. when disabled the focus is jumping to the next item. is there a way for the focus to remain there?. Below is my sample code.

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        AddItemsToTreeWidget();
        connect(ui->okButton, SIGNAL(clicked()), this, SLOT(OnOkButtonClicked()));
    }
    
    void MainWindow::OnOkButtonClicked()
    {
     ui->comboBox1->setEnabled(false);
    }
    
    void MainWindow::AddItemsToTreeWidget()
    {
        //DropDown 1
        QTreeWidgetItem* pItem = new QTreeWidgetItem(ui->treeWidget);
        pItem->setText(0, "DropDown 1");
        ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox1);
        for(int i = 0; i < 3; ++i)
        {
            ui->comboBox1->addItem(QString::number(i), QVariant(i));
        }
        ui->comboBox1->setEnabled(true);
    
        pItem = new QTreeWidgetItem(ui->treeWidget);
        pItem->setText(0, "DropDown 2");
        ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox2);
        for(int i = 0; i < 3; ++i)
        {
            ui->comboBox2->addItem(QString::number(i), QVariant(i));
        }
        ui->comboBox2->setEnabled(true);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        void AddItemsToTreeWidget();
    
    public slots:
        void OnOkButtonClicked();
    
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    

    MainWindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QTreeWidget" name="treeWidget">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>0</y>
          <width>256</width>
          <height>192</height>
         </rect>
        </property>
        <column>
         <property name="text">
          <string>Name</string>
         </property>
        </column>
        <column>
         <property name="text">
          <string>Button</string>
         </property>
        </column>
       </widget>
       <widget class="QPushButton" name="okButton">
        <property name="geometry">
         <rect>
          <x>430</x>
          <y>420</y>
          <width>113</width>
          <height>32</height>
         </rect>
        </property>
        <property name="text">
         <string>PushButton</string>
        </property>
       </widget>
       <widget class="QComboBox" name="comboBox1">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>280</y>
          <width>121</width>
          <height>31</height>
         </rect>
        </property>
        <property name="focusPolicy">
         <enum>Qt::WheelFocus</enum>
        </property>
       </widget>
       <widget class="QComboBox" name="comboBox2">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>320</y>
          <width>121</width>
          <height>31</height>
         </rect>
        </property>
        <property name="focusPolicy">
         <enum>Qt::WheelFocus</enum>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>24</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    
    jsulmJ M 2 Replies Last reply
    0
    • A AbhiVarma

      Hi, I have created a sample app where in the main window it has a treewidget with 2 columns namely, name and button. In this treeWidget i have added two combobox items and there is a push button in my mainwindow. What I'm doing is first selecting the first item so that focus is set to that item. Then on clicking push button it will set the 1st combobox to disabled. when disabled the focus is jumping to the next item. is there a way for the focus to remain there?. Below is my sample code.

      MainWindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          AddItemsToTreeWidget();
          connect(ui->okButton, SIGNAL(clicked()), this, SLOT(OnOkButtonClicked()));
      }
      
      void MainWindow::OnOkButtonClicked()
      {
       ui->comboBox1->setEnabled(false);
      }
      
      void MainWindow::AddItemsToTreeWidget()
      {
          //DropDown 1
          QTreeWidgetItem* pItem = new QTreeWidgetItem(ui->treeWidget);
          pItem->setText(0, "DropDown 1");
          ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox1);
          for(int i = 0; i < 3; ++i)
          {
              ui->comboBox1->addItem(QString::number(i), QVariant(i));
          }
          ui->comboBox1->setEnabled(true);
      
          pItem = new QTreeWidgetItem(ui->treeWidget);
          pItem->setText(0, "DropDown 2");
          ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox2);
          for(int i = 0; i < 3; ++i)
          {
              ui->comboBox2->addItem(QString::number(i), QVariant(i));
          }
          ui->comboBox2->setEnabled(true);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      

      MainWindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
          void AddItemsToTreeWidget();
      
      public slots:
          void OnOkButtonClicked();
      
      
      private:
          Ui::MainWindow *ui;
      };
      #endif // MAINWINDOW_H
      
      

      MainWindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <widget class="QTreeWidget" name="treeWidget">
          <property name="geometry">
           <rect>
            <x>10</x>
            <y>0</y>
            <width>256</width>
            <height>192</height>
           </rect>
          </property>
          <column>
           <property name="text">
            <string>Name</string>
           </property>
          </column>
          <column>
           <property name="text">
            <string>Button</string>
           </property>
          </column>
         </widget>
         <widget class="QPushButton" name="okButton">
          <property name="geometry">
           <rect>
            <x>430</x>
            <y>420</y>
            <width>113</width>
            <height>32</height>
           </rect>
          </property>
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
         <widget class="QComboBox" name="comboBox1">
          <property name="geometry">
           <rect>
            <x>20</x>
            <y>280</y>
            <width>121</width>
            <height>31</height>
           </rect>
          </property>
          <property name="focusPolicy">
           <enum>Qt::WheelFocus</enum>
          </property>
         </widget>
         <widget class="QComboBox" name="comboBox2">
          <property name="geometry">
           <rect>
            <x>20</x>
            <y>320</y>
            <width>121</width>
            <height>31</height>
           </rect>
          </property>
          <property name="focusPolicy">
           <enum>Qt::WheelFocus</enum>
          </property>
         </widget>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>800</width>
           <height>24</height>
          </rect>
         </property>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AbhiVarma said in Focus Jumps To Next Widget When Disabling The Current Widget:

      is there a way for the focus to remain there?

      A disabled widget can't have focus

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A AbhiVarma

        Hi, I have created a sample app where in the main window it has a treewidget with 2 columns namely, name and button. In this treeWidget i have added two combobox items and there is a push button in my mainwindow. What I'm doing is first selecting the first item so that focus is set to that item. Then on clicking push button it will set the 1st combobox to disabled. when disabled the focus is jumping to the next item. is there a way for the focus to remain there?. Below is my sample code.

        MainWindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            AddItemsToTreeWidget();
            connect(ui->okButton, SIGNAL(clicked()), this, SLOT(OnOkButtonClicked()));
        }
        
        void MainWindow::OnOkButtonClicked()
        {
         ui->comboBox1->setEnabled(false);
        }
        
        void MainWindow::AddItemsToTreeWidget()
        {
            //DropDown 1
            QTreeWidgetItem* pItem = new QTreeWidgetItem(ui->treeWidget);
            pItem->setText(0, "DropDown 1");
            ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox1);
            for(int i = 0; i < 3; ++i)
            {
                ui->comboBox1->addItem(QString::number(i), QVariant(i));
            }
            ui->comboBox1->setEnabled(true);
        
            pItem = new QTreeWidgetItem(ui->treeWidget);
            pItem->setText(0, "DropDown 2");
            ui->treeWidget->setItemWidget(pItem, 1, ui->comboBox2);
            for(int i = 0; i < 3; ++i)
            {
                ui->comboBox2->addItem(QString::number(i), QVariant(i));
            }
            ui->comboBox2->setEnabled(true);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        

        MainWindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
            void AddItemsToTreeWidget();
        
        public slots:
            void OnOkButtonClicked();
        
        
        private:
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
        
        

        MainWindow.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>800</width>
            <height>600</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <widget class="QTreeWidget" name="treeWidget">
            <property name="geometry">
             <rect>
              <x>10</x>
              <y>0</y>
              <width>256</width>
              <height>192</height>
             </rect>
            </property>
            <column>
             <property name="text">
              <string>Name</string>
             </property>
            </column>
            <column>
             <property name="text">
              <string>Button</string>
             </property>
            </column>
           </widget>
           <widget class="QPushButton" name="okButton">
            <property name="geometry">
             <rect>
              <x>430</x>
              <y>420</y>
              <width>113</width>
              <height>32</height>
             </rect>
            </property>
            <property name="text">
             <string>PushButton</string>
            </property>
           </widget>
           <widget class="QComboBox" name="comboBox1">
            <property name="geometry">
             <rect>
              <x>20</x>
              <y>280</y>
              <width>121</width>
              <height>31</height>
             </rect>
            </property>
            <property name="focusPolicy">
             <enum>Qt::WheelFocus</enum>
            </property>
           </widget>
           <widget class="QComboBox" name="comboBox2">
            <property name="geometry">
             <rect>
              <x>20</x>
              <y>320</y>
              <width>121</width>
              <height>31</height>
             </rect>
            </property>
            <property name="focusPolicy">
             <enum>Qt::WheelFocus</enum>
            </property>
           </widget>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>800</width>
             <height>24</height>
            </rect>
           </property>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
         </widget>
         <resources/>
         <connections/>
        </ui>
        
        
        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @AbhiVarma
        You can prevent any widget to get the focus with:

        ui->comboBox1->setEnabled(false);
        setFocus(Qt::OtherFocusReason);  
        
        1 Reply Last reply
        0

        • Login

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