[PyQt] Master-detail relationship with plain QTableView tables (no SQL database)

Vadym Honcharuk intereco at gmail.com
Sat Jun 5 07:34:20 BST 2010


Thanks a lot, Mark!

thereis  problem rather how to read data from file than write it in
using QDataStream. As all tree tables may contain different  and
unknown number of rows I find difficults to iterate row when read data
from stream. For example:

write data:

            stream = QtCore.QDataStream(fh)
            stream.writeInt32(MAGIC_NUMBER)
            stream.writeInt16(FILE_VERSION)
            stream.setVersion(QtCore.QDataStream.Qt_4_5)

            for row in range(self.srcTableModel.rowCount()):
		equipment = self.srcTableModel.data(self.srcTableModel.index(row,
EQUIPMENT)).toString()
		srcCode = self.srcTableModel.data(self.srcTableModel.index(row,
SRCCODE)).toInt()[0]
		srcName = self.srcTableModel.data(self.srcTableModel.index(row,
SRCNAME)).toString()
                stream << QtCore.QString(equipment)
                stream.writeInt32(srcCode)
		stream << QtCore.QString(srcName)

                      for row in range(self.frmTableModel.rowCount()):
		           formationSrcName =
self.frmTableModel.data(self.frmTableModel.index(row,
FORMATIONSRCNAME)).toString()
		           evolvingGasDepletion =
self.frmTableModel.data(self.frmTableModel.index(row,
EVOLVINGGASDEPLETION)).toInt()[0]
		           evolvingGasTemperature =
self.frmTableModel.data(self.frmTableModel.index(row,
EVOLVINGGASTEMPERATURE)).toInt()[0]
		           astream << QtCore.QString(formationSrcName)
		           astream.writeInt32(evolvingGasDepletion)
		           astream.writeInt32(evolvingGasTemperature)

read data

            stream = QtCore.QDataStream(fh)
            magic = stream.readInt32()
            if magic != MAGIC_NUMBER:
                raise IOError, "unrecognized file type"
            fileVersion = stream.readInt16()
            if fileVersion != FILE_VERSION:
                raise IOError, "unrecognized file type version"
            stream.setVersion(QtCore.QDataStream.Qt_4_5)
	    self.srcTableModel.ships = []
	    self.frmTableModel.ships = []
            while not stream.atEnd():
                equipment = QtCore.QString()
                stream >> equipment
                srcCode = stream.readInt32()
                srcName = QtCore.QString()
		stream >> srcName
                self.srcTableModel.ships.append(srcShip(equipment,
srcCode, srcName))

                formationSrcName = QtCore.QString()
		stream >> formationSrcName
		evolvingGasDepletion = stream.readInt32()
		evolvingGasTemperature = stream.readInt32()

self.frmTableModel.ships.append(frmShip(formationSrcName,
evolvingGasDepletion, evolvingGasTemperature))

It works only if  each tables contains one row.

thanks a lot!

with best regards,
-vadym


2010/5/20 Mark Summerfield <list at qtrac.plus.com>:
> On 2010-05-20, Vadym Honcharuk wrote:
>> 2010/5/9 Mark Summerfield <list at qtrac.plus.com>:
> [snip]
>> Thank you, Mark!
>>
>> One more question in this issue is how to save data from these three
>> tables with different models in one file by QDataStream module. The
>> point of problem in that save method calling  from their models itself
>> and if I have  three models  - every will try open same file and save
>> data in series? But seems this is not too correct desicion...
>>
>> With best rgards,
>> -vadym
>
> Hi Vadym,
>
> There are many possible approaches to this.
>
> One approach is to make just one of the models (or a new "ModelManager"
> class) responsible for saving. This class would be the only one that had
> a save method and would work somewhat like this:
>
>    open the file
>    for each row in the "main" model:
>        write the row's data
>        for each row in the "sub" model that corresponds to main data's row:
>            write the sub model row data
>            # and if there's a sub-sub model iterate over the sub-sub
>            # model's rows that correspond to the sub model's rows
>        # and if there's more than one sub model, iterate over each
>        # one's corresponding rows & write their data
>
> Other approaches are equally valid. For example, you could save one file
> per model which is the simplest and probably the most maintainable
> approach---and the one that I personally prefer.
>
> Nor is saving each model's data in series necessarily bad---but it does
> require you to synchronize and is more risky than the other two
> approaches I've mentioned.
>
> --
> Mark Summerfield, Qtrac Ltd, www.qtrac.eu
>    C++, Python, Qt, PyQt - training and consultancy
>        "Advanced Qt Programming" - ISBN 0321635906
>



-- 
Vadym Honcharuk, nic-handle:vh20-uanic
PE Inter-Eco,  http://inter-eco.com.ua
PO Box 3381, Vinnytsya, Ukraine, 21032
phone: +38 0432 699-096, tel/fax 550-578
phone mobile: +38 066 7323736, 7323735


More information about the PyQt mailing list