How to properly scale qt window to all screen sizes?
-
I have a Qt desktop application written mainly w/QGraphicsView where I manually draw everything via pixels w/Qpainter. I've drawn most objects in my program in terms of percentages relative to the window size so that works well thus far, what doesn't work well is font sizes. On Linux & Windows the behaviour of QFontMetrics fm, fm.height() is different, often it's downright wrong. What I want to do is draw window sizes based on text sizes, so that if in the target OS the end user has large text set on Windows for example, to say 200% (via settings - display - change size of text), the rest of the widget can size accordingly and the text will be properly positioned, I understand this will also work to size properly against different resolutions. Is this the correct approach?
So let's say I have this subclassed QGraphicsWidget, with a paint event like so -
class mywidget : public QGraphicsWidget { Q_OBJECT public: explicit mywidget(QGraphicsScene *scene, QGraphicsItem *parent = 0) { // various init... add widget to scene... etc textToDraw = "sampletext". topLabelFont = QFont("Arial", 12, QFont::Normal); setFont(topLabelFont); topBarLabel = textToDraw; QFontMetrics fm (topBarLabel); topLabelX = fm.width(topBarLabel); topLabelY = fm.xHeight(); // i have also tried fm.height(), behaviour is similar } protected: void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { QPainterPath topbarRect; topBY = (topLabelY / 2) + 2; topbarRect.setRect(0, 0, width, topBY); painter->fillRect(topbarRect, QBrush(QColor(4, 72, 158, 255))); } private: int topBY; QString textToDraw;
So topbarRect should be growing/shrinking as the font size changes - but it doesn't. My end goal is to draw this widget based on text size so that it displays relatively equally across all desktop screen/resolution/text sizes. Why isn't it working and what's the best approach? Thanks