[英]How to link my QTableWidget subclass to my main program
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. 感谢以下文章( Python Qt:如何在qtablewidget中捕获“ return” ),我找到了如何对
QTableWidget
进行子类化以连接ENTER键事件。 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
. 使用QtDesigner,我使用
QTableWidget
和TextField
构建了这个简单的界面。 I promoted the QTableWidget
to my own MyTableWidget
(from the above post). 我将
QTableWidget
提升为我自己的MyTableWidget
(来自以上文章)。
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 QTableWidget
的子类在其自己的文件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
. 但是我想从此子类访问GUI的其他小部件...。以及
AppTemplateMain
定义的variable1
。
I feel like I'm missing something obvious but can't figure out what. 我觉得我缺少明显的东西,但不知道是什么。 Can someone help me here.
有人可以在这里帮我吗。
Thanks a lot. 非常感谢。
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. 您不能直接连接到
keyPressEvent
因为,正如其名称所述,它是事件,而不是信号。
Thanks tmoreau, 谢谢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) 我只是找到了一种方法,就在这里(以防其他人像我一样被pyqt转储)
mytablewidget.py rom PyQt4 import QtGui from PyQt4.QtCore import Qt 从PyQt4.QtCore导入Qt到mytablewidget.py从PyQt4导入QtGui
class MyTableWidget(QtGui.QTableWidget): 类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 在main.py中
import sys from PyQt4 import QtGui, QtCore, Qt from my_interface import Ui_MainWindow 从PyQt4导入sys从my_interface导入QtGui,QtCore,Qt导入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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.