PDA

View Full Version : How to Save (Update) and Change Font for each Cell in QTableView



binary001
25th March 2016, 07:07
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;
}

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



mainwindow.cpp


#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);
}




my form UI
1180911809


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

ChrisW67
25th March 2016, 09:28
1. Call QSqlTableModel::submitAll().
2. Either subclass QSqlRelationalTableModel and change data() to return a valid font for the Qt::FontRole, or put a QIdentityProxyModel in to achieve a similar override.

binary001
25th March 2016, 09:37
Thanks.

Update (Save) is done successfully.

Changing Font for each cell is not test.

I'll try it.