如何在qtablewidget中的每一行放置单选按钮,并且只允许一行从行组中进行选择?

时间:2017-09-25 08:25:03

标签: python python-2.7 pyqt pyqt4 qtablewidget

以下是我的表格:

Table

在上表中,我已将Radiobutton放在每一行。

在表中,列'field'在行中具有相同的值,即startdate存在于两行中,有效性也在行中重复。 我想要的是只使用单选按钮从重复值中仅选择一行包含有效性。如果用户选择其他值,则必须取消选择以前选择的值。

以下是我的代码:

class Window(QtGui.QWidget):
    def __init__(self, data):
        QtGui.QWidget.__init__(self)
        self.data=data
        rows=len(data)

        data1=list()

        for d in data:
            #print d
            #print "ddddddddddddddd",type(d)

            d=d.replace('null','""')

            d=eval(d)

            #print type(d)
            data1.append(d)
        self.data1=data1
        key_list=['file','field','content','refined_content','page_number']
        data2=list()
        for line in data1:
            #print line
            d = OrderedDict((k, line[k]) for k in key_list)
            data2.append(d)
        #print data2
        data1=data2
        #print data1

        keys = ['file','field','content','refined_content','page_number']

        keys=list(keys)

        columns=len(keys)+1



        keys.append("checkbox")


        self.table = QtGui.QTableWidget(rows, columns, self)
        self.table.setHorizontalHeaderLabels(keys) 


        self.keys=keys
        #print "total columns",columns
        #print "total rows",rows
        #columns=str(columns)

        for column in range(columns-1):
            for row in range(rows):
                qwidget = QtGui.QWidget()


                checkbox = QtGui.QRadioButton()
                checkbox.setChecked(False)

                qhboxlayout = QtGui.QHBoxLayout(qwidget)
                qhboxlayout.addWidget(checkbox)
                qhboxlayout.setAlignment(Qt.AlignCenter)
                qhboxlayout.setContentsMargins(0, 0, 0, 0)
                dic=data1[row]
                #print dic

                index=int(column)
                #print "index",index
                value = dic.values()[index]
                #print "current row",row
                #print "current column",column
                self.table.setItem(row, column, QtGui.QTableWidgetItem(str(value)))
                #self.table.setItem(row, columns+1, QtGui.QTableWidgetItem(str(row)))
                self.table.setCellWidget(row,columns-1 , qwidget)

        layout = QtGui.QVBoxLayout(self)
        self.button = QtGui.QPushButton()
        self.button.setObjectName("loadButton")
        self.button.setFixedWidth(100)
        self.button.setFixedHeight(30)
        self.button.setStyleSheet('color: white;background-color:rgb(0, 76, 153);border-style: outset;border-width: 2px;border-color: rgb(0, 76, 153);border-radius: 10px;alignment:left;')

        self.button.setText("Submit")

        layout.addWidget(self.table)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.ButtonClicked)


    def ButtonClicked(self,c):
        c=self.table.columnCount()

        #print "c",c
        #print "keys",self.keys
        checked_list = []
        for i in range(self.table.rowCount()):
            #checked_list = []
            dic={}
            if self.table.cellWidget(i, c-1).findChild(type(QtGui.QRadioButton())).isChecked():
                for j in range(c-1):

                    st=str(self.table.item(i, j).text())
                    #print j,st
                    dic[self.keys[j]]=st
                    #checked_list.append(st)
                #print "ddiic",dic
                checked_list.append(dic)
        print checked_list

0 个答案:

没有答案