PDA

View Full Version : QTableView, Delegate and multi font



binary001
28th March 2016, 18:32
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.

11833

my code:



#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







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

}






#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




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

ChrisW67
28th March 2016, 21:54
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.

binary001
29th March 2016, 09:08
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.

ChrisW67
29th March 2016, 12:05
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.