-1

I begin with python v3.10, I use actually PyQt6 for the design of the windows.

I created a QTableWidget in which I put a checkbox with a picture.

Now I would like to create a signal to execute a function when I click on it.

I tried, connect, stateChanged, ... but nothing works.

I write below my code with a picture :

        while self.query.next():
        # widget checkbox
        checkbox_widget = QWidget()
        checkbox_widget.setStyleSheet('background-color: transparent;')
        w_checkbox = QtWidgets.QCheckBox

        layout_cb = QtWidgets.QHBoxLayout(checkbox_widget)
        layout_cb.addWidget(w_checkbox())
        layout_cb.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout_cb.setContentsMargins(0, 0, 0, 0)
        checkbox_widget.setLayout(layout_cb)

        tablerow = self.table_EP.rowCount()

        self.table_EP.insertRow(tablerow)

        self.table_EP.setItem(tablerow, 0, QtWidgets.QTableWidgetItem((self.query.value('col1'))))
        self.table_EP.setItem(tablerow, 1, QtWidgets.QTableWidgetItem((self.query.value('col2'))))
        self.table_EP.setItem(tablerow, 2, QtWidgets.QTableWidgetItem((self.query.value('col3'))))
        self.table_EP.setItem(tablerow, 3, QtWidgets.QTableWidgetItem((str(self.query.value('col4')))))
        self.table_EP.setItem(tablerow, 4, QtWidgets.QTableWidgetItem((str(self.query.value('col5')))))
        self.table_EP.setItem(tablerow, 5, QtWidgets.QTableWidgetItem((str(self.query.value('col6')))))
        self.table_EP.setItem(tablerow, 6, QtWidgets.QTableWidgetItem((self.query.value('col7'))))
        self.table_EP.setItem(tablerow, 7, QtWidgets.QTableWidgetItem((str(self.query.value('col8')))))
        self.table_EP.setItem(tablerow, 8, QtWidgets.QTableWidgetItem((str(self.query.value('col9')))))
        self.table_EP.setCellWidget(tablerow, 9, w_checkbox())
        self.table_EP.setCellWidget(tablerow, 9, checkbox_widget)

        w_checkbox.stateChanged.connect(self.clicked_button())

enter image description here

Thank you so much for your help (I'm sorry for my English).

  • 1
    Typos: remove the parentheses in `w_checkbox()` and in the connection to `self.clicked_button`, and remove the first `setCellWidget()` line. – musicamante Aug 23 '22 at 08:48

1 Answers1

0

This is some weird code.

Ultimately you need to call .stateChanged.connect(self.clicked_button()) on the instance of QtWidgets.QCheckBox.

You create an instance of the class QtWidgets.QCheckBox with this line: layout_cb.addWidget(w_checkbox()), but you don't assign it to a variable.

You should first assign it to a variable, then refer to it using that variable, like so:

checkbox = QtWidgets.QCheckBox()
layout_cb.addWidget(checkbox)
checkbox.stateChanged.connect(self.clicked_button())

Are you sure you need the brackets after self.clicked_button()? That only makes sense if it returns a function.

GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16