Qt/Databases/ListView
From ProgrammingExamples
ListView.cpp
#include <QApplication> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery> #include <QVariant> #include <QFile> #include <iostream> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QFile::remove ("test.sqlite"); QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE"); database.setDatabaseName("test.sqlite"); if(!database.open()) { std::cerr << "Could not open database" << std::endl; std::cerr << "Last error: " << database.lastError().text().toStdString() << std::endl; } QSqlQuery createQuery; bool createSuccess = createQuery.exec("create table AssociateTable " "(id integer primary key, " "AssociateName TEXT, " "SocialSecurityNumber TEXT)"); if(createSuccess) { std::cout << "Table created successfully!" << std::endl; } else { std::cerr << "Could not create table." << std::endl; std::cerr << "Last database error: " << database.lastError().text().toStdString() << std::endl; std::cerr << "Last query error: " << createQuery.lastError().text().toStdString() << std::endl; } // Populate the table with default values QSqlQuery insertQuery; insertQuery.prepare("INSERT INTO AssociateTable (id, AssociateName, SocialSecurityNumber) " "VALUES (:id, :AssociateName, :SocialSecurityNumber)"); insertQuery.bindValue(":id", 0); insertQuery.bindValue(":AssociateName", "AssociateName"); insertQuery.bindValue(":SocialSecurityNumber", "SocialSecurityNumber"); bool insertSuccess = insertQuery.exec(); if(!insertSuccess) { std::cerr << "Could not insert values!" << std::endl; std::cerr << "insertQuery last error: " << insertQuery.lastError().text().toStdString() << std::endl; return -1; } MyForm form; form.show(); return app.exec(); }
form.h
#ifndef ListView_H #define ListView_H #include "ui_ListView.h" class MyForm : public QWidget, private Ui::Form { Q_OBJECT public: MyForm(QWidget *parent = 0); public slots: protected: void showEvent ( QShowEvent * event ); }; #endif
form.cpp
#include "form.h" #include <iostream> #include <QSqlDatabase> #include <QStringList> #include <QSqlQuery> #include <QDebug> #include <QSqlError> #include <QVariant> #include <QSqlTableModel> #include <QSqlRecord> MyForm::MyForm(QWidget *parent) : QWidget(parent) { setupUi(this); } void MyForm::showEvent ( QShowEvent * event ) { QSqlQuery selectQuery; selectQuery.prepare("SELECT AssociateName FROM AssociateTable"); bool insertSuccess = selectQuery.exec(); if(!insertSuccess) { std::cerr << "Error selecting associates!" << std::endl; std::cerr << "Last error: " << selectQuery.lastError().text().toStdString() << std::endl; return; } QSqlTableModel* tableModel = new QSqlTableModel; tableModel->setTable("AssociateTable"); tableModel->select(); this->listView->setModel(tableModel); this->listView->setModelColumn(2); }
ListView.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>772</width> <height>624</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>330</x> <y>450</y> <width>91</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> <widget class="QListView" name="listView"> <property name="geometry"> <rect> <x>260</x> <y>90</y> <width>256</width> <height>192</height> </rect> </property> </widget> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ListView) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs ListView.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) INCLUDE_DIRECTORIES(${QT_QTSQL_INCLUDE_DIR}) ADD_EXECUTABLE(ListView ListView.cpp form.h form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(ListView ${QT_LIBRARIES} ${QT_QTSQL_LIBRARY_RELEASE})