I have been working on PyQt
focus method for different widgets whenever Enter key pressed instead of conventional Tab key.
Furthermore, I am able to set focus to the widget which I intended upon Enter key event. However, whenever the focus is on QComboBox
or QRadioButton
, these two widgets don't seems to highlight like QLineEdit
or QPushButton
widgets.
I know that I have to set focus policy to StrongFocus
and I have tried that and several other methods but unable to fix this issue.
Moreover, this behaviour is working perfectly fine and combo box or radio button seems to highlight as well with the Tab key.
Please find below my tried code until now and also snapshot for the actual results.
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Combo_and_Radio_Focus(QDialog):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Combo Box Focus Test")
# setting geometry
self.setGeometry(600, 200, 650, 400)
# Some widgets for testing
self.le1 = QLineEdit(self)
self.le1.setFixedSize(100, 25)
self.le2 = QLineEdit(self)
self.le2.setFixedSize(100, 25)
self.cbo1 = QComboBox(self)
self.cbo1.setFixedSize(100, 25)
self.cbo1.setStyleSheet('QComboBox {background-color: white;}')
self.cbo1.setFocusPolicy(Qt.StrongFocus)
self.cbo1.addItems(["", "Item1", "Item2", "Item3"])
self.cbo2 = QComboBox(self)
self.cbo2.setFixedSize(100, 25)
self.cbo2.setStyleSheet('QComboBox {background-color: white;}')
self.cbo2.setFocusPolicy(Qt.StrongFocus)
self.cbo2.addItems(["", "Item1", "Item2", "Item3"])
self.RB1 = QRadioButton("1")
self.RB1.setChecked(True)
self.RB1.setFixedSize(100, 20)
self.RB1.setFocusPolicy(Qt.StrongFocus)
self.RB2 = QRadioButton("2")
self.RB2.setFixedSize(100, 20)
self.RB2.setFocusPolicy(Qt.StrongFocus)
self.vbl = QVBoxLayout()
self.vbl.addWidget(self.le1)
self.vbl.addWidget(self.le2)
self.vbl.addWidget(self.cbo1)
self.vbl.addWidget(self.cbo2)
self.vbl.addWidget(self.RB1)
self.vbl.addWidget(self.RB2)
self.setLayout(self.vbl)
# showing all the widgets
self.show()
def keyPressEvent(self, qKeyEvent):
if int(qKeyEvent.modifiers()) == QtCore.Qt.AltModifier:
qKeyEvent.ignore()
return
if qKeyEvent.key() == QtCore.Qt.Key_Return or qKeyEvent.key() == QtCore.Qt.Key_Enter:
QWidget.focusNextChild(self)
else:
super().keyPressEvent(qKeyEvent)
App = QApplication(sys.argv)
App.setStyle(QStyleFactory.create('Fusion'))
# create the instance of our Window
combo_and_Radio_Focus = Combo_and_Radio_Focus()
# start the app
sys.exit(App.exec())
LineEdit1_Highlighted Below
ComboBox1_Dont_Highlight Below
When the Tab is pressed to make the focus change then the application sets the Qt.WA_KeyboardFocusChange
attribute in the window and that is used by the QStyle to make the border painting but since this is not the case then that attribute has to be set directly:
def keyPressEvent(self, qKeyEvent):
if qKeyEvent.modifiers() & QtCore.Qt.AltModifier:
qKeyEvent.ignore()
elif qKeyEvent.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
self.focusNextChild()
self.window().setAttribute(Qt.WA_KeyboardFocusChange)
else:
super().keyPressEvent(qKeyEvent)