PDA

View Full Version : How to properly change font weight in model?



adutzu89
19th March 2014, 11:27
I am having issues setting font weight for a column inside QTableView's model.
I found that I can use QFont for it, but I cannot find a way to set only the font weight, I want the text bolded for some cells.
I tried it like this:


QVariant ModelLIes::data(const QModelIndex &index, int role) const{
QVariant value = QSqlQueryModel::data(index, role);
if(index.column() == 5){
if(role == Qt::FontRole){
int valStare=QSqlQueryModel::data(index, Qt::DisplayRole).toInt(); // to check which cells to modify in the column
QFontInfo info(QSqlQueryModel::data(index, Qt::DisplayRole).toString());
int marime=info.pointSize(); //tried also pixelSize();
QString fam=info.family();
if(valStare==1)
return QFont(fam, marime,QFont::Bold);
}
}
return value;
}

But the font is different from the rest of my tableview.(Is it setting the font size and family to what I am using on my OS?)

Any suggestions?

anda_skoa
19th March 2014, 13:18
You are getting font info from the database but you are not applying to to all columns.
If you always want to use the font specified in the database then you have to answer FontRole for all cells. You can still change it for the cells you want bold of course.

Btw, are you sure that the value returned by the base class is both a number and a font family? Usually font families are not just numbers.

Cheers,
_

adutzu89
19th March 2014, 15:27
Oh, I misunderstood the usage, I was trying to get the font from the tableview, but it takes the font from database.

Though I do not understand:

Btw, are you sure that the value returned by the base class is both a number and a font family? Usually font families are not just numbers.

I use:

int valStare=QSqlQueryModel::data(index, Qt::DisplayRole).toInt();
To check the value returned by the query which is 0 or 1 so it knows wether to change the font or not.


QFontInfo info(QSqlQueryModel::data(index, Qt::DisplayRole).toString()); is used to get font info like font family and size, though now I understood that it took the fony from the database and not the application itself(more exactly the QTableView).

anda_skoa
19th March 2014, 15:53
If you don't have font information in the database, it might be easier to do the visual differentiation with an item delegate for the respective column.

Cheers,
_

adutzu89
19th March 2014, 16:01
ok thank you