0

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.

prajmus
  • 3,171
  • 3
  • 31
  • 41
  • Where do you create the table in your main code ? You could create a custom signal in the table, emitted in keyPressEvent, and connect this signal to a function in the main code. – Mel Aug 20 '15 at 14:15
  • Also, I'm not sure you want to create variable1 like this. Right now it's a static variable, which has a particular behaviour. See the two first answer on this question: http://stackoverflow.com/questions/68645/static-class-variables-in-python – Mel Aug 20 '15 at 14:18
  • I tried to create my own keyPressEvent like this: QtCore.QObject.connect(self.ui.tableWidget,QtCore.SIGNAL("keyPressEvent()"), self.myYo) def myYo(self, event): print("my yo") No success ! – jean Bilheux Aug 20 '15 at 15:08

2 Answers2

0

You can create a custom signal in your table, that you emit whenever you want. Using the new style signal and slots:

class MyTableWidget(QtGui.QTableWidget):
    myCustomSignal=pyqtSignal()

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Return or key == Qt.Key_Enter:
            print('clicked enter')
            self.myCustomSignal.emit()  
        ...

In your main code, you connect to this signal:

self.ui.tableWidget.myCustomSignal(self.myFunction)

You can't connect directly to keyPressEvent because, as stated in its name, it's an event, not a signal.

Mel
  • 5,837
  • 10
  • 37
  • 42
0

Thanks tmoreau,

I just found the way to do it and here it is (in case some other people are as dump as me with pyqt)

mytablewidget.py rom PyQt4 import QtGui from PyQt4.QtCore import Qt

class MyTableWidget(QtGui.QTableWidget):

parent = None

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)

def setUI(self, ui_parent):
    self.ui = ui_parent

and inside main.py

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)

    self.ui.tableWidget.setUI(self)

if __name__=="__main__":
   app = QtGui.QApplication(sys.argv)
   myapp = AppTemplateMain()
   myapp.show()

   exit_code=app.exec_()
   sys.exit(exit_code)
  • Using a reference to the parent is another way to do it, using the method `parent()` of the table. You don't need to do `self.parent=parent`, and you really don't need the `setUI` function. – Mel Aug 20 '15 at 15:24
  • The table-widget already has the main-window set as its parent (i.e. in `__init__`). So you can just do `print(self.parent().variable1)` and it will just work. No additional code is required. – ekhumoro Aug 20 '15 at 15:33
  • Well not really because I promoted the QTablewidget to myTableWidget inside QtDesigner...that means, the parent is the widget itself. using setUI was the only way I could get a reference back to the main.py function. – jean Bilheux Aug 21 '15 at 20:35
  • @jeanBilheux. No. Promoting the widget will make no difference. All that happens is that an import line is added to the bottom of the ui module, and `QTablewidget` is replaced by `myTableWidget`. The parent of the promoted widget will be the object passed to `setupUi` (i.e. the main window). Widget promotion is designed to be completely transparent - the only thing that changes is the classname. – ekhumoro Aug 25 '15 at 22:10