#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
#include <QSqlTableModel>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
initDatabase();
model.setTable("person");
model.select();
model.
setHeaderData(1, Qt
::Horizontal,
QObject::tr("First name"));
model.
setHeaderData(2, Qt
::Horizontal,
QObject::tr("Last name"));
model.
setHeaderData(0, Qt
::Horizontal,
QObject::tr("ID"));
ui->tableView->setModel(&model);
qDebug() << model.lastError();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
bool MainWindow::initDatabase()
{
db.setDatabaseName(":memory:");
if(!db.open()){
QMessageBox::critical(this, tr
("Błąd bazy danych"), tr
("Nie można połączyć się z bazą danych." "\n%1").arg("test"));
return false;
}
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
return true;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
#include <QSqlTableModel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initDatabase();
QSqlTableModel model;
model.setTable("person");
model.select();
model.setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
model.setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
model.setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
ui->tableView->setModel(&model);
qDebug() << model.lastError();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
bool MainWindow::initDatabase()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if(!db.open()){
QMessageBox::critical(this, tr("Błąd bazy danych"), tr("Nie można połączyć się z bazą danych."
"\n%1").arg("test"));
return false;
}
QSqlQuery query;
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
return true;
}
To copy to clipboard, switch view to plain text mode
Bookmarks