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. QLabel in a layout: check whether or not setText() will cause the layout to expand

QLabel in a layout: check whether or not setText() will cause the layout to expand

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I have a QLabel and a QComboBox in a horizontal layout. The problem is that when I put long text in the label, it causes the layout to expand, which leads to the whole main window expanding. I want to avoid that by compacting the text if necessary, but how can I check before hand whether or not setText call will stretch the window?

    Note: I don't care if QLabel becomes longer than it was. I only care whether or not the main window will be expanded because of that.

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

      Hi,

      You can use QFontMetrics to calculate the with of a text.

      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
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        Hi,

        Calculating width is no problem. But how can I know if the window will be expanded or not? The way I see it, I need to calculate the new width, and then ask the layout if setting this width will make it expand.

        L 1 Reply Last reply
        0
        • V Violet Giraffe

          Hi,

          Calculating width is no problem. But how can I know if the window will be expanded or not? The way I see it, I need to calculate the new width, and then ask the layout if setting this width will make it expand.

          L Offline
          L Offline
          Lorence
          wrote on last edited by
          #4

          @Violet-Giraffe why not compare the label's width and the calculated width of the text?

          V 1 Reply Last reply
          0
          • L Lorence

            @Violet-Giraffe why not compare the label's width and the calculated width of the text?

            V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            @Lorence
            I have addressed that in the last sentence of my question.

            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