How should I link specific rows to columns in a Qtablewidget

209 views Asked by At

I have a Qtablewidget with 3 columns. The first column contain user names, the second and third are columns are check boxes. I would like these check boxes to be attributes for the row of users. For example If you look at the image below, User: bruno is marked(has attributes) delete and delete home. I would like to have an output like: User Bruno marked for delete, marked for delete home. To do that, I would need to link the users to both these columns which I haven't the slightest idea. How should I approach this problem. Below is the code that I already came up with. It populates the users column from a file.

table from PyQt4 import 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

There are 1 answers

0
answerSeeker On BEST ANSWER

fixed it like this:

    def cell_was_clicked(self, item):
    if item.checkState() == QtCore.Qt.Checked:
        user = self.table
        delete = user.horizontalHeaderItem(item.column()).text()
        print ('%s' % user.item(item.row(), 0).text())
        print ('is marked for %s' % delete)
        print('%s Checked' % item.text())
        #self._list.append(item.row())
    else:
        print('%s' % item.text())
        print (item.row(),item.column())