我应该如何将特定行链接到Qtablewidget中的列

时间:2016-12-20 06:51:17

标签: python-2.7 pyqt4 pyside qtablewidget

我有一个包含3列的Qtablewidget。第一列包含用户名,第二列和第三列是复选框。我希望这些复选框是用户行的属性。例如,如果您查看下面的图像,则用户:bruno被标记(具有属性)删除并删除主页。我希望有一个输出:User Bruno marked for delete, marked for delete home。要做到这一点,我需要将用户链接到这些列,我没有丝毫想法。我应该如何处理这个问题。下面是我已经提出的代码。它从文件中填充用户列。

table     来自PyQt4导入QtGui,QtCore

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        QtGui.QWidget.__init__(self)
        self.table = QtGui.QTableWidget(rows, columns, self)
        self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("Users"))
        self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("Delete User"))
        self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("Delete User and Home"))
        self.table.verticalHeader().hide()
        header = self.table.horizontalHeader()
        header.setStretchLastSection(True)
        rowf = 0
        with open("/home/test1/data/users") as in_file:
                if in_file is not None:
                    users = in_file.readlines()
                for line in users:
                    users = QtGui.QTableWidgetItem()
                    self.table.setItem(rowf, 0, users)
                    users.setText(line)
                    rowf+=1
                    in_file.close()
        for column in range(columns):
            for row in range(rows):
                if column % 3:
                    item = QtGui.QTableWidgetItem(column)
                    item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                                  QtCore.Qt.ItemIsEnabled)
                    item.setCheckState(QtCore.Qt.Unchecked)
                    self.table.setItem(row, column, item)
        self.table.itemClicked.connect(self.cell_was_clicked)
        layout = QtGui.QVBoxLayout(self)

        layout.addWidget(self.table)
        self._list = []

    def cell_was_clicked(self, item):
        #row = self.table.currentItem().row()
        #col = self.table.currentItem().column()
        #print "item:", item
        #print "row=", row
        if item.checkState() == QtCore.Qt.Checked:
            print('"%s" Checked' % item.text())
            #self._list.append(item.row())
        else:
            #print "(", row , ",", col, ")"
            print('%s' % item.text())
            print (item.row(),item.column())


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(200, 3)
    window.resize(400, 400)
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

修理如下:

myavg(integer)