Z
Z
Zippowin2021-04-19 17:23:04
PyQt
Zippowin, 2021-04-19 17:23:04

PyQt emitting multiple signals on QTableWidget?

Hello. I'm not an expert in PyQt, I'm just starting to learn. And I encountered extremely strange behavior of signals that cannot be overcome.
There is a QTableWidget widget with a certain hierarchy, which is constantly refilled depending on user actions (to be more precise, search is implemented using QLineEdit). Table elements are clickable. I connect Double-click like this:

self.ui.tree.itemDoubleClicked.connect(self.connect)

And then I find out which element was clicked:
item = self.ui.tree.currentItem()
This works great, only after the table is refilled, the emitted signals for the same element become n + 1
That is, if we refill the table 5 times, then by clicking on the element, 5 times it will start self.connect.
I've tried everything I can.
Tried to use disconnect before refilling, and then connect again. But no matter what I do, it won't connect to the slot a second time.
I found about blockSignal () on the Internet. Hello, before refilling the table, call the method with True, and after with False, something like this:
self.ui.tree.blockSignal(True)
<перезаписываем ...>
self.ui.tree.blockSignal(False)

But that doesn't help either.
Is it really such a buggy implementation of signals in the framework? Please help me figure it out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2021-04-20
@bbkmzzzz

Here, I threw it, without duplicate signals, with refilling, etc.

import sys

from PySide2.QtWidgets import QApplication, QTableWidget, QTableWidgetItem


class Main(QTableWidget):
    def __init__(self):
        super(Main, self).__init__()
        self.setRowCount(10)
        self.setColumnCount(5)

        self.itemDoubleClicked.connect(self.__on_item_double_click)

        self.setEditTriggers(self.EditKeyPressed | self.AnyKeyPressed)

        self.filling_count = 0
        self.refill()

    def __on_item_double_click(self, item: QTableWidgetItem):
        print(f"row:{item.row()} col:{item.column()} item: {item}")
        self.refill()

    def refill(self):
        self.filling_count += 1
        for row in range(self.rowCount()):
            for col in range(self.columnCount()):
                item = QTableWidgetItem(
                    f"row:{row + 1} col:{col + 1} filled: {self.filling_count}"
                )
                self.setItem(row, col, item)
        self.resizeColumnsToContents()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question