Important: Please read the Qt Code of Conduct -

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

  • 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;
    	m_text = new textPaint(	0,
    				QSize(70, 30),
    	m_H_layout_Paint = new QHBoxLayout(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_label = new QLabel();
    	m_H_layout_Label = new QHBoxLayout(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->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);

    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) :
    void textPaint::DrawText(QPainter & painter)
    	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,*/			
    void textPaint::paintEvent(QPaintEvent*)
    	QPalette pal;   // set background
    	pal.setColor(QPalette::Background, m_colorBackground);
    	QPainter painter(this);
    	painter.translate(width() / 2, height() / 2);
    	painter.scale( (qreal)this->width()/ m_size.width(), (qreal)this->height() / m_size.height());
    	painter.setPen(QPen(m_colorText, 0.1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));

    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!

  • Lifetime Qt Champion

    You can override
    int QWidget::heightForWidth(int w) const

    if you want to keep proportions

  • Moderators

    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 );

  • 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)

  • 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.

  • Lifetime Qt Champion


    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.

  • 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.


  • Lifetime Qt Champion

    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.

Log in to reply