When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash
-
BUDIEEEEESSSSSSSSS !!!!!!!
I got to decipher the riddle !!!!!
Thanks to partner @fcarney! Your tip for running the program in debug mode was very valuable!
I noticed that the problem was NOT in any database or query.
There is a tablewidget on the form, which lists all customers. Whenever there is an update on the form, it is reflected in the tablewidget (additions, deletions, selections ...).
Similarly, when I select a record in tablewidget, this action is reflected in the form.
But I was using the "itemSelectonChanged" slot in the table widget. THAT WAS THE MISTAKE !!!!! Whenever some code updated tablewidget, the "itemSelectonChanged" slot was activated, generating an infinite loop that broke the code.
I changed the tablewidget slot to "itemDoubleClicked" and it's all settled!
Thank you very much to all of you. Each of your opinions helped me learn a little more about QT and C ++.
-
You should still fix the database warning as it may lead to crashes.
-
Exactly.
I haven't figured out how to do it yet.
I created a "Connection" class that contains two functions: 1) Open database; 2) Close database. It is through the functions of this class that I am opening the database. This class is in an .h file, which I refer to when I need to open the DB.
Example: I compile the program and the login window opens. In your .h file, I create a new instance for the "Connection" class. In the cpp file builder is the "open database" command. When user data is validated, the login window is closed and I use the .close () command to close the database.
Shortly after this, the main window opens and the whole process above (new instance creation, database opening) is repeated. Even so, the database open error messages appear.
I thought the error would occur in the class when entering the DB connection type: BDCadastros = QSqlDatabase :: addDatabase ("QSQLITE"). However, when I remove this command from the class and place it as a global variable, the database is not opened. I do not know what to do.
Here's the code of the class I created (remembering that I put it in an .h file that I give <include> to other files):
<code>
class ConexaoCadastros
{
public:
QSqlDatabase BDCadastros; //Cria um objeto da classe QSqlDatabaseConexaoCadastros() //Coloca o tipo de banco de dados no CONSTRUTOR da classe { BDCadastros=QSqlDatabase::addDatabase("QSQLITE"); } //Função para abrir o BD bool Abrir() { BDCadastros.setDatabaseName (glbCAMINHOBDCADASTROS); //Seleciona o banco de dados if(!BDCadastros.open()) //Abre o BD, testando se ele foi aberto com sucesso { return false; } else { return true; } } //Função para fechar o BD void Fechar() { BDCadastros.close(); }
};
<code/>Do you have any suggestions?
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
-
@Christian-Ehrlicher said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
I use the .close () command to close the database.
Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?
As I said, I'm new to QT and C ++.
I tried in many ways to leave the database open and make all queries throughout the code.
But I can't.
Whenever I close one window and open another, the previous window's database closes (even if I don't use the close () command.) This way, I am forced to reopen the DB when I open the new window. It's worse: The open database message appears.
How do I leave the database open during all code execution?
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
How do I leave the database open during all code execution?
As stated in the documentation - open the database once with QSqlDatabase db = QSqlDatabase::addDatabase("..."); and get the db connection with QSqlDatabase db = QSqlDatabase::database(); ...
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
the previous window's database closes
Do you have a member variable in your window which holds the database connection?
@SGaist already pointed out that you should NOT do this.
QSqlDatabase manages all your connections for you and you can get any open connection using QSqlDatabase::database();
There is absolutely no need to have member variables holding the connection. -
Literally I Googled "qt docs":
https://doc.qt.io/
Also, when searching for something specific search:
"qt json" or "qobject" or "qstring" etc -
@fcarney said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
Literally I Googled "qt docs":
https://doc.qt.io/
Also, when searching for something specific search:
"qt json" or "qobject" or "qstring" etcThanks guys.
You guys helped me A LOT!
In a little while I'll be back to "bother" you again.
:-D
-
@Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:
In a little while I'll be back to "bother" you again.
Its all about the "bother", no trouble...