1 Attachment(s)
QTableView, Delegate and multi font
Hi,
I want to use multi font in QTableview so created my newclass (QSqlRelationalTableModel
with virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) 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.
Attachment 11833
my code:
Code:
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QObject>
#include <QtSql/QSqlRelationalTableModel>
{
Q_OBJECT
public:
~myModel();
signals:
public slots:
private:
};
#endif // MYMODEL_H
Code:
#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:
}
}
Code:
#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
Code:
#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();
}
Re: QTableView, Delegate and multi font
Your data() override returns the QSqlTableModel::data() value for values not overridden but the class is derived from QSqlRelationalTableModel. Return the base class data() value.
Re: QTableView, Delegate and multi font
Thanks for your suggestion.
Now I changed QSqlTableModel to QSqlRelationalTableModel. It's ok.
I want to also change Delegate List's Font according to selected cell's font style.
How can I set delegate List's font?
Thanks.
Re: QTableView, Delegate and multi font
Provide your own delegate and set the font on the QComboBox you create as the editor based on the cell it has been opened on.