Search code examples
pythoninputpyqtqtablewidget

Python and PyQt: Get Input from QTableWidget?


So, I have a very simple question: How can I get the input from a QTableWidget and put it to some lists(first collumn to the first list, second collumn to the second list and so on...)?

I've googled but haven't really found anything...

Update:

Actually, I figured out this:

self.tableWidget.cellChanged.connect(self.cellchanged)


def cellchanged(self):
    cur = self.tableWidget.currentColumn()
    cur2 = self.tableWidget.currentRow()
    y = children[cur]
    text = self.tableWidget.currentItem().text()
    y.insert(cur2, text)

But I get this error when I update the table (it just shows, it doesn't hurt the program):

def updatetable(self):
        self.tableWidget.setRowCount(0)
        self.tableWidget.setRowCount(100)
        r=0
        c=0
        for x in children:
            for i in x:
                newItem = QtGui.QTableWidgetItem(i)
                self.tableWidget.setItem(r, c, newItem)
                r += 1
            r = 0
            c += 1

Error:

  if self.tableWidget.currentItem().text():
AttributeError: 'NoneType' object has no attribute 'text'

Update3:

Solved it:

    self.tableWidget.cellChanged.connect(self.cellchanged)


def cellchanged(self):
    cur = self.tableWidget.currentColumn()
    cur2 = self.tableWidget.currentRow()
    y = children[cur]
    self.tableWidget.cellActivated.connect(self.cell) 
def cell(self):
    text = self.tableWidget.currentItem().text()
    y.insert(cur2, text)

Solution

  • Here is an example of how to use a QTableView, and a set of QListView objects, with a single QStandardItemModel. The benefit of this approach is you only need to maintain data in one spot: the model. The views simply reflect whatever is in the model.

    In this example, each list is set to view a different column. Watch what happens when you change data. It updates everywhere. This is because internally the model is emitting signals when its data changes. The views simply listen to the model. If you didn't want to use QListView and simply wanted some basic list data structures, you would manually connect to signals emitted from the model, and update your data on demand.

    from PyQt4 import QtCore, QtGui
    
    class Widget(QtGui.QWidget):
    
        def __init__(self, *args, **kwargs):
            super(Widget, self).__init__(*args, **kwargs)
            self.resize(800,600)
    
            self.vlayout = QtGui.QVBoxLayout(self)
            self.table = QtGui.QTableView()
            self.vlayout.addWidget(self.table)
    
            self.hlayout = QtGui.QHBoxLayout()
            self.list1 = QtGui.QListView()
            self.list2 = QtGui.QListView()
            self.list3 = QtGui.QListView()      
            self.hlayout.addWidget(self.list1)
            self.hlayout.addWidget(self.list2)
            self.hlayout.addWidget(self.list3)
    
            self.vlayout.addLayout(self.hlayout)
    
            self.model = QtGui.QStandardItemModel(10,10,self)
            self.table.setModel(self.model)
    
            self.list1.setModel(self.model)
            self.list1.setModelColumn(0)
            self.list2.setModel(self.model)
            self.list2.setModelColumn(1)
            self.list3.setModel(self.model)
            self.list3.setModelColumn(2)
    
            self.populateTable()
    
        def populateTable(self):
            for row in xrange(10):
                for col in xrange(10):
                    item = QtGui.QStandardItem('%d-%d' % (row, col))
                    self.model.setItem(row, col, item)
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication([])
        window = Widget()
        window.show()
        window.raise_()
        sys.exit(app.exec_())