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. Autoadjust text into QLabel

Autoadjust text into QLabel

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.7k Views 1 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.
  • G Offline
    G Offline
    gerteksa
    wrote on last edited by
    #1

    HI everyone.

    I'm developing an application that it will be run into an embedded system to show images and text in a 48x80 LED panel. For images I'm using QLabel, that "autoadjust" the image size to 48x48 pixels with the method setScaledContents(true):
    @
    QImage img(path_imagen);
    miImagen->setPixmap(QPixmap::fromImage(img));
    miImagen->setWindowFlags(Qt::CustomizeWindowHint);
    miImagen->setGeometry(0,0,48,48);
    miImagen->setScaledContents(true);
    miImagen->setAlignment(Qt::AlignBottom);
    miImagen->setAlignment(Qt::AlignLeft);
    @
    To show the text, I'm using QLabel too. This is the code:
    @
    miTexto->setText(cadena_imagen);
    miTexto->setAlignment(Qt::AlignCenter);
    miTexto->setFont(QFont("Arial",10));
    miTexto->setGeometry(0,50,47,30);
    miTexto->setScaledContents(true);
    @
    However, the text is not autoadjust into the QLabel size, and if my text is a bit longer, is cut, for example if I put: THIS IS AN EXAMPLE in the code, it appears THIS IS A, and lost part of the text. Why it happens? With the image setScaledContents method the image works well, but why not with the text? Which solution do you propose?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      As stated in the documentation, the scaling works for QPixmaps. What you could do is write on your pixmap and then set it on your label or set a smaller font

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Actually, it doesn't really say that. Here is what it says:

        [quote]This property holds whether the label will scale its contents to fill all available space.

        When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space.

        This property's default is false.[/quote]
        It explains what happens if the label shows a pixmap, but it doesn't say that it doesn't work with text. At least, that's how I read it. I know it doesn't, but the documentation is not very clear on that, and the API method name suggests that it should work on text as well. Otherwise, the method name should have been setScaledImage or something like that.

        To me, this smells like a feature that was never quite completed and was conceived to work also for text, but only got implemented for images.

        As to a solution: I can only think of using QFontMetrics (single line) or QTextDocument (multiple lines) to get size information on your text, and using that information to adjust the font size. You may have to iterate to get to a good result.

        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