获取pyqt中qtablewidget的行数

时间:2015-12-13 21:51:07

标签: pyqt qtablewidget

我想在我点击QTableWidget(tblbimar)中的select botton时给我相同的行数(QTable)。 但是,当我点击“选择”按钮时,没有任何事情发生! 当然我试过是否发送信号“tbl_bimar_row”?(它工作正常)

# -*- coding: utf-8 -*-

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

import os
import sys

class groupbutton(QWidget):
    tbl_bimar_row = pyqtSignal(int)
    def __init__(self,parent=None):
        super(groupbutton,self).__init__(parent)
        self.parent = parent
        btnselect = QPushButton("select")
        layout = QHBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        layout.setSpacing(5)
        layout.addWidget(btnselect)
        self.setLayout(layout)
        self.connect(btnselect,SIGNAL("clicked()"),self.return_row)

    def return_row(self):
        widget_pos = QtGui.qApp.focusWidget()
        index = self.parent.indexAt(widget_pos.parentWidget().pos())
        # OR index = self.parent.indexAt(widget_pos.pos())
        if index.isValid():
            self.tbl_bimar_row.emit(index.row())


class Jarahi_add(QDialog,Ui_My_Form):
    def __init__(self,parent=None):
        super(Jarahi_add,self).__init__(parent)
        self.setupUi(self)
        self.bimar_id = None
        get_bimar()
        self.connect(self.txtbimarname,SIGNAL("textChanged(QString)"),self.get_bimar)

    def bimar_select(self,row):
        print(self.tblbimar.item(row,1).text())

    def get_bimar(self):
        data = [['1','2','3','4','5'],['6','7','8','9','10']]
        for row in data:
            inx = data.index(row)
            self.tblbimar.insertRow(inx)
            self.tblbimar.setItem(inx,0,QTableWidgetItem(row[0]))
            self.tblbimar.setItem(inx,1,QTableWidgetItem(row[1]))
            self.tblbimar.setItem(inx,2,QTableWidgetItem(row[2]))
            self.tblbimar.setItem(inx,3,QTableWidgetItem(row[3]))
            self.tblbimar.setItem(inx,4,QTableWidgetItem(row[4]))
            selbtn = groupbutton(parent=self.tblbimar)
            selbtn.tbl_bimar_row.connect(self.bimar_select)
            self.tblbimar.setCellWidget(inx,5,selbtn)
            # self.tblbimar.setItem(inx,5,QTableWidgetItem(smart_text(row[0])))
            self.tblbimar.resizeColumnsToContents()
            self.tblbimar.resizeRowsToContents()

谢谢

1 个答案:

答案 0 :(得分:0)

我将以上代码更改为以下代码并且有效:

class groupbutton(QWidget):
    tbl_bimar_row = pyqtSignal(int)
    def __init__(self,parent=None,app=None):
        super(tblbutton,self).__init__(parent)

        self.parent = parent
        self.app = app  
        ...
        ...
    def return_row(self):
        widget_pos = self.app.focusWidget()
        index = self.parent.indexAt(widget_pos.parentWidget().pos())
        self.tbl_bimar_row.emit(index.row())
class Jarahi_add(QDialog,Ui_My_Form):
    ...  
    ... 
    def get_bimar(self):

        if data:
            for row in data:
                ...  
                ...  
                selbtn = tblbutton(parent=self.tblbimar,app=self)
                selbtn.tbl_bimar_row.connect(self.bimar_select)
                self.tblbimar.setCellWidget(inx,5,selbtn)