qsqltablemodel

How to display content of multiple QSqlTableModels in one QTableView?

人盡茶涼 提交于 2019-12-19 04:58:32
问题 I have a MySql table, let's call it x : CREATE TABLE x ( Id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, A int unsigned NOT NULL, B int, FOREIGN KEY (A) REFERENCES y(Id) ); And then I have another table, let's call it y : CREATE TABLE y ( Id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT, First varchar(255), Last varchar(255) ); I want to display table x in one QTableView and in place of column A from table x I want to display columns First and Last from table y from row whose Id is

Set color to a QTableView row

我只是一个虾纸丫 提交于 2019-12-18 03:37:08
问题 void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ model = new QSqlQueryModel(this); model->setQuery(sql); } With this method i can set a QSQlQueryModels to my QTableviews. But How i can set color to a row based on a cell value? 回答1: The view draws the background based on the Qt::BackgroundRole role of the cell which is the QBrush value returned by QAbstractItemModel::data(index, role) for that role. You can subclass the QSqlQueryModel to redefine data()

Sort by Multiple Columns in PyQt Model

大城市里の小女人 提交于 2019-12-13 19:13:15
问题 I want to use the PyQt equivalent of the following SQL statement in my model/view-based PyQt application: SELECT * FROM table ORDER BY foo, bar How do I sort by multiple columns in a QSqlTableModel , especially since setSort() accepts a single column argument? 回答1: It seems there's an alternative to setSort() , called setFilter() . From the PyQt docs: QSqlTableModel.setFilter (self, QString filter) Sets the current filter to filter. The filter is a SQL WHERE clause without the keyword WHERE

QSqlRelationalTableModel with QSqlRelationalDelegate not working behind QAbstractProxyModel

十年热恋 提交于 2019-12-12 01:55:38
问题 I need to swap the rows and columns of a QSqlRelationalTableModel. After a lot of searching, I wrote a little proxymodel to flip the rows and the columns. It is partly working. The relations in the table are resolved and showed but the dropboxes to choose them gets lost. Also, how do I get them to update? Here is a little self-contained script that reproduces the behavior. Where is my error? I have strong suspicion that it has to do with the signals and slots of the models but I haven't found

QSQLTableModel inheritor and QTableView

走远了吗. 提交于 2019-12-11 08:14:22
问题 I wrote QSQLTableModel inheritor for working with qml and it's work well. I need use it with QTableView too, data shows, but I cannot modify it - when I edit everything is ok, but all changes drop when I get out from field (I know about editStrategy, but the problem occurs earlier). I suppose that something wrong with virtual function, but I cant undestant what. If i create QSqlTableModel with the same parameters, everything is ok. Somebody have any idea how can i fix this? My code: h: class

Implementing setEditStrategy in editable QSqlQueryModel

社会主义新天地 提交于 2019-12-05 05:16:14
This is a follow-up to this question . In there, we created an editable subclass of QSqlQueryModel, to use with complex queries. Now I need to add a functionality like QTableModel's setEditStrategy so I can cache all changes and accept or revert them using buttons. PyQt apparently doesn't allow multiple inheritance and I cannot find sufficient documentation to re-implement this method in my custom model, therefor here's the question: How can I re-implement QSqlTableModel.setEditStragety (or something like it) including RevertAll() and SubmitAll() in an editable QSqlQueryModel? Here's a CVME:

When or how to use fetchMore() on QSqlTableModel with a SQLite database for rowCount() to work?

别来无恙 提交于 2019-12-04 09:59:37
My class DataTable is derived from QAbstractTableModel. It uses a QSqlTableModel object internally to fetch data from a db table. It represents a record for every row in the db (it does more but the record count is always the number of rows in the db table). With MySql, my DataTable::rowCount() implementation just calls rowCount() on the QSqlTableModel, which works nicely. Now with SQLite, Qt's SQLite driver returns a row count of 256 if there are more than 256 rows in the db table, so my DataTable class also returns 256 - which is wrong. The documentation tells me to call while (sql_model-

Editable QTableView of complex SQL query

江枫思渺然 提交于 2019-12-02 12:31:16
How can I make an editable QTableView displaying data from a complex SQLite query? I need to fill a QTableView with data from several SQLite tables. This needs to be editable by the user. As the queries are a bit complex (including JOINs and CASE WHEN etc.), I'm doing this via a QSqlTableModel and a QSqlQuery. I have been told, however, that this is not how QSqlTableModels should be used. So, can someone please show me how to get a result like the one shown here via the proper way? Also, while my QTableViews are editable, the results don't seem to get stored in the SQLite database. (When I

QTableView formatting numbers

陌路散爱 提交于 2019-12-02 03:19:05
I have created a delegate and i'm able to align and boldface the numbers on the table. I would like to force them to have two decimal places, for example 1.2 should show as 1.20. This is the header of the delagete: #ifndef TOTALDELEGATE_H #define TOTALDELEGATE_H #include <QObject> #include <QStyledItemDelegate> class TotalDelegate : public QStyledItemDelegate { public: TotalDelegate(); virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; }; #endif // TOTALDELEGATE_H Here is the implementation: #include "totaldelegate.h"

QTableView formatting numbers

做~自己de王妃 提交于 2019-12-02 02:32:00
问题 I have created a delegate and i'm able to align and boldface the numbers on the table. I would like to force them to have two decimal places, for example 1.2 should show as 1.20. This is the header of the delagete: #ifndef TOTALDELEGATE_H #define TOTALDELEGATE_H #include <QObject> #include <QStyledItemDelegate> class TotalDelegate : public QStyledItemDelegate { public: TotalDelegate(); virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q