[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)

            for row in range(self.srcTableModel.rowCount()):
		equipment = self.srcTableModel.data(self.srcTableModel.index(row,
		srcCode = self.srcTableModel.data(self.srcTableModel.index(row,
		srcName = self.srcTableModel.data(self.srcTableModel.index(row,
                stream << QtCore.QString(equipment)
		stream << QtCore.QString(srcName)

                      for row in range(self.frmTableModel.rowCount()):
		           formationSrcName =
		           evolvingGasDepletion =
		           evolvingGasTemperature =
		           astream << QtCore.QString(formationSrcName)

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"
	    self.srcTableModel.ships = []
	    self.frmTableModel.ships = []
            while not stream.atEnd():
                equipment = QtCore.QString()
                stream >> equipment
                srcCode = stream.readInt32()
                srcName = QtCore.QString()
		stream >> srcName
srcCode, srcName))

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

evolvingGasDepletion, evolvingGasTemperature))

It works only if  each tables contains one row.

thanks a lot!

with best regards,

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