You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  if (!LuoLiityntaTietokantaan()) return 1;
  LuoTietokantaTaulu(0);
  TaytaDemoDataa();
  ScooterWindow window;
  window.resize(800, 600);
  window.show();
  return app.exec();
}
#ifndef SCOOTERWINDOW_H
#define SCOOTERWINDOW_H
#include <QWidget>

class QSqlTableModel;
class QTableView;

enum {
    Scooter_Id = 0,
    Scooter_Name = 1,
    Scooter_Force = 2,
    Scooter_Delta = 3,
    Scooter_Time = 4,
    Scooter_Description = 5
};

class ScooterWindow : public QWidget
{
    Q_OBJECT
public:
    ScooterWindow();
private:
    QSqlTableModel *model;
    QTableView *view;
};
#endif

#include "tietokanta.h"
#include <QtGui>
#include <QtSql>

Tietokanta::Tietokanta()
{

}

bool Tietokanta::LuoLiityntaTietokantaan()
{
    //Luodaan SqLite tietokanta
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Testidb.dat");
    if (!db.open())//avataan ja testataan aukesiko
    {//Naytetaan virhe, jos niin kavi
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        printf("DEBUG: connection NOT created\n");//printataan stdio liityntaan tulokset
        return false;
    }
    printf("DEBUG: connection created\n");
    return true;
}

void Tietokanta::LisaaRivi(
               QString      name,
               QString      force,
               QString      delta,
               QString      descr = "no comments"
               )
{
    QDateTime now = QDateTime::currentDateTime();
    QSqlQuery q;
    q.prepare("INSERT INTO tbl_data (name, force, delta,time,description) VALUES (?,?,?,?,?)");
    q.addBindValue(name);
    q.addBindValue(force);
    q.addBindValue(delta);
    q.addBindValue(now);
    q.addBindValue(descr);
    q.exec();
    q.clear();
}

void Tietokanta::TaytaDemoDataa()
{
    QSqlQuery query;
    for ( int i = 1,f=0 ; i < 100; i++, f++ )
    {
        QDateTime now = QDateTime::currentDateTime();
        QSqlQuery q;
        q.prepare("INSERT INTO tbl_data (name, force, delta,time,description) VALUES (?,?,?,?,?)");
        q.addBindValue("nimi ");
        q.addBindValue(i*2);
        q.addBindValue(f+1);
        q.addBindValue(now);
        q.addBindValue("descr ");
        q.exec();
        q.clear();
    }
}

void Tietokanta::LuoTietokantaTaulu(bool tuhotaan_jos_tosi)
{
    QSqlQuery q;
    if (tuhotaan_jos_tosi)
    {
        q.prepare("DROP TABLE tbl_data");
        q.exec();
    }
    q.clear();
    //luodaan sql-kaskyilla tietokantataulun sarakkeet
    q.prepare("CREATE TABLE tbl_data ("
                         "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                         "name VARCHAR(40) NOT NULL, "
                         "force DOUBLE NOT NULL, "
                         "delta DOUBLE NOT NULL, "
                         "time DATETIME, "
                         "description VARCHAR(80) NOT NULL)");
     q.exec();
}
#include <QtGui>
#include <QtSql>
#include "scooterwindow.h"

ScooterWindow::ScooterWindow()
{
    //luodaan malli sql.taululle
    model = new QSqlTableModel(this);
    //lisataan malliin taulu
    model->setTable("tbl_data");
    //laitetaan taulu nimen erusteella laskevaan jarjestykseen
    model->setSort(Scooter_Name, Qt::AscendingOrder);
    //laiteaan sarakkeille  otsikot
    model->setHeaderData(Scooter_Name, Qt::Horizontal, tr("Name"));
    model->setHeaderData(Scooter_Force, Qt::Horizontal, tr("Force"));
    model->setHeaderData(Scooter_Delta, Qt::Horizontal, tr("Delta"));
    model->setHeaderData(Scooter_Time, Qt::Horizontal, tr("Time"));
    model->setHeaderData(Scooter_Description, Qt::Horizontal,
                         tr("Description"));
    //otetaan malli kayttoon
    model->select();
    //tehdaan taululle nakyma
    view = new QTableView;
    //laitetaan malli nakymaan
    view->setModel(model);
    view->setSelectionMode(QAbstractItemView::SingleSelection);
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->setColumnHidden(Scooter_Id, true);
    view->resizeColumnsToContents();
    view->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QHeaderView *header = view->horizontalHeader();
    header->setStretchLastSection(true);
    //tehdaan layout
    QHBoxLayout *mainLayout = new QHBoxLayout;
    //laitetaan layoout paanakymaan
    mainLayout->addWidget(view);
    setLayout(mainLayout);
    //annetaan paaikkunalle nimi
    setWindowTitle(tr("Testi applikaatio"));
}
  • No labels
You must log in to comment.