Extending QSqlRelationalTableModel
-
I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:
model->setRelation(clientIndex, QSqlRelation("clients", "id", "firstname,lastname"));
The above code returns only the first field.
I want to make it return both fieds, firstname and lastname.What I 've done so far (my class is named QSqlRelationalTableModelB):
qsqlrelationaltablemodelb.h#ifndef QSQLRELATIONALTABLEMODELB_H #define QSQLRELATIONALTABLEMODELB_H #include <QtSql/qsqlrelationaltablemodel.h> class QSqlRelationalTableModelB : public QSqlRelationalTableModel { Q_OBJECT public: explicit QSqlRelationalTableModelB(QString text, QSqlRelationalTableModel *parent = 0); }; #endif // QSQLRELATIONALTABLEMODELB_H
qsqlrelationaltablemodelb.cpp
#include "QSqlRelationalTableModelB.h" QSqlRelationalTableModelB::QSqlRelationalTableModelB(QString text, QSqlRelationalTableModel *parent) : QSqlRelationalTableModel(parent) { QDebug() << "QSqlRelationalTableModelB!!!" }
I create a new instance of the class like this:
RTMB = new QSqlRelationalTableModelB("hi", this);
But when I build it, I get an error:
error: C2664: 'QSqlRelationalTableModelB::QSqlRelationalTableModelB(QString,QSqlRelationalTableModel *)' : cannot convert parameter 2 from 'RepairDevices *const ' to 'QSqlRelationalTableModel *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
wrote on 7 Feb 2017, 17:22 last edited byI 'm trying this which I describe in my first post:
@Panoss said in Extending QSqlRelationalTableModel:
I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:
model->setRelation(clientIndex, QSqlRelation("clients", "id", "firstname,lastname"));
The above code returns only the first field.
I want to make it return both fieds, firstname and lastname. -
I 'm trying this which I describe in my first post:
@Panoss said in Extending QSqlRelationalTableModel:
I 'm trying to extend QSqlRelationalTableModel, in order to try make some changes, e.g return two fields:
model->setRelation(clientIndex, QSqlRelation("clients", "id", "firstname,lastname"));
The above code returns only the first field.
I want to make it return both fieds, firstname and lastname.@Panoss said in Extending QSqlRelationalTableModel:
I 'm trying this which I describe in my first post
Yes, I read it, but I didn't understand. Do you mind explaining further? What do you want to return both fields? How would this in your mind work (i.e. how is one relation going to be associated with two tables? And what code returns only the first field, it's just not clear.
-
wrote on 7 Feb 2017, 17:58 last edited by Panoss 2 Jul 2017, 18:04
I have 2 tables:
- devices (fields: id, brand, client_id). The 3nd is foreign key.
- clients(with fields: id, firstname, lastname).
They are connected with devices.client_id = client.id.
When a device is displayed, I want to be also displayed firstname and lastname from clients.
-
Hi,
What about using a proxy model that would put these two fields together ?
-
wrote on 8 Feb 2017, 05:10 last edited by
I don't know what a proxy model is.
Could you give me an example? -
-
wrote on 16 Feb 2017, 17:31 last edited by Panoss
I restart this topic (as I had been distracted by other parts of the code):
I 'm trying to make it with QSortFilterProxyModel (I hope this is the correct way).
Let me remind you what I'm trying to acomplish:I have 2 tables:
1. devices (fields: id, brand, client_id). The 3nd is foreign key. 2. clients(with fields: id, firstname, lastname, address). They are connected with devices.client_id = client.id.
This is how I make the model ('model' is the model for devices):
model = new QSqlRelationalTableModel(this); model->setTable("devices");
When a device is displayed, I also display lastname from clients in a combo with this code:
QSqlTableModel *relClientModel = model->relationModel(clientIndex); ui.client_id_cbo->setModel(relClientModel); ui.client_id_cbo->setModelColumn(relClientModel->fieldIndex("lastname"));
So, how can I display and firstname and address from clients in two textboxes?
Like a master/detail form, where master is 'devices' and detail is 'clients', showing one client only (while, usually, in detail we have a list).
-
wrote on 16 Feb 2017, 19:11 last edited by
Finally, it was proved to be a lot simpler, using the model of the combo:
row = ui.client_id_cbo->currentIndex(); QModelIndex idx = ui.client_id_cbo->model()->index(row, 0); // first column QString client_firstname = idx.sibling(idx.row(), 1).data().toString(); QString client_address = idx.sibling(idx.row(), 3).data().toString(); ui.client_firstname_txt->setPlainText(client_firstname); ui.client_address_txt->setPlainText(client_address);
-
Finally, it was proved to be a lot simpler, using the model of the combo:
row = ui.client_id_cbo->currentIndex(); QModelIndex idx = ui.client_id_cbo->model()->index(row, 0); // first column QString client_firstname = idx.sibling(idx.row(), 1).data().toString(); QString client_address = idx.sibling(idx.row(), 3).data().toString(); ui.client_firstname_txt->setPlainText(client_firstname); ui.client_address_txt->setPlainText(client_address);
-
@Panoss Hello! You ask right question.
If you need a new functionality of QSqlRelationalTableModel than you should change this class and rebuild qt sources.wrote on 5 Jun 2022, 09:59 last edited by@kkmspb said in Extending QSqlRelationalTableModel:
you should change this class and rebuild qt sources
? You should rather subclass
QSqlRelationalTableModel
and add whatever functionality. You should not change Qt source code for this. -
@Panoss Hello! You ask right question.
If you need a new functionality of QSqlRelationalTableModel than you should change this class and rebuild qt sources.wrote on 3 Feb 2023, 17:04 last edited byMaybe you can see our free Qt project (4.8.1, Windows).
This project contains several classes inherited from the base classes QSqlTableModel and QTableView.
The PblSqlRelationalTableModel class is inherited from QSqlTableModel.
The PblTableView class is inherited from QTableView.
As a result, a combining class PblTableDlg was created with the composition PblTableView and PblSqlRelationalTableModel.
The QTableView class contains full functionality for managing a database table: creating, copying, deleting rows.
There is a search, selection by value. Fields in the form of combobox, checkbox,...
External links and substitutions from other tabs work automatically.Everything is configured simply in one configuration file config.cpp .
All three editing strategies are supported.