Pixel Length of a QString



  • 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)



  • you can let Qt determine the size of a give QString

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



  • 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.



  • 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.



  • Hm, never heard anything of a viewport :/



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.