Hi,
I created Qt C++ simple sqlite database program with QSqlRelationalTableModel.
Open database and view with QSqlRelationalTableModel is ok.
Now I edit data in QTableView (in my program tableView on Form) then I want to save new update data on database.
Next When I change or select style on tableView I want to change the specific font of this row's next cell (Description)
Question 1 is how can I save new update data to database?
Question 2 is how can I change each cell with different font?
Thanks.
My code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSql>
#include <QSqlRelationalTableModel>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void openDB();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSql>
#include <QSqlRelationalTableModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QSqlDatabase myDb;
void openDB();
QSqlRelationalTableModel *model;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QSqlRelationalDelegate>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
openDB();
if(!myDb.open()) { qDebug() << "database opening error song"; return; }
model->setTable("MainData");
model
->setRelation
(1,
QSqlRelation("myStyle",
"id",
"stylename"));
model->select();
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("ID"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Style"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Description"));
ui->tableView->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openDB()
{
QString strdbPath
= QDir::currentPath() + "/mydata.db3";
if(!fn.exists())
qDebug() << strdbPath << " is missing!" << endl;
else
qDebug() << "found at " << strdbPath;
myDb.setDatabaseName(strdbPath);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QSqlRelationalDelegate>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
openDB();
if(!myDb.open()) { qDebug() << "database opening error song"; return; }
model = new QSqlRelationalTableModel(this,myDb);
model->setTable("MainData");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setRelation(1, QSqlRelation("myStyle", "id", "stylename"));
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Style"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Description"));
ui->tableView->setModel(model);
ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openDB()
{
QString strdbPath= QDir::currentPath() + "/mydata.db3";
QFile fn(strdbPath);
if(!fn.exists())
qDebug() << strdbPath << " is missing!" << endl;
else
qDebug() << "found at " << strdbPath;
myDb = QSqlDatabase::addDatabase("QSQLITE","mycon");
myDb.setDatabaseName(strdbPath);
}
To copy to clipboard, switch view to plain text mode
my form UI
ui.jpgui.jpg
database
mydata.db3
====================================
1. (table) MainData
------------------------------------
id styleid Description
------------------------------------
100 1 Sample Main Style
101 2 Traditional Theme
102 3 Musical Symbol
203 4 Minority Group
204 5 Hill with Cloud
205 4 Thai
206 2 Ancient Theme
207 3 Air Symbol
208 1 Principle Theme
209 5 Middle One
=====================================
2. (table) myStyle
-------------------------------------
id styleName
-------------------------------------
1 Main Style
2 Traditional Style
3 Musical Style
4 Ethnic Group Style
5 Regional Style
mydata.db3
====================================
1. (table) MainData
------------------------------------
id styleid Description
------------------------------------
100 1 Sample Main Style
101 2 Traditional Theme
102 3 Musical Symbol
203 4 Minority Group
204 5 Hill with Cloud
205 4 Thai
206 2 Ancient Theme
207 3 Air Symbol
208 1 Principle Theme
209 5 Middle One
=====================================
2. (table) myStyle
-------------------------------------
id styleName
-------------------------------------
1 Main Style
2 Traditional Style
3 Musical Style
4 Ethnic Group Style
5 Regional Style
To copy to clipboard, switch view to plain text mode
Bookmarks