Pavel.D
Pavel.D

Reputation: 581

Signal emitting with currentIndexChanged invoking function

Having three classes. Class Table QTableWidget and a signal and a function to get row numbers with click event. which transmit and send to two other classes. This part of script works as promising.

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()

Second Combo class with QComboBox and a signal and two functions to send and transfer information to third class, that value in QComboBox is changed. To check if the signal is emitting, printing Signal emitted . This part of script seems also working fine, since print code is executed.

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()])

Third class Tab with QTabWidget and two functions and a connection, first function recieves numbers of row from first class Table, second function should and supposed to run and execute, when index of combobox from second class Combo changes. Checking to see if it works printing a string text I am executing. But it does not work as expected?! When values of QComboBox changes, function updatedata() never runs!

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)

I wonder, what's it I'm doing wrong?

UPDATE:

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_())

Upvotes: 0

Views: 576

Answers (1)

S. Nick
S. Nick

Reputation: 13691

In my opinion, you are connecting the signal self.combo[self.Row].Toupdatedata.connect (self.updatedata) in the wrong place. I noted the text where the changes were made. Try it:

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_())

enter image description here

enter image description here

Upvotes: 1

Related Questions