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. Auto new line in text when overflowing

Auto new line in text when overflowing

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.8k 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.
  • S Offline
    S Offline
    Spook
    wrote on last edited by Spook
    #1

    Hi,
    I need to put text from a text file into a specific area in a QImage (see it as a simple rectangle delimited by visible lines). However if the text is too large, then it overflows and that's definitely not the result I want. (Note that I cannot edit the text itself to add line breaks)
    Is there any possibility to "generate" a new line? What I've tried so far:

    • QLabel::setWordWrap (only works if a line break character is present, which is not the case for me
    • drawText with a bounding box

    None of them seem to work for what I'm trying to do.
    Thanks!

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

      Hi,

      Can you share a sample code that reproduces this ?

      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
      • S Offline
        S Offline
        Spook
        wrote on last edited by Spook
        #3

        Hi, thanks for replying!
        Here is some of the code, this is where everything happens:

        QImage my_template("char456.png");
        QPainter my_painter(&my_template);
        my_painter.drawText(QRect(155, 134, 522, 66), Qt::TextWordWrap, "aeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); //on purpose
        

        The template in question is a simple rectangle, like this:
        Image

        kshegunovK 1 Reply Last reply
        0
        • S Spook

          Hi, thanks for replying!
          Here is some of the code, this is where everything happens:

          QImage my_template("char456.png");
          QPainter my_painter(&my_template);
          my_painter.drawText(QRect(155, 134, 522, 66), Qt::TextWordWrap, "aeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); //on purpose
          

          The template in question is a simple rectangle, like this:
          Image

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Spook
          Well, without having a breakable space (that's a white space character) the function has no way of knowing where to break the character sequence. Have you tried passing QTextOption::WrapAnywhere to the QPainter::drawText function? I believe it should be working for you, namely:

          QImage image("char.png");
          QPainter painter(&image);
          
          QString longString = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
          QRect envelope(0, 0, image.width(), image.height());
          
          painter.drawText(envelope, longString, QTextOption::WrapAnywhere);
          

          Read and abide by the Qt Code of Conduct

          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