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. Pixel Length of a QString

Pixel Length of a QString

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.5k 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.
  • E Offline
    E Offline
    Estefunny
    wrote on last edited by
    #1

    I need to resize QLineEdits depending on the QString inside.
    On StackOverFlow i found this:
    @
    QLineEdit lineEdit;
    QFontMetrics fm(lineEdit->fontMetrics());
    int pixelLength = fm.width(lineEdit->text());
    lineEdit->setFixedWith(pixelLength);
    @

    But the QLineEdit will be a bit too small to show the full string.

    So what is the correct way to resize a QLineEdit depending on the given QString?

    (Working with C++ 11, Qt 5.1.1, Win7)

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nnead
      wrote on last edited by
      #2

      you can let Qt determine the size of a give QString

      @QString txt = "blabla";
      boundingRect = metrics.boundingRect(txt);
      width=boundingRect.width();
      height=boundingRect.height();@

      1 Reply Last reply
      0
      • M Offline
        M Offline
        msue
        wrote on last edited by
        #3

        Resizing a QLineEdit defines the outer bounds of the QLineEdit, which es greater than the area for the string inside. The difference depends on the style, margin and padding.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by
          #4

          Perhaps resizing the viewport of a QLineEdit would help?

          Or else the size of the viewport compared with the size of the QLineEdit would give you the data needed tot calculate the new size of the QLineEdit. Don't forget about the margins and padding ofcourse.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Estefunny
            wrote on last edited by
            #5

            Hm, never heard anything of a viewport :/

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jan-Willem
              wrote on last edited by
              #6

              See "here":http://qt-project.org/forums/viewthread/3427

              Don't know if QLineEdit has one though, but in case not there is probably some other possibility to get the inner rectangle.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jan-Willem
                wrote on last edited by
                #7

                Just checked, no viewport in QLineEdit, sorry.
                Perhaps you can use the paintRect in the QPainEvent?

                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