'QtQuick 2.0 get cap height of font
In QML (QtQuick 2.0), FontMetrics provides every info about a font's size except its cap height. I really need to find out the cap height of a font for a program (it even provides xHeight but not the cap height!!). Is there a mathematical relation between the x height and cap height? If not, is there any other way of finding the cap height of a font? Please help! I actually tried searching for all possible solutions but couldn't find anything!
Solution 1:[1]
The QFontMetricsF has capHeight method.
https://doc.qt.io/qt-5/qfontmetricsf.html#capHeight
May be you can expose from C++ as said below. If that works, QFontMetricsF has much better interface which you can use.
The below code is hand written (syntax or compiler errors will be there). But gives you an idea.
//EXPOSE THE INFORMATION FROM C++
class fontDetails : public QObject
{
Q_OBJECT
Q_PROPERTY(qreal capHeight READ GetCapHeight CONSTANT)
public:
qreal GetCapHeight() const {
return QFontMetricsF(<<YOUR FONT>>).capHeight();
}
};
//SET THE CONTEXT
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQuickView view;
fontDetails fontdetails;
view.engine()->rootContext()->setContextProperty("fontdetails", &fontdetails);
view.setSource(QUrl::fromLocalFile("<<YOUR QML>>.qml"));
view.show();
return app.exec();
}
And then consume in QML
Label {
text: fontdetails.GetCapHeight()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
