Focus Jumps To Next Widget When Disabling The Current Widget
-
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_HMainWindow.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> -
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_HMainWindow.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>@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
-
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_HMainWindow.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>@AbhiVarma
You can prevent any widget to get the focus with:ui->comboBox1->setEnabled(false); setFocus(Qt::OtherFocusReason);