[Solved]cellChanged Signal doesnt work in QBox Layout



  • Hi there,

    I started programming with Qt a few days ago and stumbled upon a problem.
    After i added a Qtable in a QBox, the Data(Numbers), that are entered when the program runs, cant be read nor does the script thats defined in the cellChanged.connect(script) execute. If I run the Table in its own window by just doing tablename.show() all the functions work.

    Am I missing some peace of code to add for the Signals within the QBox?

    (Using PyQt4)

    Best regards,

    Alex



  • Here are some snippets of my code:
    MainWindow.py:

    from PyQt4 import QtGui, QtCore
    import sys
    from Constructors.functions import MP_Tabelle
    
    /class Main_Window(QtGui.QWidget):
        def __init__(self):
            super(Main_Window, self).__init__()
            self.Tabelle = MP_Tabelle.MP_Tabelle()
            self.initUI()
    
        def initUI(self):
            #Menu
            save_Button = QtGui.QPushButton("")
            save_Button.setIcon(QtGui.QIcon('./src/img/buttons/save.png'))
            save_Button.setIconSize(QtCore.QSize(30,30))
            save_Button.clicked.connect(self.save_file)
    
            open_Button = QtGui.QPushButton("")
            open_Button.setIcon(QtGui.QIcon('./src/img/buttons/open.png'))
            open_Button.setIconSize(QtCore.QSize(30, 30))
    
            settings_Button = QtGui.QPushButton("")
            settings_Button.setIcon(QtGui.QIcon('./src/img/buttons/settings.png'))
            settings_Button.setIconSize(QtCore.QSize(30, 30))
    
            Menu = QtGui.QHBoxLayout()
            Menu.addWidget(save_Button)
            Menu.addWidget(open_Button)
            Menu.addWidget(settings_Button)
            Menu.addStretch(1)
    
            #Main Layout
            Main_Layout= QtGui.QVBoxLayout()
            Main_Layout.addLayout(Menu)
            Main_Layout.insertWidget(1,MP_Tabelle.MP_Tabelle().table)
    
            self.setLayout(Main_Layout)
            self.setGeometry(0, 0, 720, 480)
            self.center()
            self.setWindowTitle("Test")
            self.show()
    
        def center(self):
            frameGm = self.frameGeometry()
            screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
            centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
            frameGm.moveCenter(centerPoint)
            self.move(frameGm.topLeft())
    
        def save_file(self):
            self.Tabelle.save()
    
    def main():
        app = QtGui.QApplication(sys.argv)
        #ex = Main_Window()
        sys.exit(app.exec_())
    

    MP_Tabelle.py:

    from PyQt4.QtCore import *
    import pandas as pd
    from Constructors.widgets import table as table
    
    
    class MP_Tabelle(table.table, QObject):
        def __init__(self):
            super(MP_Tabelle,self).__init__(0,0, "")
            self.Datei_Artikelstamm = "Master_Art_DB.csv"
            self.Art_DB = pd.read_csv(self.Datei_Artikelstamm, encoding='utf-8-sig', sep=";")
            self.MP_Gen()
    
    
        def MP_Art_Gen(self):
            for index, row in self.Art_DB.iterrows():
                if "nan" in str(row['Untergruppe_Name']):
                    #if "0" in str(row['Untergruppe_ID']):
                    self.add_row(index)
                    self.edit_cell(index, 0, row['Artikelnummer'])
                    self.edit_cell(index, 4, row['Artikelname'])
                else:
                    self.add_row_header(index, row['Artikelnummer'] + " " + row['Untergruppe_Name'], "white", "red")
    
        def MP_Gen(self):
            self.add_column(0, "Art-Nr", 100)
            self.add_column(1, "ist", 40)
            self.add_column(2, "soll", 40)
            self.add_column(3, "dif", 40)
            self.add_column(4, "Typ", 250)
            self.add_column(5, "Bemerkung", 100)
            for i in range(20):
                self.add_column(6+i, i, 40)
            self.MP_Art_Gen()
            self.editable("column", 0, True)
            self.editable("column", 1, True)
            self.editable("column", 2, True)
            self.editable("column", 3, True)
            self.editable("column", 4, True)
    
            self.hide_vert_header()
            self.show()
            self.update()
    
    
        def save(self, dataname="Projekt"):
            print("Save Pressed")
            print(self.NumberofColumns)
            print(self.get_data(3,0))
    

    table.py:

    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtCore import pyqtSlot
    
    
    class table(QObject):
        def __init__(self, c, r, title):
            super(table, self).__init__()
            self.NumberofColumns = c
            self.NumberofRows = r
            self.table = QTableWidget()
            self.table.setWindowTitle(title)
            self.table.setRowCount(r)
            self.table.setColumnCount(c)
            self.table.resize(400,250)
    
        def create(self, c, r):
            self.table.setRowCount = r
            self.table.setColumnCount = c
    
        def add_row(self, index):
            self.table.insertRow(index)
            self.table.verticalHeader().setResizeMode(index, QHeaderView.Fixed)
            self.table.verticalHeader().setDefaultSectionSize(20)
            for i in range(self.NumberofColumns):
                self.table.setItem(index,i, QTableWidgetItem(""))
            self.NumberofRows = self.NumberofRows + 1
    
        def add_column(self, index, title="NaN", width=100):
            self.table.insertColumn(index)
            self.table.setColumnWidth(index, width)
            self.table.horizontalHeader().setResizeMode(index, QHeaderView.Fixed)
            if title != "NaN":
                self.table.setHorizontalHeaderItem(index, QTableWidgetItem(title))
    
            self.NumberofColumns = self.NumberofColumns + 1
            #print(self.NumberofColumns)
    
        def editable(self,columnorrow, index, editable=True):
            if editable == True:
                if columnorrow == "column":
                    for i in range(self.NumberofRows):
                        #self.table.setItem(i, index, QTableWidgetItem())
                        self.table.item(i, index).setFlags(Qt.ItemIsEnabled)
                if columnorrow == "row":
                    for i in range(self.NumberofColumns):
                        self.table.item(index, i).setFlags(Qt.ItemIsEnabled)
    
    
        def add_row_header(self, index, title, textcolor="black", backcolor="white"):
            self.table.insertRow(index)
            for i in range(self.NumberofColumns):
                self.table.setItem(index, i, QTableWidgetItem())
            self.table.item(index, 0).setText(str(title))
            self.table.setSpan(index, 0, 1, self.NumberofColumns)
            self.table.item(index, 0).setBackground(QColor(backcolor))
            self.table.item(index, 0).setForeground(QColor(textcolor))
            self.table.item(index, 0).setFont(QFont("Times", 10, QFont.Weight(65)))
            self.table.item(index, 0).setFlags(Qt.ItemIsEnabled)
            self.NumberofRows = self.NumberofRows + 1
    
        def edit_cell(self, row, col, text):
            self.table.item(row, col).setText(str(text))
    
        def update(self):
            self.table.cellChanged.connect(self.recalc)
            print("Debug")
    
        def recalc(self, row, col):
            #Summe errechnen
            print("Cell Updated!")
            Art_Sum = []
            for i in range(6, self.NumberofColumns):
                if len(self.table.item(row, i).text()) == 0:
                    pass
                else:
                    Art_Sum.append(int(float(self.table.item(row, i).text())))
            if len(self.table.item(row, 2).text()) == 0:
                Art_Soll = 0
            if len(self.table.item(row, 2).text()) != 0:
                Art_Soll = int(float(self.table.item(row, 2).text()))
            Art_Dif = Art_Soll - sum(Art_Sum)
            self.table.item(row, 1).setText(str(sum(Art_Sum)))
            self.table.item(row, 3).setText(str(Art_Dif))
    
    
    
        def hide_vert_header(self):
            self.table.verticalHeader().hide()
    
        def show(table):
            table.table.show()
    
        def get_data(self, r, c):
            get_data_data = self.table.item(r, c).text()
            return get_data_data
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why are you putting GUI classes in plain QObject based classes ?



  • @SGaist said in cellChanged Signal doesnt work in QBox Layout:

    devn
    as in adding the Table in my Main_Window Class? Sorry i'm just starting with this.


  • Lifetime Qt Champion

    Sorry, I don't understand what you wrote.

    But if you want to build custom widgets, then the base class are usually QWidget or QDialog and QMainWindow.

    You should check the Qt documentation tutorials especially the part about layouts.



  • If I run the MP_Tabelle.py in standalone, it works, but running it with the Main_Window.py inside the QVBoxLayout, the table appears, i can add numbers into the table, BUT the code for cellChanged isnt executed nor the the numbers entered in the Table return when I try to use item(r,c).text() on them.


  • Lifetime Qt Champion

    As I wrote before, you really should consider refactoring your code for better integration.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.