The application crashes after 3 seconds.I'm writing a task manager in qt with a database,.
-
Compile output
21:48:24: Starting C:\Users\Julia\Documents\qtprojects\taskman\build\Desktop_Qt_6_7_1_MinGW_64_bit-Debug\debug\taskman.exe... Application started Database: connection ok Error adding task: "Parameter count mismatch" Failed to add Task 1 Error adding task: "Parameter count mismatch" Failed to add Task 2 Printing tasks... Finished printing tasks. Starting event loop 21:48:28: C:\Users\Julia\Documents\qtprojects\taskman\build\Desktop_Qt_6_7_1_MinGW_64_bit-Debug\debug\taskman.exe exited with code 0
QT += core gui sql CONFIG += c++17 greaterThan(QT_MAJOR_VERSION, 5): QT += widgets TARGET = taskman TEMPLATE = app SOURCES += main.cpp \ Task.cpp \ TaskManager.cpp \ DatabaseManager.cpp HEADERS += Task.h \ TaskManager.h \ DatabaseManager.h
main.cpp
#include <QApplication> #include "TaskManager.h" #include "DatabaseManager.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); DatabaseManager dbManager("tasks.db"); TaskManager taskManager(&dbManager); taskManager.show(); return app.exec(); }
Task.h
#ifndef TASK_H #define TASK_H #include <QString> #include <QDate> class Task { public: Task(const QString &title, const QString &description, const QDate &dueDate); QString getTitle() const; QString getDescription() const; QDate getDueDate() const; private: QString title; QString description; QDate dueDate; }; #endif // TASK_H Task.cpp #include "Task.h" Task::Task(const QString &title, const QString &description, const QDate &dueDate) : title(title), description(description), dueDate(dueDate) { } QString Task::getTitle() const { return title; } QString Task::getDescription() const { return description; } QDate Task::getDueDate() const { return dueDate; }
Taskmanager.h
#ifndef TASKMANAGER_H #define TASKMANAGER_H #include <QWidget> #include <QVBoxLayout> #include <QLabel> #include <QPushButton> #include "DatabaseManager.h" class TaskManager : public QWidget { Q_OBJECT public: TaskManager(DatabaseManager *dbManager, QWidget *parent = nullptr); private: DatabaseManager *dbManager; QVBoxLayout *layout; QLabel *titleLabel; QLabel *descriptionLabel; QLabel *dueDateLabel; QPushButton *closeButton; private slots: void onCloseButtonClicked(); }; #endif // TASKMANAGER_H
Taskmanager.cpp
#include "TaskManager.h" #include <QVBoxLayout> #include <QLabel> #include <QPushButton> TaskManager::TaskManager(DatabaseManager *dbManager, QWidget *parent) : QWidget(parent), dbManager(dbManager) { layout = new QVBoxLayout(this); titleLabel = new QLabel("Title", this); descriptionLabel = new QLabel("Description", this); dueDateLabel = new QLabel("Due Date", this); closeButton = new QPushButton("Close", this); layout->addWidget(titleLabel); layout->addWidget(descriptionLabel); layout->addWidget(dueDateLabel); layout->addWidget(closeButton); connect(closeButton, &QPushButton::clicked, this, &TaskManager::onCloseButtonClicked); } void TaskManager::onCloseButtonClicked() { close(); }
Database.h
#ifndef DATABASEMANAGER_H #define DATABASEMANAGER_H #include <QString> #include <QVector> #include "Task.h" class DatabaseManager { public: DatabaseManager(const QString &path); QVector<Task> getAllTasks() const; private: QString dbPath; }; #endif // DATABASEMANAGER_H
Database.cpp
#include "DatabaseManager.h" #include <QSqlDatabase> #include <QSqlQuery> #include <QSqlError> #include <QVariant> DatabaseManager::DatabaseManager(const QString &path) : dbPath(path) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(dbPath); if (!db.open()) { qDebug() << "Error: connection with database failed"; } else { qDebug() << "Database: connection ok"; } } QVector<Task> DatabaseManager::getAllTasks() const { QVector<Task> tasks; QSqlQuery query("SELECT title, description, due_date FROM tasks"); while (query.next()) { QString title = query.value(0).toString(); QString description = query.value(1).toString(); QDate dueDate = query.value(2).toDate(); tasks.append(Task(title, description, dueDate)); } return tasks; }
-
20:49:38: Running steps for project taskman... 20:49:38: Configuration unchanged, skipping qmake step. 20:49:38: Starting: "C:\Qt\Tools\mingw1120_64\bin\mingw32-make.exe" -j4 C:/Qt/Tools/mingw1120_64/bin/mingw32-make -f Makefile.Debug mingw32-make[1]: Entering directory 'C:/Users/Julia/Documents/qtprojects/taskman/build/Desktop_Qt_6_7_1_MinGW_64_bit-Debug' g++ -c -fno-keep-inline-dllexport -g -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../../../taskman2 -I. -IC:/Qt/6.7.1/mingw_64/include -IC:/Qt/6.7.1/mingw_64/include/QtWidgets -IC:/Qt/6.7.1/mingw_64/include/QtGui -IC:/Qt/6.7.1/mingw_64/include/QtSql -IC:/Qt/6.7.1/mingw_64/include/QtCore -Idebug -I/include -IC:/Qt/6.7.1/mingw_64/mkspecs/win32-g++ -o debug\main.o ..\..\..\taskman2\main.cpp g++ -c -fno-keep-inline-dllexport -g -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../../../taskman2 -I. -IC:/Qt/6.7.1/mingw_64/include -IC:/Qt/6.7.1/mingw_64/include/QtWidgets -IC:/Qt/6.7.1/mingw_64/include/QtGui -IC:/Qt/6.7.1/mingw_64/include/QtSql -IC:/Qt/6.7.1/mingw_64/include/QtCore -Idebug -I/include -IC:/Qt/6.7.1/mingw_64/mkspecs/win32-g++ -o debug\TaskManager.o ..\..\..\taskman2\TaskManager.cpp g++ -c -fno-keep-inline-dllexport -g -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../../../taskman2 -I. -IC:/Qt/6.7.1/mingw_64/include -IC:/Qt/6.7.1/mingw_64/include/QtWidgets -IC:/Qt/6.7.1/mingw_64/include/QtGui -IC:/Qt/6.7.1/mingw_64/include/QtSql -IC:/Qt/6.7.1/mingw_64/include/QtCore -Idebug -I/include -IC:/Qt/6.7.1/mingw_64/mkspecs/win32-g++ -o debug\DatabaseManager.o ..\..\..\taskman2\DatabaseManager.cpp g++ -Wl,-subsystem,windows -mthreads -o debug\taskman.exe debug/main.o debug/Task.o debug/TaskManager.o debug/DatabaseManager.o C:\Qt\6.7.1\mingw_64\lib\libQt6Widgets.a C:\Qt\6.7.1\mingw_64\lib\libQt6Gui.a C:\Qt\6.7.1\mingw_64\lib\libQt6Sql.a C:\Qt\6.7.1\mingw_64\lib\libQt6Core.a -lmingw32 C:\Qt\6.7.1\mingw_64\lib\libQt6EntryPoint.a -lshell32 mingw32-make[1]: Leaving directory 'C:/Users/Julia/Documents/qtprojects/taskman/build/Desktop_Qt_6_7_1_MinGW_64_bit-Debug' 20:50:31: The process "C:\Qt\Tools\mingw1120_64\bin\mingw32-make.exe" exited normally. 20:50:31: Elapsed time: 00:53.
Is it proper debugging? Im newbee
21:15:19: Starting C:\Users\Julia\Documents\qtprojects\taskman\build\Desktop_Qt_6_7_1_MinGW_64_bit-Debug\debug\taskman.exe... Application started Initializing DatabaseManager with path: "tasks.db" Database: connection ok Adding task: "Task 1" Error adding task: "Parameter count mismatch" Failed to add Task 1 Adding task: "Task 2" Error adding task: "Parameter count mismatch" Failed to add Task 2 Fetching tasks from database Fetched 0 tasks Printing tasks... Finished printing tasks. Starting event loop 21:15:22: C:\Users\Julia\Documents\qtprojects\taskman\build\Desktop_Qt_6_7_1_MinGW_64_bit-Debug\debug\taskman.exe exited with code 0
-
@Torch said in The application crashes after 3 seconds.I'm writing a task manager in qt with a database,.:
Is it proper debugging?
No, this is build log.
Run your app in debugger (in QtCreator it is the green triangle with a bug on it in the left bottom corner).Did you also notice the errors in the output of your application?
Error adding task: "Parameter count mismatch" Failed to add Task 1 Error adding task: "Parameter count mismatch" Failed to add Task 2
From where in your code are they coming?
-
I forgot to post here Task.cpp
#include "Task.h" Task::Task(const QString &title, const QString &description, const QDate &dueDate) : title(title), description(description), dueDate(dueDate) { } QString Task::getTitle() const { return title; } QString Task::getDescription() const { return description; } QDate Task::getDueDate() const { return dueDate; }
Later i will try debugging
-
"Parameter count mismatch" comes from
QSqliLiteDriver::exec()
, which is probably entered when the event loop starts spinning. As the message indicates, the number of parameters to be passed to the database doesn't match up with the expected count (derived from the indexes).Just by code-reading, there is nothing obviously crash-prone in the code.
What does stand out, though:DatabaseManager::getAllTasks()
constructs anQSqlQuery
without passing a database object.- The method doesn't get called from anywhere in the posted code.
That means on the one hand, there probably is code that you don't show.
On the other hand, database errors aren't handled. That's probably the reason for the crash. -