Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QSqlTableModelExample3

 

QtQt CreatorLubuntu

 

QSqlTableModel example 3 is a QSqlTableModel example.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQSqlTableModelExample3/CppQSqlTableModelExample3.pro

 

exists (../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists (../../DesktopApplication.pri) {
  QT += core
  QT += gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  CONFIG   += console
  CONFIG   -= app_bundle
  TEMPLATE = app
  CONFIG(release, debug|release) {
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }
  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../Libraries/boost_1_55_0
  }
}

SOURCES += main.cpp\
        qtdialog.cpp
HEADERS  += qtdialog.h
FORMS    += qtdialog.ui

 

 

 

 

 

./CppQSqlTableModelExample3/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QApplication>
#include "qtdialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./CppQSqlTableModelExample3/qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QDialog>
#include <QtSql>
#pragma GCC diagnostic pop

namespace Ui {
  class QtDialog;
}

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  QtDialog(const QtDialog&) = delete;
  QtDialog& operator=(const QtDialog&) = delete;
  ~QtDialog();
  
private slots:
  void on_edit_sql_textChanged(const QString &arg1);

private:
  Ui::QtDialog *ui;
  QSqlDatabase m_database;

  static const QSqlDatabase CreateDatabase();
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQSqlTableModelExample3/qtdialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qtdialog.h"

#include <cassert>

#include <QtSql>
#include <QSqlDatabase>
#include "ui_qtdialog.h"
#pragma GCC diagnostic pop

QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog),
  m_database(CreateDatabase())
{
  ui->setupUi(this);

  QSqlTableModel * const model = new QSqlTableModel(this, m_database);

  model->setTable("Animals");

  const bool can_load_data = model->select();
  assert(can_load_data);

  ui->database->setModel(model);
  ui->database->setSortingEnabled(true);
  ui->database->setAlternatingRowColors(true);
  ui->database->horizontalHeader()->setSortIndicatorShown(true);

  ui->edit_sql->setText("select * from animals where n_paws > 2 and can_fly = 0");
}

QtDialog::~QtDialog()
{
  delete ui;
}

const QSqlDatabase QtDialog::CreateDatabase()
{
  //Create a database
  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //Choose from QSQLITE QODBC3 QODBC
  db.setDatabaseName("zoo_database.sqlite");
  const bool ok = db.open();
  assert(ok);

  //Delete the table 'Animals' from a possible previous session
  QSqlQuery("DROP TABLE animals");
  assert(db.tables().empty() && "There must be zero tables at startup");

  //Creata a 'Animals' table
  QSqlQuery("CREATE TABLE Animals (name TEXT NOT NULL, \"n_paws\" INT, \"can_fly\")");
  assert(db.tables().size() == 1 && "Table Animals must have been created");

  //Check that table is empty
  {
    QSqlQuery query("SELECT * FROM Animals");
    //Using QSqlQuery::size is not supported by all drivers, so count the number of rows manually
    int size = 0;
    while (query.next()) { ++size; }
    assert(size == 0 && "Table Animals must be empty directly after its creation");
  }

  //Insert animals
  QSqlQuery("INSERT INTO Animals VALUES (\"Starfish\",5,0)");
  QSqlQuery("INSERT INTO Animals VALUES (\"Duck\",2,1)");
  QSqlQuery("INSERT INTO Animals VALUES (\"Horse\",4,0)");
  QSqlQuery("INSERT INTO Animals VALUES (\"Sponge\",1,0)");

  //Check that table is not empty anymore
  {
    QSqlQuery query("SELECT * FROM Animals");
    //Using QSqlQuery::size is not supported by all drivers, so count the number of rows manually
    int size = 0;
    while (query.next()) { ++size; }
    assert(size == 4 && "Table Animals must contain four animals");
  }

  return db;
}

void QtDialog::on_edit_sql_textChanged(const QString &arg1)
{
  QSqlQuery query(arg1);
  ui->selection->clear();

  const int n_cols = 3;
  ui->selection->setColumnCount(n_cols);
  ui->selection->setRowCount(0);

  if (!query.isActive()) return;
  //Using QSqlQuery::size is not supported by all drivers, so count the number of rows manually
  int row = 0;
  while (query.next())
  {
    ui->selection->setRowCount(ui->selection->rowCount() + 1);
    for (int col=0; col!=n_cols; ++col)
    {
      QTableWidgetItem * const item = new QTableWidgetItem;
      item->setText(query.value(col).toString());
      ui->selection->setItem(row,col,item);
    }
    ++row;
  }
  ui->selection->setRowCount(row);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml