具有三个班级。 Table
类QTableWidget以及一个信号和一个函数,用于通过click事件获取行号。传输并发送给其他两个类。脚本的这一部分很有希望。
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()
第二个Combo
类带有QComboBox和一个信号以及两个函数,用于向第三类发送和传输信息,该值在QComboBox中已更改。要检查信号是否正在发射,请打印Signal emitted
。由于执行了打印代码,脚本的这一部分似乎也可以正常工作。
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和两个函数以及一个连接的第三类Tab
,第一个函数从第一类Table
接收行数,第二个函数应该并且应该在索引{{1 }}类的第二名combobox
发生了变化。检查是否可以打印字符串文本Combo
。但是它没有按预期工作?!当QComboBox的值更改时,函数I am executing
永远不会运行!
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)
答案 0 :(得分:1)
我认为您在错误的位置连接了信号self.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_())