2

I'm writing a simple student scores manager for practicing programming on PyQt (I don't want to use terrible Visual Basic anymore). But I had a big problem on choose data models.

I found QSqlTableModel first, it is a great model with auto-updating. The trouble is, I need to use a lot of SQL (JOIN, WHERE) to select data from database. QSqlTableModel has select() and filter() only.

Then I found QSqlQueryModel, but it is read only. So I rewrite its setData() method. So it is read-write now. Unfortunately, QSqlQueryModel less usable features than QSqlTableModel.

As you see, if I can using SQL with QSqlTableModel, I can resolve all my problems.

So...?

1 Answer 1

2

QSqlTableModel has the setQuery method, which you can use to set a custom query, something like:

model = QSqlTableModel()
query = QSqlQuery(your_query)
model.setQuery(query)

However, the Qt documentation states:

This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

Sign up to request clarification or add additional context in comments.

2 Comments

I think setFilter() is what you are looking for
The same cited documentation gives examples of simple tables that unnecessarily use setQuery(). So the documentation is a bit schizofrenic about this subject :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.