Change table elements

I would like to change the elements of a QTableWidget due to a change in items of a QComboBox. I can create the table correctly. However, in the following code I get the error message:
print(self.dateTable.currentRow())
^^^^^^^^^^^^^^
AttributeError: ‘showObservation’ object has no attribute ‘dateTable’

from PyQt6.QtWidgets import QTableWidget, QComboBox, QTableWidgetItem,
QFormLayout, QWidget, QApplication, QMainWindow
import sys
from PyQt6.QtGui import QAction

class MainWindow(QMainWindow):
def init(self):
super().init()

    self.setWindowTitle("table test")

    fileAction = QAction("file",self)

    showObservationDateAction = QAction("showObservationDate", self)
    showObservationDateAction.triggered.connect(self.showObservationDateActionClick)
  
    questionAction = QAction("question", self)
    aboutAction = QAction("about", self)

    menu = self.menuBar()

    file_menu = menu.addMenu("file")
    file_menu.addAction(fileAction)
    file_menu.addSeparator()

    fits_menu = menu.addMenu("fits")

    fits_menu.addAction(showObservationDateAction)

    help_menu = menu.addMenu("help")
    help_menu.addAction(questionAction)
    help_menu.addAction(aboutAction)


def fileActionClick(self):
    print("File click")

def showObservationDateActionClick(self):
    self.w = showObservation()
    self.w.show()

class showObservation(QWidget):
def init(self):
super().init()

    layout = QFormLayout()
   
    self.itemCombo = QComboBox()
    self.itemCombo.currentTextChanged.connect(self.itemComboChanged)

    for i in range (5):
        self.itemCombo.addItem("item " + str(i))
    
    self.itemCombo.setCurrentIndex(3)
        
    self.dateTable = QTableWidget()
    self.dateTable.setFixedWidth(800)
    self.dateTable.setFixedHeight(300)
    
    headers = ['col 0', 'col 1', 'col 2', 'col 3']
    self.dateTable.setColumnCount(len(headers))
    self.dateTable.setHorizontalHeaderLabels(headers)

    self.dateTable.setRowCount(4)
    
    for i in range(5):
        cell = []
        
        for j in range(len(headers)):
            cell.append(QTableWidgetItem("table cell " + str(i) + str(j)))
            
            self.dateTable.setItem(i,j, cell[j])
                
    
    layout.addRow(self.itemCombo)
    layout.addRow(self.dateTable)

    self.setLayout(layout)
        
def itemComboChanged(self, s):
    print(s)
    print(self.dateTable.currentRow())

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

Hello,
That’s because at the time itemComboChanged is called, your table is not created. You are calling setCurrentIndex() before creating the table here:

    self.itemCombo.setCurrentIndex(3)
        
    self.dateTable = QTableWidget()
    self.dateTable.setFixedWidth(800)
    self.dateTable.setFixedHeight(300)

Edit: Note that adding items probably also emit the signal.

Either create the table before the combobox, or do the connection to the signal currentTextChanged after calling setCurrentIndex, or after table creation.

Personally I’m always doing connections after GUI initialization to avoid this kind of issues.

1 Like