{"id":2361,"date":"2025-10-13T16:00:17","date_gmt":"2025-10-13T07:00:17","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2361"},"modified":"2025-10-14T14:49:57","modified_gmt":"2025-10-14T05:49:57","slug":"qtexample4-databasesqlite","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/10\/13\/qtexample4-databasesqlite\/","title":{"rendered":"[QT]example4-Widget-database(SQLite)"},"content":{"rendered":"\n<p>\ud83d\udc49 \uc544\ub798\ub294 \uc704\uc82f \ubc29\uc2dd\uc758 \uacbd\uc6b0 \uc785\ub2c8\ub2e4.<br>Below is the widget method.<\/p>\n\n\n\n<p><strong>1.UI\ub514\uc790\uc778 \/ UI Desing<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"826\" height=\"530\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-0.png\" alt=\"\" class=\"wp-image-2364\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-0.png 826w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-0-300x192.png 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-0-768x493.png 768w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/figure>\n\n\n\n<p><strong>2.mainwindow.h<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef MAINWINDOW_H\n#define MAINWINDOW_H\n\n#include &lt;QMainWindow&gt;\n\n\/\/ \ucf54\ub4dc \ucd94\uac00 \/ Add code to here\n#include &lt;QStringListModel&gt;\n\n\nQT_BEGIN_NAMESPACE\nnamespace Ui {\nclass MainWindow;\n}\nQT_END_NAMESPACE\n\nclass MainWindow : public QMainWindow\n{\n    Q_OBJECT\n\npublic:\n    MainWindow(QWidget *parent = nullptr);\n    ~MainWindow();\n\nprivate:\n    Ui::MainWindow *ui;\n\n    \/\/ --&gt; \ucf54\ub4dc \ucd94\uac00 \/ Add cod to here\n\n    void loadData(); \/\/ \ub370\uc774\ud130\ub85c\ub529 \/ data loding\n    void deleteSelectedRecord(); \/\/ db\ub808\ucf54\ub4dc \uc0ad\uc81c \/ delete db record\n    void deleteAllRecord(); \/\/ \ubaa8\ub4e0 \ub370\uc774\ud130 \uc0ad\uc81c \/ Delete all data\n    QStringListModel *model; \/\/ \uba64\ubc84\ub85c \uac1d\uccb4\ub85c \uc120\uc5b8 \/ Declare as an object with a member\n\n    \/\/&lt;-- \uc5ec\uae30\uae4c\uc9c0 \/ up to here\n\n};\n#endif \/\/ MAINWINDOW_H\n<\/code><\/pre>\n\n\n\n<p><strong>3.main.cpp<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mainwindow.h\"\n\n#include &lt;QApplication&gt;\n\nint main(int argc, char *argv&#91;])\n{\n    QApplication a(argc, argv);\n    MainWindow w;\n    w.show();\n    return a.exec();\n}\n<\/code><\/pre>\n\n\n\n<p><strong>4.mainwindow.cpp<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mainwindow.h\"\n#include \".\/ui_mainwindow.h\"\n\n\/\/ \ucf54\ub4dc\ucd94\uac00 \/ Add code\n#include &lt;QSqlDatabase&gt;\n#include &lt;QSqlQuery&gt;\n#include &lt;QStringListModel&gt;\n#include&lt;QMessageBox&gt;\n#include &lt;QSqlError&gt;\n\n\nMainWindow::MainWindow(QWidget *parent)\n    : QMainWindow(parent)\n    , ui(new Ui::MainWindow)\n{\n    ui-&gt;setupUi(this);\n\n    \/\/-- \uc5ec\uae30\uc11c\ubd80\ud130 \ucf54\ub4dc \ucd94\uac00 \/ Add code from here\n\n    \/\/ DB \uc5f0\uacb0 \/ DB Connection\n    QSqlDatabase db = QSqlDatabase::addDatabase(\"QSQLITE\");\n    db.setDatabaseName(\"localdata.db\");\n    db.open();\n\n    \/\/ \ud14c\uc774\ube14 \uc0dd\uc131 \/ Create Table\n    QSqlQuery query;\n    query.exec(\"CREATE TABLE IF NOT EXISTS records (id INTEGER PRIMARY KEY, text1 TEXT, text2 TEXT)\");\n\n    \/\/ \ubc84\ud2bc \ud074\ub9ad \uc2dc \uc800\uc7a5(\uc2dc\uadf8\ub110 \uc2ac\ub86f)\n    \/\/ Save data on button click (signal slot).\n    connect(ui-&gt;pushButton, &amp;QPushButton::clicked, this, &#91;=]() {\n        QString t1 = ui-&gt;textEdit-&gt;toPlainText();\n        QString t2 = ui-&gt;plainTextEdit-&gt;toPlainText();\n\n        QSqlQuery q;\n        q.prepare(\"INSERT INTO records (text1, text2) VALUES (?, ?)\");\n        q.addBindValue(t1);\n        q.addBindValue(t2);\n        q.exec();\n\n        \/\/ QTextEdit\uc744 \ube44\uc6c1\ub2c8\ub2e4.\n        ui-&gt;textEdit-&gt;clear();\n        ui-&gt;plainTextEdit-&gt;clear();\n\n        loadData(); \/\/ \ub9ac\uc2a4\ud2b8 \uac31\uc2e0\n    });\n\n    \/\/ \ub370\uc774\ud130 \uc0ad\uc81c \ubc84\ud2bc \/ Delete data button\n    connect(ui-&gt;pushButton_2, &amp;QPushButton::clicked, this, &#91;=]() {\n        deleteSelectedRecord();\n    });\n\n    \/\/ \ubaa8\ub4e0 \ub370\uc774\ud130 \uc0ad\uc81c \ubc84\ud2bc \/ Delete all data button\n    connect(ui-&gt;pushButton_3, &amp;QPushButton::clicked, this, &#91;=]() {\n        deleteAllRecord();\n    });\n\n    \/\/connect(ui-&gt;pushButton_2, &amp;QPushButton::clicked, this, &amp;MainWindow::deleteSelectedRecord);\n\n    loadData(); \/\/ \ucd08\uae30 \ub85c\ub529 \/ data loading\n\n    \/\/-- \uc5ec\uae30\uae4c\uc9c0 \/ Up to here...\n\n}\n\n\n\/\/--&gt; \ucf54\ub4dc \ucd94\uac00 \/ \uc5ec\uae30\uc11c\ubd80\ud130 \ucf54\ub4dc \ucd94\uac00 \/ Add code from here\n\n\/\/ \ub370\uc774\ud130\ub85c\ub4dc \/ data loading\nvoid MainWindow::loadData() {\n\n    QStringList items;\n    QSqlQuery query(\"SELECT text1, text2 FROM records\");\n    while (query.next()) {\n        items &lt;&lt; query.value(0).toString() + \" | \" + query.value(1).toString();\n    }\n\n    \/\/ \uba64\ubc84\ubcc0\uc218\ub85c \uc0ac\uc6a9 \/ Used as a member variable\n    \/\/ \uba64\ubc84 \ubcc0\uc218 \ucd08\uae30\ud654 \/ Initialize member variables\n    model = new QStringListModel(items,this);\n\n    \/\/ \ub9ac\uc2a4\ud2b8\ubdf0\uc5d0 \uc5f0\uacb0 \/ Connect to list view\n    ui-&gt;listView-&gt;setModel(model);\n\n   \/\/ \uc9c0\uc5ed\ubcc0\uc218\uc0ac\uc6a9\n   \/\/ QStringListModel *model = new QStringListModel(items, this);\n   \/\/ ui-&gt;listView-&gt;setModel(model);\n}\n\n\/\/ \ub808\ucf54\ub4dc \uc0ad\uc81c \/ delete record\nvoid MainWindow::deleteSelectedRecord() {\n    QModelIndex index = ui-&gt;listView-&gt;currentIndex();\n    if (!index.isValid()) {\n        QMessageBox::warning(this, \"\uc0ad\uc81c \uc624\ub958\/Delete error\", \"\uc0ad\uc81c\ud560 \ud56d\ubaa9\uc744 \uc120\ud0dd\ud558\uc138\uc694\/Select the items you want to delete.\");\n        return;\n    }\n\n    QString selectedText = model-&gt;data(index, Qt::DisplayRole).toString();\n\n    QStringList parts = selectedText.split(\" | \");\n    if (parts.size() != 2) {\n        QMessageBox::warning(this, \"\uc0ad\uc81c \uc624\ub958\/Delete error\", \"\ud56d\ubaa9 \ud615\uc2dd\uc774 \uc62c\ubc14\ub974\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.\/The item format is incorrect.\");\n        return;\n    }\n\n    QString text1 = parts&#91;0].trimmed();\n    QString text2 = parts&#91;1].trimmed();\n\n    \/\/ deburging\n    \/\/QMessageBox::warning(this, text1, text2);\n\n    QSqlQuery query;\n\n    \/\/ :text1,:text2\ub294 \ubc14\uc778\ub529 \ubcc0\uc218\n    \/\/ :text1,:text2 are binding variables\n    query.prepare(\"DELETE FROM records WHERE text1 = :text1 AND text2 = :text2\");\n    query.bindValue(\":text1\", text1);\n    query.bindValue(\":text2\", text2);\n\n    if (!query.exec()) {\n        QMessageBox::critical(this, \"DB \uc624\ub958\/DB error\", query.lastError().text());\n        return;\n    }\n\n    \/\/ \uc0ad\uc81c \ud6c4 \ub9ac\uc2a4\ud2b8 \uac31\uc2e0\n    \/\/ Update list after deletion\n    loadData();\n\n}\n\n\/\/ \ubaa8\ub4e0 \ub370\uc774\ud130 \uc0ad\uc81c\n\/\/ Delete all data\nvoid MainWindow::deleteAllRecord() {\n    QSqlQuery query;\n    query.prepare(\"DELETE FROM records\");\n\n    if (!query.exec()) {\n        QMessageBox::critical(this, \"DB \uc624\ub958\/DB error\", query.lastError().text());\n        return;\n    }\n\n    \/\/ \uc0ad\uc81c \ud6c4 \ub9ac\uc2a4\ud2b8 \uac31\uc2e0\n    \/\/ Update list after deletion\n    loadData();\n\n}\n\n\/\/&lt;-- \uc5ec\uae30\uae4c\uc9c0 \/ Up to here\n\nMainWindow::~MainWindow()\n{\n    delete ui;\n}\n<\/code><\/pre>\n\n\n\n<p>CMakeLists.txt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cmake_minimum_required(VERSION 3.16)\n\nproject(example4 VERSION 0.1 LANGUAGES CXX)\n\nset(CMAKE_AUTOUIC ON)\nset(CMAKE_AUTOMOC ON)\nset(CMAKE_AUTORCC ON)\n\nset(CMAKE_CXX_STANDARD 17)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)\n\nfind_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)\nfind_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)\n\n<strong>#-- sqlite \ubaa8\ub4c8 \ucd94\uac00 \/  Add sqlite module\nfind_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets Sql)<\/strong>\n\n\nset(PROJECT_SOURCES\n        main.cpp\n        mainwindow.cpp\n        mainwindow.h\n        mainwindow.ui\n)\n\nif(${QT_VERSION_MAJOR} GREATER_EQUAL 6)\n    qt_add_executable(example4\n        MANUAL_FINALIZATION\n        ${PROJECT_SOURCES}\n    )\n# Define target properties for Android with Qt 6 as:\n#    set_property(TARGET example4 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR\n#                 ${CMAKE_CURRENT_SOURCE_DIR}\/android)\n# For more information, see https:\/\/doc.qt.io\/qt-6\/qt-add-executable.html#target-creation\nelse()\n    if(ANDROID)\n        add_library(example4 SHARED\n            ${PROJECT_SOURCES}\n        )\n# Define properties for Android with Qt 5 after find_package() calls as:\n#    set(ANDROID_PACKAGE_SOURCE_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\/android\")\n    else()\n        add_executable(example4\n            ${PROJECT_SOURCES}\n        )\n    endif()\nendif()\n\n<strong># -- sqlite \ub9c1\ud06c \/ sqlite linke\ntarget_link_libraries(example4 PRIVATE\n    Qt${QT_VERSION_MAJOR}::Core\n    Qt${QT_VERSION_MAJOR}::Widgets\n    Qt${QT_VERSION_MAJOR}::Sql\n)<\/strong>\n\n\ntarget_link_libraries(example4 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)\n\n# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.\n# If you are developing for iOS or macOS you should consider setting an\n# explicit, fixed bundle identifier manually though.\nif(${QT_VERSION} VERSION_LESS 6.1.0)\n  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.example4)\nendif()\nset_target_properties(example4 PROPERTIES\n    ${BUNDLE_ID_OPTION}\n    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}\n    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}\n    MACOSX_BUNDLE TRUE\n    WIN32_EXECUTABLE TRUE\n)\n\ninclude(GNUInstallDirs)\ninstall(TARGETS example4\n    BUNDLE DESTINATION .\n    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}\n    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n)\n\nif(QT_VERSION_MAJOR EQUAL 6)\n    qt_finalize_executable(example4)\nendif()\n<\/code><\/pre>\n\n\n\n<p><strong>5.\uc2e4\ud589 \/ run<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"836\" height=\"658\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-1.png\" alt=\"\" class=\"wp-image-2365\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-1.png 836w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-1-300x236.png 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example4-1-768x604.png 768w\" sizes=\"auto, (max-width: 836px) 100vw, 836px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49 \uc544\ub798\ub294 \uc704\uc82f \ubc29\uc2dd\uc758 \uacbd\uc6b0 \uc785\ub2c8\ub2e4.Below is the widget method. 1.UI\ub514\uc790\uc778 \/ UI Desing 2.mainwindow.h 3.main.cpp 4.mainwindow.cpp CMakeLists.txt 5.\uc2e4\ud589 \/ run<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,1],"tags":[],"class_list":["post-2361","post","type-post","status-publish","format-standard","hentry","category-qt","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2361"}],"version-history":[{"count":11,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2361\/revisions"}],"predecessor-version":[{"id":2410,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2361\/revisions\/2410"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}