[PyQt] beginner, array setdata, write back

ulrichD ulrich at dorda.net
Thu May 22 16:32:27 BST 2008


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?
-- 
View this message in context: http://www.nabble.com/beginner%2C-array-setdata%2C-write-back-tp17406877p17406877.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list