I have a QTableWidget generated by QTDesigner in which I want to set some custom key press events.
when current cell is in edit mode and if user presses Tab key, it moves to next cell in edit mode but i just want it move to next cell selected and not in edit mode.
when current cell is in edit mode and if user presses Left, right, up, down- it should move to corresponding cell in select mode again and not in edit mode.
both of this works well right now if current cell is just selected and not in edit mode. but how can i set this customized event?
Thanks!!
Code Generated by UI Designer:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(428, 285)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(20, 20, 391, 231))
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed|QtWidgets.QAbstractItemView.SelectedClicked)
self.tableWidget.setRowCount(5)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName("tableWidget")
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.verticalHeader().setVisible(False)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Age"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "City"))
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_())
My Script:
from PyQt5 import QtWidgets, QtCore
from demo import Ui_MainWindow
class DemoTable(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(DemoTable, self).__init__()
self.setupUi(self)
#KeyPressEvent
self.tableWidget.keyPressEvent = self.KeyPressed
def KeyPressed(self,event):
if event.key() == QtCore.Qt.Key_Left:
print('Left Key Pressed')
elif event.key() == QtCore.Qt.Key_Right:
print('Right Key Pressed')
elif event.key() == QtCore.Qt.Key_Tab:
print('Tab Key Pressed')
return QtWidgets.QTableWidget.keyPressEvent(self.tableWidget, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
demowindow = DemoTable()
demowindow.show()
sys.exit(app.exec_())