Python 使用currentIndexChanged调用函数发出信号
有三节课。类Python 使用currentIndexChanged调用函数发出信号,python,python-3.x,pyqt5,signals-slots,Python,Python 3.x,Pyqt5,Signals Slots,有三节课。类TableQTableWidget以及一个信号和一个函数,用于通过单击事件获取行号。发送到另外两个类。脚本的这一部分很有前途 class Table(QtWidgets.QWidget): rownumber = QtCore.pyqtSignal(int) def __init__(self, parent=None): super(Table, self).__init__(parent) self.tableWidget = Qt
Table
QTableWidget以及一个信号和一个函数,用于通过单击事件获取行号。发送到另外两个类。脚本的这一部分很有前途
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(0, 3)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
第二个组合
类,带有QCOMBOX和一个信号以及两个向第三个类发送和传输信息的函数,QCOMBOX中的值会发生更改。要检查信号是否发出,请打印发出的信号
。脚本的这一部分似乎也可以正常工作,因为执行了打印代码
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Combo, self).__init__(parent)
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["1","2","3","4"])
self.combo.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot(int)
def setdatastrength(self, index):
value = self.combo[index]
self.dataupdate()
def dataupdate(self):
self.Toupdatedata.emit()
print('Signal emitted')
def get_data(self):
return tuple([self.combo.currentText()])
第三类选项卡
带有QTabWidget和两个函数以及一个连接,第一个函数接收来自第一类表
的行数,当来自第二类组合
的组合框
的索引更改时,第二个函数应该运行并执行。检查是否可以打印字符串文本我正在执行。但它并没有像预期的那样工作?!当QComboBox的值更改时,函数updatedata()
将永远不会运行
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = [Combo(), Combo()]
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rownumber.connect(self.rowselected_tables)
self.combo[self.Row].Toupdatedata.connect(self.updatedata)
# Alternative calling function currentrow.
# self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
@QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
self.Tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()
self.datacombo[row] = a1
@QtCore.pyqtSlot()
def updatedata(self):
self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
print('I am executing', self.datagroup)
我想知道,我做错了什么
更新:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
rowCount = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(3, 3)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tableWidget)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
def getrow(self):
count = self.tableWidget.rowCount()
self.rowCount.emit(count)
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Combo, self).__init__(parent)
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["1","2","3","4"])
self.hbox = QtWidgets.QHBoxLayout()
self.con = QtWidgets.QLabel("Number: ")
self.hbox.addWidget(self.con)
self.hbox.addWidget(self.combo)
self.setLayout(self.hbox)
self.combo.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot(int)
def setdatastrength(self, index):
self.dataupdate()
def dataupdate(self):
self.Toupdatedata.emit()
print('Signal emitted')
def get_data(self):
return tuple([self.combo.currentText()])
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = []
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rowCount.connect(self.addrow)
self.Table.getrow()
self.Table.rownumber.connect(self.rowselected_tables)
self.combo[self.Row].Toupdatedata.connect(self.updatedata)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tab)
# Alternative calling function currentrow.
# self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
@QtCore.pyqtSlot(int)
def addrow(self, count):
for row in range(count):
self.combo.append(Combo())
self.datacombo.append(Combo().get_data())
@QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
while self.tab.count() > 1:
self.tab.removeTab( self.tab.count()-1 )
self.tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()
self.datacombo[row] = a1
else:
for n in [1]:
self.tab.removeTab( n )
@QtCore.pyqtSlot()
def updatedata(self):
self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
print('I am executing', self.datagroup)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Tab()
w.show()
sys.exit(app.exec_())
在我看来,您将signalself.combo[self.Row].Toupdatedata.connect(self.updatedata)
连接到了错误的位置。
我注意到修改的文本。试试看:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
class Table(QtWidgets.QWidget):
rownumber = QtCore.pyqtSignal(int)
rowCount = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(Table, self).__init__(parent)
self.tableWidget = QtWidgets.QTableWidget(3, 3)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tableWidget)
self.tableWidget.cellClicked.connect(self.cellClick)
def cellClick(self, row, column):
self.rownumber.emit(row)
def currentrow(self):
return self.tableWidget.currentRow()
def getrow(self):
count = self.tableWidget.rowCount()
self.rowCount.emit(count)
class Combo(QtWidgets.QWidget):
Toupdatedata = QtCore.pyqtSignal(int) # + int
def __init__(self, rowTable, parent=None): # + rowTable
super(Combo, self).__init__(parent)
self.rowTable = rowTable # +
#@ print(rowTable, '------')
self.combo = QtWidgets.QComboBox()
self.combo.addItems(["item1", "item2", "item3", "item4"])
self.hbox = QtWidgets.QHBoxLayout()
self.con = QtWidgets.QLabel("Number: ")
self.hbox.addWidget(self.con)
self.hbox.addWidget(self.combo)
self.setLayout(self.hbox)
self.combo.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot(int)
def setdatastrength(self, index):
self.dataupdate()
def dataupdate(self):
print('+ Signal emitted ->', self.rowTable)
self.Toupdatedata.emit(self.rowTable) # + self.rowTable
def get_data(self):
return tuple([self.combo.currentText()])
class Tab(QtWidgets.QWidget):
def __init__(self, parent=None):
super( Tab, self).__init__()
self.tab = QtWidgets.QTabWidget()
self.Table = Table()
self.combo = []
self.datacombo = []
self.Row = 0
self.tab.addTab( self.Table, 'Tables')
self.Table.rowCount.connect(self.addrow)
self.Table.getrow()
self.Table.rownumber.connect(self.rowselected_tables)
#- self.combo[self.Row].Toupdatedata.connect(self.updatedata)
self.lay = QtWidgets.QHBoxLayout(self)
self.lay.addWidget(self.tab)
# Alternative calling function currentrow.
# ? self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
@QtCore.pyqtSlot(int)
def addrow(self, count):
for row in range(count):
cb = Combo(row) # + row
self.combo.append(cb)
self.datacombo.append(cb.get_data()[0]) # get_data()[0]
self.combo[row].Toupdatedata.connect(lambda rowTable=row: self.updatedata(rowTable)) # <===== !!!
@QtCore.pyqtSlot(int)
def rowselected_tables(self, row):
self.Row = row
if row > 0:
while self.tab.count() > 1:
self.tab.removeTab( self.tab.count()-1 )
self.tab.addTab(self.combo[row], 'Combo')
a1 = self.combo[row].get_data()[0] # + [0]
self.datacombo[row] = a1
else:
for n in [1]:
self.tab.removeTab( n )
@QtCore.pyqtSlot()
def updatedata(self, rowTable): # + rowTable
# self.datacombo[self.Table.currentrow()] = self.combo[self.Table.currentrow()].get_data()
self.datacombo[rowTable] = self.combo[rowTable].get_data()[0] # [0]
print('I am executing', self.datacombo ) # ? self.datagroup
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Tab()
w.show()
sys.exit(app.exec_())
导入系统
从PyQt5导入QtCore、QtWidgets、QtGui
类表(qtwidts.QWidget):
rownumber=QtCore.pyqtSignal(int)
rowCount=QtCore.pyqtSignal(int)
def uuu init uuu(self,parent=None):
超级(表,自身)。\uuuu初始化\uuuuu(父级)
self.tableWidget=qtwidts.QTableWidget(3,3)
self.lay=qtwidts.QHBoxLayout(self)
self.lay.addWidget(self.tableWidget)
self.tableWidget.cellClicked.connect(self.cellClick)
def单元单击(自身、行、列):
self.rownumber.emit(行)
def当前行(自身):
返回self.tableWidget.currentRow()
def getrow(自身):
count=self.tableWidget.rowCount()
self.rowCount.emit(计数)
类组合(qtwidts.QWidget):
Toupdatedata=QtCore.pyqtSignal(int)#+int
def uu init_uu(self,rowTable,parent=None):#+rowTable
超级(组合,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.rowTable=rowTable#+
#@打印(行表“-----”)
self.combo=qtwidts.QComboBox()
self.combo.addItems([“item1”、“item2”、“item3”、“item4”])
self.hbox=qtwidts.QHBoxLayout()
self.con=qtwidts.QLabel(“编号:”)
self.hbox.addWidget(self.con)
self.hbox.addWidget(self.combo)
self.setLayout(self.hbox)
self.combo.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot(int)
def设置数据强度(自身,索引):
self.dataupdate()
def数据更新(自我):
打印(“+发射信号->”,self.rowTable)
self.Toupdatedata.emit(self.rowTable)#+self.rowTable
def get_数据(自身):
返回元组([self.combo.currentText()]))
类选项卡(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
超级(制表符,自我)。\uuuu初始化
self.tab=QtWidgets.QTabWidget()
self.Table=Table()
self.combo=[]
self.datacombo=[]
self.Row=0
self.tab.addTab(self.Table,“Tables”)
self.Table.rowCount.connect(self.addrow)
self.Table.getrow()
self.Table.rownumber.connect(self.rowselected_表)
#-self.combo[self.Row].Toupdatedata.connect(self.updatedata)
self.lay=qtwidts.QHBoxLayout(self)
self.lay.addWidget(self.tab)
#可选调用函数currentrow。
# ? self.combo[self.Table.currentrow()].Toupdatedata.connect(self.updatedata)
@QtCore.pyqtSlot(int)
def addrow(自身,计数):
对于范围内的行(计数):
cb=组合(行)#+行
self.combo.append(cb)
self.datacombo.append(cb.get_data()[0])#get_data()[0]
self.combo[row].Toupdatedata.connect(lambda rowTable=row:self.updatedata(rowTable))#0:
当self.tab.count()大于1时:
self.tab.removeTab(self.tab.count()-1)
self.tab.addTab(self.combo[row],'combo')
a1=self.combo[row].get_data()[0]#+[0]
self.datacombo[row]=a1
其他:
对于[1]中的n:
自选项卡移除选项卡(n)
@QtCore.pyqtSlot()
def updatedata(自身,行表):#+行表
#self.datacombo[self.Table.currentrow()]=self.combo[self.Table.currentrow()].get_data()
self.datacombo[rowTable]=self.combo[rowTable].get_data()[0]#[0]
打印('我正在执行',self.datacombo)#?self.datagroup
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=qtwidts.QApplication(sys.argv)
w=制表符()
w、 show()
sys.exit(app.exec_())
提供一个(最小值是特殊的)查看我的更新。我试图提供一个最小的脚本。我知道self.combo[self.Row].Toupdatedata.connect(self.updatedata)
是错误的根源。