In this post, you will learn how to make a simple database access app using Qt creator.
Steps:
- Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl + N).
- From the left column select Application, from the middle column select Qt Widget Application.
- Click on Choose.
- Give a name to the project and set the path where you want to save this project files.
- Click Next,
- Default kits will be selected.
- Click next.
- Give the class name of your choice. I gave InfoDB.
- Select Baseclass as QMainWindow from the dropdown list.
- (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
- Click Next
- Click on Finish.
main.cpp doesn`t need to be modified.
Open InfoDB.pro and add SQL for Qt+=
QT += core gui widgets sql
Open infodb.h
and modify it as
#ifndef INFODB_H #define INFODB_H #include <QMainWindow> #include <QPushButton> #include <QLabel> #include <QLineEdit> #include <QListWidget> #include <QListWidgetItem> #include <QDebug> #include <QDialog> #include <QtSql> #include <QDebug> #include <QSqlDatabase> #include <QTableWidget> #include <QPropertyAnimation> namespace Ui { class InfoDB; } class InfoDB : public QMainWindow { Q_OBJECT public: explicit InfoDB(QWidget *parent = nullptr); ~InfoDB(); QSqlDatabase db; bool isLoggedIn; int addOrDeleteFlag; QLabel *main_header; QLabel *main_footer; QPropertyAnimation *animation; QPropertyAnimation *animation_footer; QPushButton *btn_LogInLogOut; QPushButton *btn_add; QPushButton *btn_del; QPushButton *btn_info; QPushButton *btn_ShowTable; QString correctPassword; QDialog *pass_dlg_wid; QLineEdit *input_pass; QLabel *pass_header; QLabel *wrong_pass_info; QLabel *bg_image; QWidget *wid_modify; QLabel *lbl_name; QLabel *lbl_city; QLabel *lbl_zip; QLineEdit *name_input; QLineEdit *city_input; QLineEdit *zip_input; QDialog *search_dlg_wid; QTableWidget *tb_wid; QPushButton *btn_addGo; private: void showSearchReult(QSqlQuery&,int); bool FindToDeleteCount; private slots: void handle_OnOff(); void authenticate_Input(); void handle_AddInfoUI(); void handle_AddInfoToDb(); void handle_DelInfo(); void handle_SearchInfo(); void showTabledata(); private: Ui::InfoDB *ui; }; #endif // INFODB_H
Open infodb.cpp
and copy-paste below mentioned code.
Restricted content. Please login to view the content.
Note:-Don`t forget to put an image(sqliteqt.png) to the respective folder and give the path to the QPixmap.
bg_image->setPixmap(QPixmap("/*Mention your path here*/sqliteqt.png"));
Change the database path, name, and table name in infodb.cpp file to point to correction location.
db.setDatabaseName("C:/DBs/mydb.sqlite");
In each qry.exe do not forget to put your table name which you have added into the database.
status = qry.exec("SELECT * FROM /*replace with your table name*/mytable WHERE zip=" + zip + ";");
I am using the Mozilla Firefox add-on to view the database content, you can use any other tool as well.
Link to use the add-on with Mozilla Firefox: sqlite-manager-webext
Download Project files
Restricted content. Please login to view the content.
Video
Thank you for reading.