A
A
Alexander Chibilyaev2015-07-02 16:13:42
Python
Alexander Chibilyaev, 2015-07-02 16:13:42

PyQt How to set connect for an unknown number of buttons on a form?

The base from sqlite is displayed in QTableWidget and its own button is placed in the last column in each line.
QtCore.QObject.connect(self.btn[i], QtCore.SIGNAL("clicked()"), lambda: self.StartStop(i)) - how to write connect to each table row?

for i in xrange(self.datacount):
            self.btn[i] = QPushButton("Edit")

        for i in xrange(self.datacount):
            self.data0 = str(self.data[i][0])
            self.data1 = str(self.data[i][1])
            self.data2 = str(self.data[i][2])
            self.data3 = str(self.data[i][3])
            self.data4 = str(self.data[i][4])
            self.data5 = str(self.data[i][5])
            self.window.tableWidget.setItem(i, 0, QtGui.QTableWidgetItem(self.data0))
            self.window.tableWidget.setItem(i, 1, QtGui.QTableWidgetItem(self.data1))
            self.window.tableWidget.setItem(i, 2, QtGui.QTableWidgetItem(self.data2))
            self.window.tableWidget.setItem(i, 3, QtGui.QTableWidgetItem(self.data3))
            self.window.tableWidget.setItem(i, 4, QtGui.QTableWidgetItem(self.data4))
            self.window.tableWidget.setItem(i, 5, QtGui.QTableWidgetItem(self.data5))
            self.window.tableWidget.setCellWidget(i, 6, self.btn[i]);
            QtCore.QObject.connect(self.btn[i], QtCore.SIGNAL("clicked()"), lambda: self.StartStop(i))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Robotex, 2015-07-02
@Robotex

How are the buttons added? That's where they are added and you need to connect to them.

Y
Yaroslav, 2015-07-02
@torwig

Perhaps this is because of the lambda, Lutz even has an example on this topic in the book, but I don’t remember exactly on which page.
You can also try using QSignalMapper.

S
Sergey6661313, 2016-08-19
@Sergey6661313

I will probably never use lambdas again. I did not expect such a trick from them.

# coding=utf-8

import sys
from PyQt4 import QtGui
from PyQt4.QtGui import QPushButton, QTableWidget
from PyQt4.Qt import QApplication
window = QApplication(sys.argv)

class myTable(QTableWidget):
    class MyCustomEditButton(QPushButton):
        def __init__(self, i, name):
            self.i = i
            super().__init__(name)
            self.clicked.connect(self.slot_pressed)

        def slot_pressed(self):
            window.tableWidget.StartStop(self.i)


    def __init__(self):
        super().__init__()
        self.data = [
            ["1 строка"],
            ["2 строка"],
            ["3 строка"],
            ["4 строка"],
            ["5 строка"]
        ]
        self.datacount = len(self.data)
        self.setColumnCount(len(self.data[0])+1)
        self.setRowCount(self.datacount)

        for i in range(self.datacount):
            btn = self.MyCustomEditButton(i, "Edit")

            self.data0 = str(self.data[i][0])
            self.setItem(i, 0, QtGui.QTableWidgetItem(self.data0))
            self.setCellWidget(i, 1, btn)

    def StartStop(self, bb):
        print(bb)


window.tableWidget = myTable()
window.tableWidget.show()
window.exec()

Try it, figure it out. Generally strange behavior of a python. Somehow it's not instinctive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question