在python中单击QtableWidget单元格中的按钮后获取错误的行索引

2024-03-28 11:05:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Qtablewidget的每个单元格中都添加了按钮,当我单击按钮时,它给出了错误的行索引例如,如果我单击第1行(共10行)按钮,我得到的行索引是0而不是1,我无法理解为什么会发生这种情况

self.table = QtGui.QTableWidget()                              
self.table.setColumnCount(2)

for i in range (0,9):
   self.table.insertRow(self.table.rowCount())

   btnplot = QPushButton(self.table)
   btnplot.setText('Plot')
   btnplot.setFixedSize(50,30);
   btnplot.setStyleSheet("background-color:white;")

   self.lstdata = QtGui.QTableWidget()
   self.lstdata.setColumnCount(2)

   self.lstdata.insertRow(self.lstdata.rowCount())
   self.lstdata.setCellWidget(self.lstdata.rowCount()-1,1,btnplot);

   btnplot.clicked.connect(self.cellClick)                                                             

   self.table.setCellWidget(self.table.rowCount()-1,1,self.lstdata)



   def cellClick(self):

      button = self.sender()
      #print (button.text())
      index = self.table.indexAt(button.pos())
      currentRow = index.row()
      print currentRow

请告诉我这个问题


Tags: selfindextablebutton按钮printqtguiqtablewidget