When is the StyleSheet applied? - Not seeing 'font' updated.
-
Using Qt Designer I have a QDialog that includes an empty (at design time) QTableWidget.
The Dialog has the following Stylesheet (as set in the designer):QTableView { font-size: 22pt; background-color: black; color: white; selection-background-color: black; selection-color: white; } QHeaderView { font: normal "Sans Serif"; font-size: 20pt; background-color: grey; color: white; } QHeaderView::down-arrow { image: url(:/UiImages/down_24.png); } QHeaderView::up-arrow { image: url(:/UiImages/up_24.png); }
Now in my code I add QTableWidgetItem to the table:
QTableWidgetItem* item = new QTableWidgetItem(name); item->setFlags(Qt::ItemIsEnabled); // Disable the selectable, editable, dragable etc (keep "Enabled) ui.tableWidget->setItem(row, 2, item); QFont font = item->font(); qDebug() << "Font Size is"<<font.pointSize()<<"incrementing to"<<font.pointSize()+4; font.setPointSize(font.pointSize()+4); item->setFont(font); // <--- THIS LINE ui.tableWidget->resizeRowToContents(row);
When I read the font size the debug statement shows it as "9", which is not what the stylesheet indicated. If I
setFont
then it will be set to 13 (9+4), and then appear small on the screen.
If I do NOT callsetFont
, then the text appears on the screen at size 22 as per the stylesheet... (but reading the fontsize from the widget still returns 9).It seems that the wordwrap is also not working correctly since it seems to be determined on the fontsize (9), not the fontsize that the text is actually being rendered as (22) so I always get ellipsed long text.
When (or what) should I read the font size in order to get the size as set per the stylesheet?
I thought that the widget font data would be updated to match the stylesheet? -
@PValentine said in When is the StyleSheet applied? - Not seeing 'font' updated.:
I thought that the widget font data would be updated to match the stylesheet?
No, this is not possible since the widget has no knowledge how it is drawn by the stylesheet.
-
@PValentine
As @Christian-Ehrlicher has said, you cannot read the effects of stylesheet in Qt code.Try not to set font in code at the same time as using stylesheets. Are you aware there is a QTableView::item CSS selector? This affects all the table's items, there isn't a way of selecting individual ones.