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. Figuring out the minimum size of rich text

Figuring out the minimum size of rich text

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 992 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.
  • A Offline
    A Offline
    Absurd
    wrote on last edited by Absurd
    #1

    I have a QTextEdit which I initialize as follows:

    QTextEdit* textEdit = new QTextEdit();
    textEdit->setReadOnly(true);
    textEdit->setHtml("<p style=\"color:red; font-family:'Courier New';\">Raw Header</p>");
    textEdit->setAlignment(Qt::AlignCenter);
    textEdit->append("<p style=\"font-family:'Courier New'\">00000000<br />00000000<br />00000000<br />00000000</p>");
    textEdit->setAlignment(Qt::AlignCenter);
    

    This textEdit is later added to a QGridLayout, and on that layout it looks like this:

    19393bc1-3eb7-427a-ae0f-53aa5f2ce30e-image.png

    I want to resize it to its minimum size without having scroll bars, something like:

    textEdit->setFixedSize(minimumWidthWhileTextStillVisible, minimumHeightWhileTextStillVisible);
    

    I tried to look around but I couldn't find a solution.
    Every suggested solution I could find made the assumption that the QTextEdit is already visible which is not the case here.
    I want to predetermine the width/height before the show() is called by the layout on that object.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Good question. Maybe QTextDocument::size() returns it. Did not check it though.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        I tried that, unfortunately textEdit->document()->size() returns (0,0), probably because the document is not visible yet...

        Will it help if I will subclass QTextEdit and re-implement the sizeHint()? Will the layout manager invoke the sizeHint() after the object is visible?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Absurd said in Figuring out the minimum size of rich text:

          Will it help if I will subclass QTextEdit and re-implement the sizeHint()?

          How do you know the size then?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @Absurd said in Figuring out the minimum size of rich text:

            Will it help if I will subclass QTextEdit and re-implement the sizeHint()?

            How do you know the size then?

            A Offline
            A Offline
            Absurd
            wrote on last edited by Absurd
            #5

            @Christian-Ehrlicher by returning QTextDocument::size() from sizeHint().
            If the QTextEdit is visible by the time sizeHint() is invoked by the layout manager maybe it'll return the actual document size?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Maybe first try out if the expected value of QTextDocument::size() is correct when the widget is visible. Then it's maybe worth a try.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              A 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Maybe first try out if the expected value of QTextDocument::size() is correct when the widget is visible. Then it's maybe worth a try.

                A Offline
                A Offline
                Absurd
                wrote on last edited by Absurd
                #7

                @Christian-Ehrlicher thanks.
                Yes, since posting that I tried that too, and that does not worth a try.
                The QTextDocument::size() returns QSize(72, 173), so when I try to use that as the QTextEdit size I get:

                3f683603-a9e5-4e6f-9efb-53ecec5405cb-image.png

                I was thinking iterating over the QTextBlock's in the QTextDocument and trying to figure out the expected size from QTextBlock::blockFormat() somehow, but again - if it's not visible everything returns 0 (for example QTextBlockFormat::lineHeight() returns 0 for the two blocks).

                Is there a way to somehow render it internally into an object without actually show()'ing it?

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  There is also a function QTextDocument::idealWidth() - what does it return?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Absurd
                    wrote on last edited by
                    #9

                    This one returns 8 (far from being ideal).

                    JonBJ 1 Reply Last reply
                    0
                    • A Absurd

                      This one returns 8 (far from being ideal).

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Absurd Looks like it thinks a very narrow document would be a good idea ;-)

                      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