How do you get data from QTableWidget that user has edited (Python with PyQT)
Question: I asked a similar question before, but the result didn't work, and I don't
know why. Here was the original code:

    
    
    def click_btn_printouts(self):
        self.cur.execute("""SELECT s.FullName, m.PreviouslyMailed, m.nextMail, m.learnersDate, m.RestrictedDate, m.DefensiveDate FROM
                            StudentProfile s LEFT JOIN Mailouts m ON s.studentID=m.studentID""")
        self.all_data = self.cur.fetchall()
        self.search_results()
        self.table.setRowCount(len(self.all_data))
        self.tableFields = ["Check","Full name","Previously mailed?","Next mail","learnersDate","Restricted date","Defensive driving date"]
        self.table.setColumnCount(len(self.tableFields))
        self.table.setHorizontalHeaderLabels(self.tableFields)
        self.checkbox_list = []
        for i, self.item in enumerate(self.all_data):
            FullName = QtGui.QTableWidgetItem(str(self.item[0]))
            PreviouslyMailed = QtGui.QTableWidgetItem(str(self.item[1]))
            LearnersDate = QtGui.QTableWidgetItem(str(self.item[2]))
            RestrictedDate = QtGui.QTableWidgetItem(str(self.item[3]))
            DefensiveDate = QtGui.QTableWidgetItem(str(self.item[4]))
            NextMail = QtGui.QTableWidgetItem(str(self.item[5]))
            self.table.setItem(i, 1, FullName)
            self.table.setItem(i, 2, PreviouslyMailed)
            self.table.setItem(i, 3, LearnersDate)
            self.table.setItem(i, 4, RestrictedDate)
            self.table.setItem(i, 5, DefensiveDate)
            self.table.setItem(i, 6, NextMail)
            chkBoxItem = QtGui.QTableWidgetItem()
            chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
            chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
            self.checkbox_list.append(chkBoxItem)
            self.table.setItem(i, 0, self.checkbox_list[i])
    

The suggested code to add was this (indentation accurate) to the end of the
function:

    
    
        self.changed_items = set()
        self.table.itemChanged.connect(self.log_change)
    

And add the following function:

    
    
    def log_change(self):
        self.changed_items.add(self.item)
        print(self.item)
    

The expected print was the edited data, but what I get is the data before it
was edited.

I can't use QTableView and QtSql unless I can find a way to use it with an SQL
query, get every selected record into a list, and stop certain columns from
being edited. If anybody knows how to do these, that would be great, I just
really have no time to go through all the documentation myself at the moment.

All I want to do is have the user be able to change data from the
QTableWidget, and get that changed data as a record.

Basically, my end goal is to have the equivalent of
`setEditStrategy(QSqlTableModel.OnManualSubmit)` for QTableWidget.

I have been trying to figure this out for a while now and I just want it
sorted out, it is the last thing I need to do to finish this program for a
client.


Answer: It is always difficult to answer without a minimal working example, so I
produced one myself and put the suggestion from the [other
post](http://stackoverflow.com/questions/39742199/how-do-i-get-the-
information-that-the-user-has-changed-in-a-table-in-pyqt-with-p) in, modifying
it, such that it outputs the changed item's text and its position inside the
table.

    
    
    # runs with Python 2.7 and PyQt4
    from PyQt4 import QtGui, QtCore
    import sys
    
    
    class App(QtGui.QMainWindow):
    
        def __init__(self, parent=None):
            super(App, self).__init__(parent)
            self.setMinimumSize(600,200)
    
            self.all_data = [["John", True, "01234", 24],
                             ["Joe", False, "05671", 13],
                             ["Johnna", True, "07145", 44] ]
    
            self.mainbox = QtGui.QWidget(self)
            self.layout = QtGui.QVBoxLayout()
            self.mainbox.setLayout(self.layout)
            self.setCentralWidget(self.mainbox)
    
            self.table = QtGui.QTableWidget(self)
            self.layout.addWidget(self.table)
    
            self.button = QtGui.QPushButton('Update',self)
            self.layout.addWidget(self.button)
    
            self.click_btn_printouts()
            self.button.clicked.connect(self.update)
    
        def click_btn_printouts(self):
    
            self.table.setRowCount(len(self.all_data))
            self.tableFields = ["Name", "isSomething", "someProperty", "someNumber"]
            self.table.setColumnCount(len(self.tableFields))
            self.table.setHorizontalHeaderLabels(self.tableFields)
            self.checkbox_list = []
            for i, self.item in enumerate(self.all_data):
                FullName = QtGui.QTableWidgetItem(str(self.item[0]))
                FullName.setFlags(FullName.flags() & ~QtCore.Qt.ItemIsEditable)
                PreviouslyMailed = QtGui.QTableWidgetItem(str(self.item[1]))
                LearnersDate = QtGui.QTableWidgetItem(str(self.item[2]))
                RestrictedDate = QtGui.QTableWidgetItem(str(self.item[3]))
    
                self.table.setItem(i, 0, FullName)
                self.table.setItem(i, 1, PreviouslyMailed)
                self.table.setItem(i, 2, LearnersDate)
                self.table.setItem(i, 3, RestrictedDate)
    
            self.changed_items = []
            self.table.itemChanged.connect(self.log_change)
    
        def log_change(self, item):
            self.table.blockSignals(True)
            item.setBackgroundColor(QtGui.QColor("red"))
            self.table.blockSignals(False)
            self.changed_items.append(item)
            print item.text(), item.column(), item.row()
    
        def update(self):
            print "Updating "
            for item in self.changed_items:
                self.table.blockSignals(True)
                item.setBackgroundColor(QtGui.QColor("white"))
                self.table.blockSignals(False)
                self.writeToDatabase(item)
    
        def writeToDatabase(self, item):
            text, col, row = item.text(), item.column(), item.row()
            #write those to database with your own code
    
    
    if __name__=='__main__':
        app = QtGui.QApplication(sys.argv)
        thisapp = App()
        thisapp.show()
        sys.exit(app.exec_())
    

You may use this example now to refer to any further problems.

