Reputation: 53
My pyqt layout contain several widget including qtableview
, qlineedit
, qpushbutton
etc. I can navigate through widgets using Tab key, however I am trying to use Enter key (Return) to navigate through 'qtableview'.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QShortcut
from PyQt5.QtGui import QKeySequence
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
# label name
self.label_name = QtWidgets.QLabel(self.centralwidget)
self.label_name.setText( "Name")
self.verticalLayout.addWidget(self.label_name)
# line edit
self.lineEdit_name = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_name.setObjectName("lineEdit_name")
self.verticalLayout.addWidget(self.lineEdit_name)
#tableview
self.tableView = QtWidgets.QTableView(self.centralwidget)
# create tablemodel
self.table_model = QStandardItemModel()
self.table_model.setHorizontalHeaderLabels( ["Item", "Unit", "unitPrice", "Unit*unitPrice"])
self.table_model.insertRow(0)
self.tableView.setModel(self.table_model)
# keyboard shortcut activation
self.shortcut = QShortcut(QKeySequence("Return"), MainWindow)
self.shortcut.activated.connect(self.on_enter)
self.verticalLayout.addWidget(self.tableView)
MainWindow.setCentralWidget(self.centralwidget)
def on_enter(self):
print('hasFocus '+ str(self.tableView.hasFocus()))
if(self.tableView.hasFocus()):
self.tableView.focusNextChild()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
tableview.hasFocus()
is used, in order to check whether focus is on tableview
or not. The issue I am facing here is that, when I click on any cell self.tableView.hasFocus()
return False
, which dose not activate self.tableView.focusNextChild()
and I can not naviagte further using Enter key. If I don't use if(self.tableView.hasFocus()):
it may leads to naviagtion through other widgets like qlineedit
. How can I use Enter key to navigate through my table cell?
Test 1 : curser on lineEdit, press Enter on Keyboard => print: hasFocus False
Test 2 : selected the table cell. press Enter key => print: hasFocus True
Test 3 : clicked on cell (curser visible), press Enter key=> print: hasFocus False
Here I am satisficed with test 1 and test2. However, in test 3 the hasFocus()
shows False
, so when I press Enter key the courser dose not come out of table from edit mode. Then I need to use tab key to come out of edit mode, is it possible to get out of edit mode by pressing Enter key
Upvotes: 2
Views: 1362
Reputation: 120598
When a cell is being edited, a separate editing widget is shown which takes the keyboard focus. Your example can be fixed by testing whether this widget is open:
def on_enter(self):
if (self.tableView.hasFocus() or
self.tableView.isPersistentEditorOpen(self.tableView.currentIndex())):
self.tableView.focusNextChild()
(NB: isPersistentEditorOpen requires Qt >= 5.10)
Upvotes: 2