Hi,
I want to use multi font in QTableview so created my newclass (QSqlRelationalTableModel
with virtual QVariant data(const QModelIndex & index, int role = Qt:isplayRole) const)
Now each row can change the font I want. but when I select and change data with ItemDelegate (Dropdown List) and click next cell. new selected data is only shown id (key). (please see picture Style col's row 1 only show 4.
But I submit, refresh and data is save and table is correct.
1. How can I fix it?
2. I want to change delegate list's font according to cell's font. How can I do it?
Thanks.
Untitled.jpg
my code:
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QObject>
#include <QtSql/QSqlRelationalTableModel>
{
Q_OBJECT
public:
~myModel();
signals:
public slots:
private:
};
#endif // MYMODEL_H
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QObject>
#include <QtSql/QSqlRelationalTableModel>
class myModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
explicit myModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase());
~myModel();
virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
signals:
public slots:
private:
};
#endif // MYMODEL_H
To copy to clipboard, switch view to plain text mode
#include "mymodel.h"
#include <QFont>
#include <QDebug>
#include <QBrush>
{
// qDebug() << db.isOpen() << " is open";
}
myModel::~myModel()
{
}
{
switch( role )
{
case Qt::FontRole:
break;
case Qt::ForegroundRole:
if ((index.
row()%2
)==1) return QBrush( Qt
::red );
else return QBrush( Qt
::blue );
break;
default:
}
}
#include "mymodel.h"
#include <QFont>
#include <QDebug>
#include <QBrush>
myModel::myModel(QObject *parent, QSqlDatabase db)
:QSqlRelationalTableModel(parent,db)
{
// qDebug() << db.isOpen() << " is open";
}
myModel::~myModel()
{
}
QVariant myModel::data(const QModelIndex &index, int role) const
{
switch( role )
{
case Qt::FontRole:
if ( index.column()==1 ) return QVariant::fromValue(QFont("Aharoni", 12, QFont::Normal));
else return QVariant::fromValue(QFont("Browallia New", 12, QFont::Normal));
break;
case Qt::ForegroundRole:
if ((index.row()%2)==1) return QBrush( Qt::red );
else return QBrush( Qt::blue );
break;
default:
return QSqlTableModel::data(index,role);
}
}
To copy to clipboard, switch view to plain text mode
#include <QFile>
#include <QDebug>
#include <QSqlRelationalDelegate>
#include <QSql>
#include <QSqlQuery>
#include "mymodel.h"
namespace Ui {
class myEditor;
}
{
Q_OBJECT
public:
explicit myEditor
(QWidget *parent
= 0);
~myEditor();
private slots:
void on_pushButton_clicked();
private:
Ui::myEditor *ui;
myModel *model;
void openDB();
};
#endif // MYEDITOR_H
#include <QFile>
#include <QDebug>
#include <QSqlRelationalDelegate>
#include <QSql>
#include <QSqlQuery>
#include "mymodel.h"
namespace Ui {
class myEditor;
}
class myEditor : public QDialog
{
Q_OBJECT
public:
explicit myEditor(QWidget *parent = 0);
~myEditor();
private slots:
void on_pushButton_clicked();
private:
Ui::myEditor *ui;
QSqlDatabase myDb;
QSqlQuery *qStyle;
myModel *model;
void openDB();
};
#endif // MYEDITOR_H
To copy to clipboard, switch view to plain text mode
#include "myeditor.h"
#include "ui_myeditor.h"
myEditor
::myEditor(QWidget *parent
) : ui(new Ui::myEditor)
{
ui->setupUi(this);
openDB();
if(!myDb.open()) { qDebug() << "database opening error song"; return; }
model = new myModel(this,myDb);
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);
}
myEditor::~myEditor()
{
delete ui;
}
void myEditor::openDB()
{
QString strdbPath
= QDir::currentPath() + "/mydata.db3";
if(!fn.exists())
qDebug() << strdbPath << " is missing!" << endl;
else
qDebug() << "found at " << strdbPath;
myDb.setDatabaseName(strdbPath);
}
void myEditor::on_pushButton_clicked()
{
model->submitAll();
}
#include "myeditor.h"
#include "ui_myeditor.h"
myEditor::myEditor(QWidget *parent) :
QDialog(parent),
ui(new Ui::myEditor)
{
ui->setupUi(this);
openDB();
if(!myDb.open()) { qDebug() << "database opening error song"; return; }
model = new myModel(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));
}
myEditor::~myEditor()
{
delete ui;
}
void myEditor::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);
}
void myEditor::on_pushButton_clicked()
{
model->submitAll();
}
To copy to clipboard, switch view to plain text mode
Bookmarks