Clandeik
18th May 2014, 13:20
So hello,I have an existing database in my project.I connected my database to my prog.I can't understand how to show my database in Tableview.Please look at my project, what I'm doing wrong?
10369
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/GLOSSARIY/Ð¯Ð´ÐµÑ€Ð½Ð°Ñ Ð¼ÐµÐ´Ð¸Ñ†Ð¸Ð½Ð°.sqlite");
mydb.open();
if (!mydb.open())
ui->label->setText("Failed to open your database");
else
ui->label->setText("Connected succesfully...");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QSqlQueryModel * model = new QSqlQueryModel();
QSqlQuery query;
query.exec("SELECT * FROM ГлоÑÑарий по радиобиологи ¸");
model->setQuery(query);
ui->tableView->setModel(model);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QtDebug>
#include <QFileInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
//public:
//QSqlDatabase mydb;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QSqlQueryModel *model;
QSqlDatabase mydb;
};
#endif // MAINWINDOW_H
10369
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/GLOSSARIY/Ð¯Ð´ÐµÑ€Ð½Ð°Ñ Ð¼ÐµÐ´Ð¸Ñ†Ð¸Ð½Ð°.sqlite");
mydb.open();
if (!mydb.open())
ui->label->setText("Failed to open your database");
else
ui->label->setText("Connected succesfully...");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QSqlQueryModel * model = new QSqlQueryModel();
QSqlQuery query;
query.exec("SELECT * FROM ГлоÑÑарий по радиобиологи ¸");
model->setQuery(query);
ui->tableView->setModel(model);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
#include <QtDebug>
#include <QFileInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
//public:
//QSqlDatabase mydb;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QSqlQueryModel *model;
QSqlDatabase mydb;
};
#endif // MAINWINDOW_H