Thanks to the following post (Python Qt: How to catch "return" in qtablewidget) I found how to subclass QTableWidget
to connect an ENTER key event. My problem is that from this subclass, I can't find the way to reach all the other widgets.
Here is what I did.
Using QtDesigner I built this simple interface with a QTableWidget
and a TextField
. I promoted the QTableWidget
to my own MyTableWidget
(from the above post).
The main code is as follow:
import sys
from PyQt4 import QtGui, QtCore, Qt
from my_interface import Ui_MainWindow
class AppTemplateMain(QtGui.QMainWindow):
variable1 = 'my variable 1'
#initialize app
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__=="__main__":
app = QtGui.QApplication(sys.argv)
myapp = AppTemplateMain()
myapp.show()
exit_code=app.exec_()
sys.exit(exit_code)
and the subclass of QTableWidget
is defined in its own file mytablewidget.py
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
class MyTableWidget(QtGui.QTableWidget):
def __init__(self, parent=None):
self.parent = parent
super(MyTableWidget, self).__init__(parent)
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Return or key == Qt.Key_Enter:
print('clicked enter')
else:
super(MyTableWidget, self).keyPressEvent(event)
When I click in the table, I get as expected the message 'clicked enter' printed as expected. But I want to have access to the other widgets of the GUI from this subclass.... and to the variable1
defined in AppTemplateMain
.
I feel like I'm missing something obvious but can't figure out what. Can someone help me here.
Thanks a lot.