[PyQt] beginner, array setdata, write back

Matt Newell newellm at blur.com
Fri May 23 03:16:24 BST 2008


On Thursday 22 May 2008 08:32:27 ulrichD wrote:
> I'm a novice to pyqt and search for something so basic, it's embarrassing.
> Unfortunately I still couldn't find any answer in the net (probably don't
> even know what to search for)
> (in addition i hope I know dontt post twice now, the first time I got an
> mail saying it was rejected)
>
> I simply want a gui to show me the content of an numpy array. When I change
> a value in the gui, i want this to be written back into the array.
>
> I tried something like:
>
>
> XXXXXXXXXXXXXXXXXXXXXXXXXX
> from __future__ import division
> import sys
> from math import *
> from PyQt4.QtCore import *
> from PyQt4 import QtCore
> from PyQt4.QtGui import *
>
> class sheet(QDialog):   #By inheriting QDialog we get a blank form, that
> is, a gray rectangle, and some convenient behaviors and methods
>     def __init__(self, arr, parent=None):    #A widget that has no parent
> becomes a top-level window
>         QWidget.__init__(self)
>
>         tablemodel = MyTableModel(arr, self)
>         self.tabmod=tablemodel
> 	tableview = QTableView()
>         tableview.setModel(tablemodel)
>
>         layout = QVBoxLayout(self)
>         layout.addWidget(tableview)
>         self.setLayout(layout)
>
> class MyTableModel(QAbstractTableModel):
>     def __init__(self, datain, parent=None):
>         QAbstractTableModel.__init__(self, parent)
>         self.arraydata = datain
>
>     def rowCount(self, parent):
>         return len(self.arraydata)
>
>     def columnCount(self, parent):
>         return len(self.arraydata[0])
>
>     def data(self, index, role):
>         if not index.isValid():
>             return QVariant()
>         elif role != Qt.DisplayRole:
>             return QVariant()
>         return QVariant(self.arraydata[index.row()][index.column()])
>
>     def isEditable(self, index):
>         """ Return true if the index is editable. """
>         return True
>
>     def flags(self, index):   #function is called to chaeck if itmes are
> changable etc, index is a PyQt4.QtCore.QModelIndex object
>         if not index.isValid():
>             return QtCore.Qt.ItemIsEnabled
>
>         ret_flags = QtCore.Qt.ItemIsSelectable |
> QtCore.QAbstractItemModel.flags(self, index)
>         if self.isEditable(index):
>             #print index.row()  #returns the selected elements row
> 	    #print index.column()
> 	    ret_flags = ret_flags | QtCore.Qt.ItemIsEditable
>         return ret_flags
>
>
>     def setData(self, index, value, role):
>       """ if a item is edited, this command is called
>       value.toString() constains the new value
>       cahnge here to have it evaluate stuff!"""
>       self.arraydata[index.row(),index.column()]=value.toString()
> #<<<<<<<<<<< the problem (i think)
>
>
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>
> if this file is called ugui.py, it hem run it from the shell with
>
> import ugui
> arr=rand(3,3)
> form = ugui.sheet(arr)
> form.show()
>
> I think I encounter two problems:  I access the newly entered data
> incorrectely and there seems to be a variable type issue.
>
> Maybe someone can help me out here?

You need to return the data with both Qt.DisplayRole AND Qt.EditRole.  You 
need to return True in setData, and you need to use value.toFloat() or 
toInt() or whatever type you want that the numpy array will accept.

btw, next time it would be nice if the example would be runnable just by 
pasting into a text file, it took me a while to figure out where the 
rand(3,3) function came from...

Also, you might have to mess with the delegate to get the desired editting 
precision.

Matt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: numpy_model_working.py
Type: application/x-python
Size: 1923 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080523/399770f2/numpy_model_working.bin


More information about the PyQt mailing list