Hi,

I want to get the font of a QWidget which is set by a stylesheet. The font() method always returns the global system font or the font set by setFont, but not the font which is set by setStyleSheet() and is used for painting in the widget. I need the font to do some calculations based on the fontsize.

Example code

Qt Code:
  1. FontTest::FontTest(QWidget *parent, Qt::WFlags flags)
  2. : QMainWindow(parent, flags)
  3. {
  4. ui.setupUi(this);
  5.  
  6. QTextEdit *edit = new QTextEdit(this);
  7. setCentralWidget(edit);
  8. edit->setStyleSheet("font-family: Arial;font-style: normal;font-size: 12pt;font-weight: bold;");
  9. edit->setPlainText("Hello World");
  10. qDebug() << "Font: " << edit->font().family() << edit->font().pointSize();
  11. }
To copy to clipboard, switch view to plain text mode 

Painting is done with Arial, 12pt but qDebug() prints "Font: "MS Shell Dlg 2" 8 "

This is just a small example. In real life I have no knowledge about the font of the stylesheet because it comes from a qss file supplied at runtime and is set at application level.

I use Qt 4.8.1 under Win7

Thanks for any help

Helmut