Program crash when creating sqlite database
-
Hello,
I'm working on a QML application that need a datebase base. The problem is when i create my database in main.cpp the program crash.
Here is my code:int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); AppWrapper wrapper; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection db.setDatabaseName("C:/Users/a.diaw/Documents/sql/databases/db_modrian.sqllite"); if(!db.open()){ qDebug()<<"problem opening database"; } /*QSqlQuery query; query.exec("create table person " "(id integer primary key, " "firstname varchar(20), " "lastname varchar(30), " "age integer)");*/ if ( !wrapper.initialize()) return -1; return app.exec(); }
-
@Babs
Then I don't know, as your scope seems OK. No idea what else your code might do (e.g.wrapper.initialize()
). So as I said why don't you run from debugger and look at stack trace, that's what it's for instead of guessing? (Even putting inqDebug()
s to show which statement it is/is not reaching is better than now.) -
@JonB the programm indeed crash in wrapper.initialize()
Here is the code:bool AppWrapper::initialize() { Planning *plan= new Planning(this); mModel.setDataSource(plan); registerQtQmlTricksUiElements (&mEngine); registerQtQmlTricksSmartDataModel (&mEngine); mEngine.rootContext()->setContextProperty("myModel",&mModel); qmlRegisterType<PalierModel>("TableModel",0,1,"TableModel"); mEngine.rootContext()->setContextProperty("plan", plan); qmlRegisterType<DateTimeValidator>("my.components", 1, 0, "DateTimeValidator"); mEngine.load(QUrl(QStringLiteral("qrc:/main.qml"))); ``` if(mEngine.rootObjects().isEmpty()){ return false; } else{ return true; } }
It crash when we get to the line
if(mEngine.rootObjects().isEmpty()){ return false; }
Here is the error message
12:35:05: The process was ended forcefully.
12:35:05: C:\Users\a.diaw\Documents\build-PlanningModrian-Desktop_Qt_5_14_1_MinGW_64_bit-Debug\Debug\modrian.57fb0ae3\undefinedmodrian.exe crashed. -
@Babs said in Program crash when creating sqlite database:
Thank you all i resolved my problem.
would you mind sharing the solution? Thanks.
-
@Pablo-J-Rogina i resolved the problem by moving the database creation in wrapper.initialize(). I assume that it's all about events in Qt. I don't understand yet this mechanism may be any one can explain it?
-
@Babs said in Program crash when creating sqlite database:
I don't understand yet this mechanism may be any one can explain it?
Please create a new post with this subject, so not to hijack this thread. Thanks.