Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly scale qt window to all screen sizes?
Forum Updated to NodeBB v4.3 + New Features

How to properly scale qt window to all screen sizes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 323 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt_coding
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved