Skip to content
Next Next commit
Make pointSize set the pixel size
Make the function `pointSize` set the font size in pixels. This works if the intended model is that users use global fractional scaling. In that case pixel sized fonts are also scaled so that they should stay legible for different screen sizes and pixel densities.
  • Loading branch information
michaelgregorius committed Mar 31, 2024
commit c96cc2a4fd767f553a7fda030da1f69496d9d93a
18 changes: 3 additions & 15 deletions include/gui_templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,16 @@
#include <QFont>
#include <QGuiApplication>

// TODO: remove once qt5 support is dropped
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
#include <QScreen>
#endif

namespace lmms::gui
{


// return DPI-independent font-size - font with returned font-size has always
// the same size in pixels
inline QFont pointSize(QFont fontPointer, float fontSize)
// Convenience method to set the font size in pixels
inline QFont pointSize(QFont fontPointer, int fontSize)
{
// to calculate DPI of a screen to make it HiDPI ready
qreal devicePixelRatio = QGuiApplication::primaryScreen()->devicePixelRatio();
qreal scaleFactor = std::max(devicePixelRatio, 1.0); // Ensure scaleFactor is at least 1.0

fontPointer.setPointSizeF(fontSize * scaleFactor);
fontPointer.setPixelSize(fontSize);
return fontPointer;
}


} // namespace lmms::gui

#endif // LMMS_GUI_TEMPLATES_H