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. Keep text in Text-Label widget at right proportions when adding to layout

Keep text in Text-Label widget at right proportions when adding to layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 4.4k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    PavlovsDog
    wrote on last edited by
    #1

    Hello,

    I am working on an application that should always have the same proportions in regards to the window size. All elements should grow and expand depending on the size of the window
    For the textfields I wrote a class that creates a box with text and everything works, but:

    When the widget is added to a layout, a stretch is applied which then distorts the text. The proportions are not correct anymore.

    Of course I could set the stretches in a way that would make the text appear at right proportions but thats a lot of fumbling around to get it right. and as soon as a stretch somwhere changes all other stretches will be wrong aswell.

    This is the .cpp file where the widget is created and added to the layouts:

    #include "mainwindow.h"
    
    #include <QPalette>
    
    MainWindow::MainWindow(QWidget *parent)
    	: QWidget(parent)
    {
    	QPalette pal;
    	pal.setColor(QPalette::WindowText, Qt::white);
    	pal.setColor(QPalette::Window, Qt::black);
    
    	QFont font;
    	font.setFamily("FreeSans");
    	font.setBold(true);
    	font.setPointSize(14);
    
    	m_text = new textPaint(	0,
    				QSize(70, 30),
    				"Lorem",
    				Qt::white,
    				Qt::darkBlue,
    				font);
    
    	m_H_layout_Paint = new QHBoxLayout(0);
    	m_H_layout_Paint->setSpacing(0);
    	m_H_layout_Paint->setMargin(0);
    	m_H_layout_Paint->insertStretch(0, 1);
    	m_H_layout_Paint->insertWidget(1, m_text, 1);
    	m_H_layout_Paint->insertStretch(2, 1);
    	m_text->update();
    
    	m_label = new QLabel();
    	m_label->setText("Lorem");
    	m_label->setPalette(pal);
    	m_label->setAlignment(Qt::AlignCenter);
    	m_label->setAutoFillBackground(true);
    	m_label->setFont(font);
    
    	m_H_layout_Label = new QHBoxLayout(0);
    	m_H_layout_Label->setSpacing(0);
    	m_H_layout_Label->setMargin(0);
    	m_H_layout_Label->insertStretch(0, 1);
    	m_H_layout_Label->insertWidget(1, m_label, 5);
    	m_H_layout_Label->insertStretch(2, 1);
    
    	m_layout = new QVBoxLayout(0);
    	m_layout->setSpacing(0);
    	m_layout->setMargin(0);
    	m_layout->insertStretch(0, 1);
    	m_layout->insertLayout(1, m_H_layout_Paint, 2);
    	m_layout->insertStretch(2, 1);
    	m_layout->insertLayout(3, m_H_layout_Label, 4);
    	m_layout->insertStretch(4, 1);
    
    	setLayout(m_layout);
    }
    

    And this is the textPaint class:

    #include "textpaint.h"
    #include <QtGui>
    #include <iostream>
    
    textPaint::textPaint(QWidget *parent,
    		 QSize const& size,
    		 QString const& text,
    		 QColor const& colorText,
    		 QColor const& colorBackground,
    		 QFont const& font) :
    	QWidget(parent),
    	m_size(size),
    	m_text(text),
    	m_colorText(colorText),
    	m_colorBackground(colorBackground),
    	m_font(font)
    {
    	resize(m_size);
    }
    
    
    
    void textPaint::DrawText(QPainter & painter)
    {
    	painter.setFont(m_font);
    
    	QFontMetrics fontMetric(m_font);
    	QSize textSize = fontMetric.size(Qt::TextExpandTabs, m_text);
    
    
    	painter.drawText(QRect(QPoint(-textSize.width()/2, -textSize.height()/2),
    				 QPoint(textSize.width()/2, textSize.height()/2)),
    				 /*Qt::AlignJustify | Qt::AlignCenter,*/			
    				 m_text);
    
    }
    
    
    
    void textPaint::paintEvent(QPaintEvent*)
    {
    	QPalette pal;   // set background
    	pal.setColor(QPalette::Background, m_colorBackground);
    	this->setAutoFillBackground(true);
    	this->setPalette(pal);
    
    	QPainter painter(this);
    	painter.setRenderHint(QPainter::Antialiasing);
    	painter.translate(width() / 2, height() / 2);
    	painter.scale( (qreal)this->width()/ m_size.width(), (qreal)this->height() / m_size.height());
    
    	painter.setBrush(Qt::NoBrush);
    	painter.setPen(QPen(m_colorText, 0.1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
    
    	DrawText(painter);
    }
    
    

    In the QLabel class the text is always at correct proportions no matter how much the widget is stretched or resized. I want it to look like that when I start the application. But QLabel doesnt resize the text. What I am looking for is something like a QLabel that also resizes the text.

    Any ideas?

    Thanks for reading!

    raven-worxR 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You can override
      int QWidget::heightForWidth(int w) const
      http://doc.qt.io/qt-5/qwidget.html#heightForWidth

      if you want to keep proportions

      1 Reply Last reply
      2
      • P PavlovsDog

        Hello,

        I am working on an application that should always have the same proportions in regards to the window size. All elements should grow and expand depending on the size of the window
        For the textfields I wrote a class that creates a box with text and everything works, but:

        When the widget is added to a layout, a stretch is applied which then distorts the text. The proportions are not correct anymore.

        Of course I could set the stretches in a way that would make the text appear at right proportions but thats a lot of fumbling around to get it right. and as soon as a stretch somwhere changes all other stretches will be wrong aswell.

        This is the .cpp file where the widget is created and added to the layouts:

        #include "mainwindow.h"
        
        #include <QPalette>
        
        MainWindow::MainWindow(QWidget *parent)
        	: QWidget(parent)
        {
        	QPalette pal;
        	pal.setColor(QPalette::WindowText, Qt::white);
        	pal.setColor(QPalette::Window, Qt::black);
        
        	QFont font;
        	font.setFamily("FreeSans");
        	font.setBold(true);
        	font.setPointSize(14);
        
        	m_text = new textPaint(	0,
        				QSize(70, 30),
        				"Lorem",
        				Qt::white,
        				Qt::darkBlue,
        				font);
        
        	m_H_layout_Paint = new QHBoxLayout(0);
        	m_H_layout_Paint->setSpacing(0);
        	m_H_layout_Paint->setMargin(0);
        	m_H_layout_Paint->insertStretch(0, 1);
        	m_H_layout_Paint->insertWidget(1, m_text, 1);
        	m_H_layout_Paint->insertStretch(2, 1);
        	m_text->update();
        
        	m_label = new QLabel();
        	m_label->setText("Lorem");
        	m_label->setPalette(pal);
        	m_label->setAlignment(Qt::AlignCenter);
        	m_label->setAutoFillBackground(true);
        	m_label->setFont(font);
        
        	m_H_layout_Label = new QHBoxLayout(0);
        	m_H_layout_Label->setSpacing(0);
        	m_H_layout_Label->setMargin(0);
        	m_H_layout_Label->insertStretch(0, 1);
        	m_H_layout_Label->insertWidget(1, m_label, 5);
        	m_H_layout_Label->insertStretch(2, 1);
        
        	m_layout = new QVBoxLayout(0);
        	m_layout->setSpacing(0);
        	m_layout->setMargin(0);
        	m_layout->insertStretch(0, 1);
        	m_layout->insertLayout(1, m_H_layout_Paint, 2);
        	m_layout->insertStretch(2, 1);
        	m_layout->insertLayout(3, m_H_layout_Label, 4);
        	m_layout->insertStretch(4, 1);
        
        	setLayout(m_layout);
        }
        

        And this is the textPaint class:

        #include "textpaint.h"
        #include <QtGui>
        #include <iostream>
        
        textPaint::textPaint(QWidget *parent,
        		 QSize const& size,
        		 QString const& text,
        		 QColor const& colorText,
        		 QColor const& colorBackground,
        		 QFont const& font) :
        	QWidget(parent),
        	m_size(size),
        	m_text(text),
        	m_colorText(colorText),
        	m_colorBackground(colorBackground),
        	m_font(font)
        {
        	resize(m_size);
        }
        
        
        
        void textPaint::DrawText(QPainter & painter)
        {
        	painter.setFont(m_font);
        
        	QFontMetrics fontMetric(m_font);
        	QSize textSize = fontMetric.size(Qt::TextExpandTabs, m_text);
        
        
        	painter.drawText(QRect(QPoint(-textSize.width()/2, -textSize.height()/2),
        				 QPoint(textSize.width()/2, textSize.height()/2)),
        				 /*Qt::AlignJustify | Qt::AlignCenter,*/			
        				 m_text);
        
        }
        
        
        
        void textPaint::paintEvent(QPaintEvent*)
        {
        	QPalette pal;   // set background
        	pal.setColor(QPalette::Background, m_colorBackground);
        	this->setAutoFillBackground(true);
        	this->setPalette(pal);
        
        	QPainter painter(this);
        	painter.setRenderHint(QPainter::Antialiasing);
        	painter.translate(width() / 2, height() / 2);
        	painter.scale( (qreal)this->width()/ m_size.width(), (qreal)this->height() / m_size.height());
        
        	painter.setBrush(Qt::NoBrush);
        	painter.setPen(QPen(m_colorText, 0.1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
        
        	DrawText(painter);
        }
        
        

        In the QLabel class the text is always at correct proportions no matter how much the widget is stretched or resized. I want it to look like that when I start the application. But QLabel doesnt resize the text. What I am looking for is something like a QLabel that also resizes the text.

        Any ideas?

        Thanks for reading!

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @PavlovsDog
        to add to @mrjj: in order to make this work you also need to do the following to get the heightForWidth() method get called:

        QSizePolicy sp = this->sizePolicy();
            sp.setHeightForWidth( true );
        this->setSizePolicy( sp );
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • P Offline
          P Offline
          PavlovsDog
          wrote on last edited by
          #4

          Thank you for your replies!
          If I understand correct the heightForWidth() method helps to keep the ratio of width to height always the same, but that is currently not my problem. (Might help me later though)

          Right now it doesnt really matter if its distorted when resizing the window.
          My Problem is, that the text is distorted when I start the application.

          For example with QLabel you can add stretches to the layout as much as you want, when you start the application the letters in the text will always have the right proportions.
          (I dont use QLabel because it doesnt resize the text when the window-size is changed)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PavlovsDog
            wrote on last edited by
            #5

            I think the problem is that the stretch is applied after the paintevent is called.
            So I would need a way to find out how the stretch affects the size and then repaint it based on that information.
            For Example: If I would know the stretch will make the text in the widget twice as wide I could draw it before with only half the width. Then it would turn out as I want it to look.

            mrjjM 1 Reply Last reply
            0
            • P PavlovsDog

              I think the problem is that the stretch is applied after the paintevent is called.
              So I would need a way to find out how the stretch affects the size and then repaint it based on that information.
              For Example: If I would know the stretch will make the text in the widget twice as wide I could draw it before with only half the width. Then it would turn out as I want it to look.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @PavlovsDog

              Hi
              You kinda lost me.
              When a widget is resized, it will first resize then paint.
              Else it would paint in the old size.

              Can you show a picture of what is wrong ?

              None of the Widgets will alter its text to fit anything.

              The Qt system can adjust size to help on high res screens but overall scaling and nothing
              about text size / word wrap etc.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                PavlovsDog
                wrote on last edited by
                #7

                Okay, sorry. I hope with this picture it is more understandable.

                Left and right windwos are always the same, but the right window is expanded (dragged bigger by mouse).
                The blue Label is my textPaint class. The black one is a QLabel.

                1: Looks quite normal but the text in the paintedText Label (blue) is a little compressed vertically.

                2: I increased the stretch factor of the paintedText Label (blue) so now it is clearly visible that the Text gets stretched

                3: This is photoshopped and shows what I would like. The Box is stretched but the text stays like a normal text. When the application starts, the text is neither compressed, nor stretched. Just like the font was designed.
                When the window is then expanded, text and box behave as if they were a picture, getting stretched and cmpressed in proportion to the window width and height.

                0_1502973088876_Labels.png

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  Hi
                  I think its the use of painter scale that does this or drawText(QRect())
                  Not sure why text is stretched.

                  Normally you alter only point size for a font to make it bigger smaller as stretching a font never look good.

                  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