How to use an Object of type QSqlTableModel in another function of the same class?
-
Hi People I'm new here and I formerly developed Software in RAD Studio. I started with QT Creator because I need to... Don't ask please...
To my question: I am developing a database application and I need to use a model based TableView in a MainWindow-Class. For this to work, I decided to use the QSqlTableModel.
I Use the following code to bring it to work - and as far as I can see - it works...The Database connection "DB_Connection" is already set as private "global " object in the class of MainWindow and gets the connection via a function called setDB like so:
//mainwindow.h class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QSqlTableModel getSQLTableModel(); void setSQLTableModel(QSqlTableModel *model); void setDB(QSqlDatabase db); QSqlTableModel* getTableModel(); void onOpenMainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; QSqlTableModel *TableModel; QSqlDatabase DB_Connection; };
//mainwindow.cpp void MainWindow::onOpenMainWindow() { QSqlTableModel *model = new QSqlTableModel; setSQLTableModel(model); model->setTable("mytable"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); model->setHeaderData(0, Qt::Horizontal, tr("oneOfManyColumns")); model->setHeader... . . -> and so on ui->view->setModel(model); ui->view->hideColumn(0); }
//mainwindow.cpp void MainWindow::setSQLTableModel(QSqlTableModel *model) { this->TableModel = model; }
//mainwindow.cpp void MainWindow::on_pushButton_clicked() { QSqlTableModel *model = TableModel; model->submitAll(); }
It compiles without errors. Also it shows the DB-Data correctly in the tableView. I can also click it, edit, etc. BUT when i push the button it only saves the data once. After that I can still make changes in the tableView but I can't save it anymore. I don't get errors. It must have to do with the "model"-object. But I don't know how to fix that.
So generally spoken: How can I use a button to save changes, that a user makes inside the tableView connected with the QSqlTableModel? I explicitly DON'T want it to be updated right with the change!
Thanks in advance for your ideas and I hope someone can help me...
EDIT: I don't usually instanciate objects with "new" but I had no other Idea how it would work...
Greetz, mooswitz
-
Hi and welcome to devnet,
There are several issues with your code but one of the most important thing: you don't check that submitAll succeeds. That's the first thing you should add.
Next thing: don't store QSqlDatabase member variables. That's not how the class shall be used and is explained in the documentation.
Why are you creating the function local
model
pointer in your slot ? You can call submitAll directly on your member variable. -
Hi and welcome to devnet,
There are several issues with your code but one of the most important thing: you don't check that submitAll succeeds. That's the first thing you should add.
Next thing: don't store QSqlDatabase member variables. That's not how the class shall be used and is explained in the documentation.
Why are you creating the function local
model
pointer in your slot ? You can call submitAll directly on your member variable.@SGaist said in How to use an Object of type QSqlTableModel in another function of the same class?:
Why are you creating model in your slot ? You can call submitAll directly on your member variable.
OP is not doing that.
QSqlTableModel *model = TableModel;
. So just getting a reference to the member variable. -
@SGaist said in How to use an Object of type QSqlTableModel in another function of the same class?:
Why are you creating model in your slot ? You can call submitAll directly on your member variable.
OP is not doing that.
QSqlTableModel *model = TableModel;
. So just getting a reference to the member variable. -
@SGaist
First of all: thanks for your reply. So besides of that issue you mentioned, what else is wrong? - you mentioned several issues... I can only learn from it if I know what's wrong.To your Question: If I use the Variable
TableModel
from inside the class I get an error that it can't be accessed. That is why I set the reference with
model
Next thing: don't store QSqlDatabase member variables. That's not how the class shall be used and is explained in the documentation.
I understand that and I am aware of it. But I need the database connection in several Widgets. This is truly something new for me since in RAD Studio you had a very different concept of accessing the database. But apart of that I need an option to set the credentials for the database only once and still can access it in every window. I'd be indeed very happy if you'd show me a better option.
Greetz, mooswitz
-
@SGaist
First of all: thanks for your reply. So besides of that issue you mentioned, what else is wrong? - you mentioned several issues... I can only learn from it if I know what's wrong.To your Question: If I use the Variable
TableModel
from inside the class I get an error that it can't be accessed. That is why I set the reference with
model
Next thing: don't store QSqlDatabase member variables. That's not how the class shall be used and is explained in the documentation.
I understand that and I am aware of it. But I need the database connection in several Widgets. This is truly something new for me since in RAD Studio you had a very different concept of accessing the database. But apart of that I need an option to set the credentials for the database only once and still can access it in every window. I'd be indeed very happy if you'd show me a better option.
Greetz, mooswitz
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
I understand that and I am aware of it. But I need the database connection in several Widgets.
That's why you have connection names.
QSqlDatabase is a value class. Each time you need it just doauto db = QSqlDatabase::database()
https://doc.qt.io/qt-6/qsqldatabase.html@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
But apart of that I need an option to set the credentials for the database only once and still can access it in every window.
That's how it works. You define connection with all the parameters, then can retrieve it in any part of your program with a call to
database()
. -
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
I understand that and I am aware of it. But I need the database connection in several Widgets.
That's why you have connection names.
QSqlDatabase is a value class. Each time you need it just doauto db = QSqlDatabase::database()
https://doc.qt.io/qt-6/qsqldatabase.html@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
But apart of that I need an option to set the credentials for the database only once and still can access it in every window.
That's how it works. You define connection with all the parameters, then can retrieve it in any part of your program with a call to
database()
.@artwaw
Thanks for your answer. I read that document several times but now I understand the concept way better.I still have the problem, that I don't know how to securely trigger "submitAll()" from QSqlTableModel from inside another function like a button.
The strange thing is, that it works with the code from my first post. - but only once. When I then close the window "MainWindow" while the application is still running, then I can retrigger "submitAll()" once again and for this one time it works again.
I need a way that a user can trigger that button endless times to make as many changes as he wants...
Also I will implement the following statement tomorrow to check if it actually worked:
if(model.submitAll()){ qDebug << "worked"; } else{ qDebug << "did'nt work"; }
I'll report back tomorrow.
Greetz, mooswitz
-
@SGaist
First of all: thanks for your reply. So besides of that issue you mentioned, what else is wrong? - you mentioned several issues... I can only learn from it if I know what's wrong.To your Question: If I use the Variable
TableModel
from inside the class I get an error that it can't be accessed. That is why I set the reference with
model
Next thing: don't store QSqlDatabase member variables. That's not how the class shall be used and is explained in the documentation.
I understand that and I am aware of it. But I need the database connection in several Widgets. This is truly something new for me since in RAD Studio you had a very different concept of accessing the database. But apart of that I need an option to set the credentials for the database only once and still can access it in every window. I'd be indeed very happy if you'd show me a better option.
Greetz, mooswitz
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
from inside the class I get an error that it can't be accessed.
This does not make sense. Your code goes:
QSqlTableModel *model = TableModel; model->submitAll();
So it is already "accessing member variable
TableModel
from within the class". So you can just go:TableModel->submitAll();
Let us know about the result of
submitAll()
. -
I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
https://pastebin.com/e2SVXZVbHere I made an error. It was meant so say:
"In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
So I can later check for isOpen(). That seems a lot better to me." -
I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
https://pastebin.com/e2SVXZVbHere I made an error. It was meant so say:
"In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
So I can later check for isOpen(). That seems a lot better to me."@mooswitz
I don't know why you are getting "spam". I wouldn't normally look at pastebin or anything else external.You now have
static QSqlDatabase db;
as a member variable. Which goes against what the docs tell you (not) to do. Get rid of a member variable and use
QSqlDatabase::database()
static function per the docs. -
@mooswitz
I don't know why you are getting "spam". I wouldn't normally look at pastebin or anything else external.You now have
static QSqlDatabase db;
as a member variable. Which goes against what the docs tell you (not) to do. Get rid of a member variable and use
QSqlDatabase::database()
static function per the docs.I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D
I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
then I can get that connection always and everywhere inside my project with simply calling
QSqlDatabase db = QSqlDatabase::database();
how can I make it work with several database connections? Let's say I have two different databases...
Would i then add them like so: ?
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
-
I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D
I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
then I can get that connection always and everywhere inside my project with simply calling
QSqlDatabase db = QSqlDatabase::database();
how can I make it work with several database connections? Let's say I have two different databases...
Would i then add them like so: ?
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
-
I got it now. I germany one would say "I was standing on the hose" I think it's best translated as: "I was drawing a blank" :D
I don't know what exactly was that hard for me to understand that but now I got it. So if i set the Database once in any source file by executing this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
then I can get that connection always and everywhere inside my project with simply calling
QSqlDatabase db = QSqlDatabase::database();
how can I make it work with several database connections? Let's say I have two different databases...
Would i then add them like so: ?
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB1"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","MyDB2"); db.setHostName(DBHOSTNAME); db.setDatabaseName(DBNAME); db.setUserName(DBUSERNAME); db.setPassword(DBPASSWORD);
-
I tried to reply here but akismet is constantly flagging my reply as spam. Therefore I posted my reply to Pastebin:
https://pastebin.com/e2SVXZVbHere I made an error. It was meant so say:
"In any source-file where a database connection is needed I can now use the class DatabaseManager to call QSqlDatabase DB_Connection = DatabaseManager::instance();
So I can later check for isOpen(). That seems a lot better to me." -
@mooswitz in addition to what my fellows wrote: the SQL related classes you use offer error methods that allow you to learn what went wrong. Using them will help you more than just printing "something wrong happened".
-
Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.
Greetz, mooswitz
-
Thank you both for your advices but I indeed used the .lastError() and related functions already. I did'nt mention them in my first posts because initially QSqlDatabase was'nt my problem. Although I made mistakes that I now have corrected. It's working perfect now.
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.But I think that my initial problem got solved by correcting the database connection. So thanks. I consider this thread as solved.
Greetz, mooswitz
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.Don't, that would be unclean. It is not a design pattern that would fit that use case.
-
@mooswitz said in How to use an Object of type QSqlTableModel in another function of the same class?:
My concern was: how can I use QSqlTableModel to trigger .submitAll(); via a button. But since I use a member variable it works.
I wonder if there is a similar approach to it like with QSqlDatabase::database(); now that I know how to use that it's super nice.Don't, that would be unclean. It is not a design pattern that would fit that use case.
@SGaist said in How to use an Object of type QSqlTableModel in another function of the same class?:
Don't, that would be unclean. It is not a design pattern that would fit that use case.
Okay. So What would be the better approach? I have taken the time now to figure out what would work better instead... so first: Why is there the "submitAll()" method, if I should'nt use it with a Button or sth. similar to trigger it? Or did you mean that I should not use the member variable for that?
What I want to do is the following: You (as a user) get presented with a TableView, showing some Data from a mariadb. This Data (in my case userdata) can be edited here. I do not want it to update directly after change. So that's Why I want a button to trigger that. Regarding the option to use QSqlTableModel for that - what is then the better option to trigger an Insert to the db with the Data loaded with the TableModel?
-
The unclean part was about trying to have a global QSqlTableModel in a similar fashion to QSqlDatabase.
For the rest, you did thing correctly.You could even simplify a bit by connecting the button directly to the submitAll function.Bad suggestion, you'd lose error handling.