Using QSqlQueryModel instead of QStandardItemModel
-
Hi,
First thing to keep in mind: QSqlQueryModel is read-only but there are examples to make it writable.
If you want to filter things or modify what is shown by the view, a QSortFilterProxyModel might be of interest.
There are several ways to customise the header views content depending on what view you are using to show your table data.
All in all, no, you're not crazy, but I recommend starting small and modify the models gradually.
-
Hi,
First thing to keep in mind: QSqlQueryModel is read-only but there are examples to make it writable.
If you want to filter things or modify what is shown by the view, a QSortFilterProxyModel might be of interest.
There are several ways to customise the header views content depending on what view you are using to show your table data.
All in all, no, you're not crazy, but I recommend starting small and modify the models gradually.
@SGaist
OK, thank you.Then let's take one specific problem:
The columns which come back from the database from a
SELECTare insufficient for me. I need an extra column (at least for display), whose value is calculated in Qt code based on one of the columns which is returned. In the code above thismodel.setData(model.index(0, LetterColumns.LtType), letterTypeMap[letter.type]).Can I do this by adding my own column to the data/table returned from SQL query? (I don't think so.) Can I do this in
QTree/TableView? i.e. have the view have its own extra column beyond what is the model? (Again, perhaps not?) Or else I seem to be stuck.... -
@SGaist
OK, thank you.Then let's take one specific problem:
The columns which come back from the database from a
SELECTare insufficient for me. I need an extra column (at least for display), whose value is calculated in Qt code based on one of the columns which is returned. In the code above thismodel.setData(model.index(0, LetterColumns.LtType), letterTypeMap[letter.type]).Can I do this by adding my own column to the data/table returned from SQL query? (I don't think so.) Can I do this in
QTree/TableView? i.e. have the view have its own extra column beyond what is the model? (Again, perhaps not?) Or else I seem to be stuck....There's a proxy model for that: KExtraColumnsProxyModel
-
There's a proxy model for that: KExtraColumnsProxyModel
@VRonin said in Using QSqlQueryModel instead of QStandardItemModel:
There's a proxy model for that: KExtraColumnsProxyModel
Oohh --- thank you! That's a third-party package I'd need to install and distribute/have end-users install, which I'm not dead keen on.... Does that imply that there is indeed no in-built facility for my extra column in Qt alone?
-
There is not but that particular class can be just copy pasted and just adding a few lines to your license (that class comes from KDE which uses LGPL). No need to compile/distribute external binaries
-
There is not but that particular class can be just copy pasted and just adding a few lines to your license (that class comes from KDE which uses LGPL). No need to compile/distribute external binaries
@VRonin
I have now had a chance to investigate KExtraColumnsProxyModel.This seems like what I was looking for, and I will mark this post as solved.
However, unfortunately it has made me realise I cannot use any third-party code for Qt. It all tends to be written in C++ (understandably). I am using PyQt (not my choice!). Which leaves me with:
- I don't fancy introducing C++ code which I need to compile for my multi-platform targets.
- Even if I did, I don't think you can auto-generate PyQt/Python bindings.
- And looking at that code I don't fancy rewriting it natively in Python.
Unless there are any PyQt/Python experts here? Which is all rather a shame going forward, as it limits me to native Qt-supplied solutions... :(
-
@SGaist
I'd be obliged if you would comment on the following:I did look at sip, and more specifically https://riverbankcomputing.com/pipermail/pyqt/2016-May/037557.html, and https://steveire.wordpress.com/2016/05/18/generating-python-bindings-with-clang/ which discusses something like
clang->sipto generate Python bindings for C++. The gist was that the source had to be annotated in a particular way (via XML, "rules files"??) to allow it to work, and even then there were issues and it needed some manual help. So it did not look straightforward?Even if it does not apply here, I would like to understand whether generating PyQt/Python bindings from arbitrary C++ source is a totally simple, automated process or whether in practice it's not so simple? Thanks.
-
Looks pretty interesting and I'd say worth a shot.
Well, the proposition of Steve makes it automated. Otherwise take a look at the PyMyLabel project and the sip example, the class you need isn't overly complicated so writing the corresponding sip files wouldn't be that complex.