[Qt-interest] How to make size of QLineEdit fit text content?

Denis Akhmetzyanov dakhmetzyanov at smartlabs.tv
Wed May 5 10:09:36 CEST 2010


Hi,
The quick and dirty decision is possible:

class MyWindow : public QWidget
{
    Q_OBJECT
public:
    MyWindow();

private slots:
    void resizeLineEditToContents();

private:
    QLineEdit *m_lineEdit;
};

MyWindow::MyWindow()
{
    resize(640, 480);

    m_lineEdit = new QLineEdit(this);
    connect(m_lineEdit, SIGNAL(textChanged(QString)), this,
SLOT(resizeLineEditToContents()));
}

void MyWindow::resizeLineEditToContents() {
    QString text = m_lineEdit->text();
    QFontMetrics fm = m_lineEdit->fontMetrics();
    int w = fm.boundingRect(text).width();
    m_lineEdit->resize(w, m_lineEdit->height());
}


2010/5/4 Lingfa Yang <lingfa at brandeis.edu>

> How to make size of QLineEdit fit text content?
> I checked
> lineEdit->adjustSize();
> it does not fit longer text in, and the widget remains in default width.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>



-- 
Best regards,
Denis Akhmetzyanov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100505/c718af4c/attachment.html 


More information about the Qt-interest-old mailing list