<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><font face="Helvetica, Arial, sans-serif">You don't need to
"invent a new MVC", just use the MVC in PyQt:<br>
QTableView is usable with any type of model that can be built
using,<br>
for example, QAbstractTableModel. I'm attaching some example
code<br>
that has an "ODTableModel" for which the "model" is a list of
OrderedDict<br>
instances, and an "ObjectTableModel" that subclasses
ODTableModel to<br>
use arbitrary objects, which could be, for example, sqlalchemy
objects.<br>
You write your own code to do the sqlalchemy interactions with
the<br>
database, and just call them from the ObjectTableModel ...<br>
that is one easy way to apply the PyQt MVC with an ORM back-end.<br>
This example code is based on code I use in my app, which does<br>
exactly what I describe.<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">You would want to
create a subclass of QTableView that could be<br>
called "ObjectTableView" that takes a list of object instances
and a "view"<br>
(a list of the names of the attributes that you want to
display). That's<br>
where you could create, say, a context menu that gives you
various<br>
options like to delete an object, etc. Again, the objects can
be sqlalchemy<br>
objects, and you can do db operations using them (sqlalchemy
objects are<br>
basically always in a transaction, which you can use to do
operations and<br>
commits, etc.).<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">Steve<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">On 6/10/20 5:42 PM,
Nenad Lamza wrote:
<blockquote type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<pre class="moz-quote-pre" wrap=""><div class="moz-txt-sig">Thanks Sibylle,
do you know how much work it takes, inventing completely new MVC? I haven't found any example of it (SQLAlchemy, psycopg2, QTableView, (QDataWidgetMapper) together) on the web. Can you put any link. It is much more practical and logical to use PyQt Sql classes with QTableView. I avoid QAbstractItemModel and use it only for very very special cases.
It is so sad that Qt and PyQt haven't documented what PostgreSQL drivers and what version of them we should use with particular Qt/PyQt versions.
And because of that you suggest me to turn the upside down the whole Qt MVC framework. I won't do that. The app with standard PyQt MVC, QSqlDatabase, QSqlQuery, QSqlQueryModel, QSqlQueryModel,..., QTableView works so great that I think it is easier to (randomly) find the right combination of drivers than write the whole new framework.
Nenad
Am 10.06.2020 um 14:34 schrieb Nenad Lamza:
</div></pre>
<blockquote type="cite" style="color: #000000;">
<pre class="moz-quote-pre" wrap="">Thanks Barry (and also Dennis) to your answers.
So, I have a choice:
1. Use standard PyQt MVC, QSqlDatabase, QSqlQuery, QSqlQueryModel,
QSqlQueryModel, QTableView,..., but without ORM like SqlAlchemy, and
potentially have problems with PostgreSQL drivers
2. Use other PostgreSQL drivers like psycopg2 and don't use PyQt MVC and
Sql classes and use ORM like SqlAlchemy with business logic and objects.
Isn't the whole point of (Py)Qt MVC to use all those classes I
mentioned? I can't imagine in that case (without PyQt Sql classes and
using SqlAlchemy) how would I present data to the user in eg. QTableView
or some other GUI table on screen? Building my own MVC (it takes years)?
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">You can use the Qt Model-View classes, just without QtSql. Instead you
could create your own Model class, subclassed from QAbstractItemModel or
use a QStandardItemModel (or subclass that). And put your data, got via
SQLAlchemy or directly from psycopg2, into that model. QTableView and
QDataWidgetMapper can use that just as well as the QSql...Model classes.
HTH
Sibylle</pre>
</blockquote>
<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif"></font><br>
</p>
</body>
</html>