Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

python - How to add multiple QPushButtons to a QTableView?

I have a QTableView to which I want to set a QPushButton for every row. I am doing this as follows within my class derived from QWidget following an example found here:

 for index in range(number_rows):
        btn_sell = QPushButton("Edit", self)
        btn_sell.clicked.connect(self.button_edit)
        table_view.setIndexWidget(table_view.model().index(index, 4), btn_sell)

If the table is drawn and I click on one of the QPushButton the method self.button_edit is called - but which one? It does not seem that an 'event' of any sort is given to self.button_edit, so how can I find the row-index of the QPushButton that was clicked within the button_edit method?

Maybe there is a different way altogether to add a button to each row of a table?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your event handler will look similar to this:

def handleButtonClicked(self):
    button = QtGui.qApp.focusWidget()
    # or button = self.sender()
    index = self.table.indexAt(button.pos())
    if index.isValid():
        print(index.row(), index.column())

This uses the indexAt function to get the button's position.

For clarity, my script looks like this:

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.table = QtGui.QTableWidget()
        self.table.setColumnCount(3)
        self.setCentralWidget(self.table)
        data1 = ['row1','row2','row3','row4']
        data2 = ['1','2.0','3.00000001','3.9999999']

        self.table.setRowCount(4)

        for index in range(4):
            item1 = QtGui.QTableWidgetItem(data1[index])
            self.table.setItem(index,0,item1)
            item2 = QtGui.QTableWidgetItem(data2[index])
            self.table.setItem(index,1,item2)
            self.btn_sell = QtGui.QPushButton('Edit')
            self.btn_sell.clicked.connect(self.handleButtonClicked)
            self.table.setCellWidget(index,2,self.btn_sell)

    def handleButtonClicked(self):
        button = QtGui.qApp.focusWidget()
        # or button = self.sender()
        index = self.table.indexAt(button.pos())
        if index.isValid():
            print(index.row(), index.column())

Which will produce a small GUI like this:

small gui

When the Edit buttons are clicked, it prints, to the console:

(0, 2)
(1, 2)
(2, 2)
(3, 2)

The first element is your row index, the second is your column (remember it is 0 based, which is why it shows 2, not 3 - despite the column headers).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...