Rt Rtt
Rt Rtt

Reputation: 617

PyQt5: How to get the signal from a QTableWidget with QComboBox?

I understand this is a beginner's question, I searched but could not found the answer.

In the App there is a QTableWidget, which contains a QComboBox, a QDoubleSpinBox, and some regualar items.

By changing ANY cells I want to have the method on_change activated and print something.

The code below doesnot take the change-signal from the QComboBox and QDoubleSpinBox. How could I correct the code?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

list_text = ["a", "b", "c"]

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.createTable()

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.setLayout(self.layout)

        self.show()

    def createTable(self):
        # Create table
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(6)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setItem(0, 0, QTableWidgetItem("Text0"))
        self.tableWidget.setItem(0, 1, QTableWidgetItem("Text1"))
        self.tableWidget.setItem(1, 0, QTableWidgetItem("Text2"))
        self.tableWidget.setItem(1, 1, QTableWidgetItem("Text3"))

        self.cbx = QComboBox()
        self.cbx.addItems(list_text)
        self.tableWidget.setCellWidget(2, 0, self.cbx)


        self.dsb = QDoubleSpinBox(self)
        self.dsb.setDecimals(0)
        self.dsb.setMinimum(1)
        self.dsb.setMaximum(100)
        self.dsb.setSingleStep(1)
        self.dsb.setValue(50)
        self.tableWidget.setCellWidget(3, 0, self.dsb)


        # table selection change
        self.tableWidget.itemChanged.connect(self.on_change)

    def on_change(self):
        print("any item was changed. ")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Thanks for the help!

Upvotes: 0

Views: 3794

Answers (1)

user9333067
user9333067

Reputation:

You don't "change the item", you change "the items content", when switching something on the QCombobox or QSpinbox.

Therefore, they need their own connection:

# table selection change
self.tableWidget.itemChanged.connect(self.on_change)    
# adding with this
self.dsb.valueChanged.connect(self.on_change)
self.cbx.currentTextChanged.connect(self.on_change)

If you were adding something new to the basic cell, the signal would get triggered, but in your setup there is just an object in the cell, that is not changing at all (because it's child is doing so).

Upvotes: 2

Related Questions