0

I have a table created with 2 columns and 0 rows called tableWidget_Events.

Here's what the table and grid of buttons look like: table snip

Using the value from the spinbox, I want to insert the button text as a row, so if the spinbox = 1 and I click walk, there should be a row created with 1 in the first column and walk in the second column.

Here's the code I have to connect the buttons:

    self.pushButton_SO.clicked.connect(self.add_table)
    self.pushButton_Walk.clicked.connect(self.add_table)
    self.pushButton_GB.clicked.connect(self.add_table)
    self.pushButton_FB.clicked.connect(self.add_table)
    self.pushButton_PU.clicked.connect(self.add_table)

Then the function to add the row:

def add_table(self):
    button = self.sender() # get button text
    row = self.spinBox_AB_TBL.value() #get value from spinbox

    rowPosition = self.tableWidget_Events.rowCount()
    self.tableWidget_Events.insertRow(rowPosition) #insert new row
    
    self.tableWidget_Events.setItem(row, 0, QtGui.QTableWidgetItem(self.spinBox_AB_TBL.value()))
    self.tableWidget_Events.setItem(row, 1, QtGui.QTableWidgetItem(button.text()))

The buttons are connected. I can print the button text to the console, and each button click adds a new row. Inserting the text is what I'm hung up on.

Thank you.

1 Answer 1

2

QTableWidgetItem can not receive an integer as data, you must convert it to string, in your case the value() method of QSpinBox returns an integer, and this causes the problem.

Also if you want to insert you must indicate the appropriate position, in your case it should be rowPosition-1 instead of row

def add_table(self):
    button = self.sender() # get button text
    row = self.spinBox_AB_TBL.value() #get value from spinbox

    rowPosition = self.tableWidget_Events.rowCount()
    self.tableWidget_Events.insertRow(rowPosition) #insert new row

    self.tableWidget_Events.setItem(rowPosition-1, 0, QtGui.QTableWidgetItem(str(row)))
    self.tableWidget_Events.setItem(rowPosition-1, 1, QtGui.QTableWidgetItem(button.text()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.