qlineedit自动调整为内容

时间:2015-05-04 13:13:09

标签: qt resize qlineedit

我试图使用lineedit和按钮做一个小部件。如果单击该按钮,它应该打开一个我可以选择文件的文件。然后应该在lineedit中显示文件名。这是我到目前为止所得到的:

#include "widget_openimage.h"
#include <QFontMetrics>

Widget_openimage::Widget_openimage(QWidget *parent) : QWidget(parent) {

// horizontal layout
layout = new QHBoxLayout();

// linedit on the left which shows the path of the chosen file
lineedit = new QLineEdit();
lineedit->setReadOnly(true);

// pushbutton on the right to select the file
btn = new QPushButton("...");
btn->setFixedSize(20,20);
connect(btn, SIGNAL(clicked()), this, SLOT(btn_clicked()));
connect(lineedit, SIGNAL(textChanged(QString)), this, SLOT(resize_to_content()));

layout->addWidget(lineedit);
layout->addWidget(btn);
this->setLayout(layout);
}

void Widget_openimage::btn_clicked() {
QString filename = QFileDialog::getOpenFileName(this,tr("Open"), "", tr("Image Files (*.png *.jpg *.bmp));
if (filename.isEmpty())
return;
else {
      lineedit->setText(filename);
     }
}

void Widget_openimage::resize_to_content() {
QString text = lineedit->text();
QFontMetrics fm = lineedit->fontMetrics();
int width = fm.boundingRect(text).width();
lineedit->resize(width, lineedit->height());
}

按钮的openfile功能正常,右侧路径也显示在lineedit中。但调整大小不起作用。有人可以借给我一个人吗?

2 个答案:

答案 0 :(得分:8)

首先,您的代码存在一些格式问题,因此我对其进行了编辑并添加了一些我自己的代码。我使用setFixedSize()而不是resize(),因为用户可以决定最小化窗口,如果发生这种情况,那么为什么你要打扰显示完整的文件路径(我猜你想要显示完整的路径)时间有一个原因,并且没有让用户能够将窗口最小化到lineedit中没有显示所有文本的点。

Widget_openimage::Widget_openimage(QWidget *parent) : QWidget(parent) {

    // horizontal layout
    layout = new QHBoxLayout();

    // linedit on the left which shows the path of the chosen file
    lineedit = new QLineEdit;
    lineedit->setReadOnly(true);

    // pushbutton on the right to select the file
    btn = new QPushButton("...");
    btn->setFixedSize(20,20);

    connect(btn, SIGNAL(clicked()), this, SLOT(btn_clicked()));

    //do this connection so when the text in line edit is changed, its size    changes to show the full text
    connect(lineedit, SIGNAL(textChanged(QString)), this, SLOT(resize_to_content()));

    layout->addWidget(lineedit);
    layout->addWidget(btn);
    this->setLayout(layout);
}

void Widget_openimage::btn_clicked() {
    QString filename = QFileDialog::getOpenFileName(this,tr("Open"), "", tr("Image Files (*.png *.jpg *.bmp)"));

    //you have to set the file path text somewhere here
    lineedit->setText(filename);

    if (filename.isEmpty()) {
        return;
    }
}

void Widget_openimage::resize_to_content() {
    QString text = lineedit->text();

    //use QFontMetrics this way;
    QFont font("", 0);
    QFontMetrics fm(font);
    int pixelsWide = fm.width(text);
    int pixelsHigh = fm.height();

    lineedit->setFixedSize(pixelsWide, pixelsHigh);

    Widget_openimage::adjustSize();
}

答案 1 :(得分:0)

我使用正确的字体并仅更改宽度来这样做:

void Widget_openimage::resizeToContent(QLineEdit *lineEdit)
{
    QString text = lineEdit->text();
    QFontMetrics fm(lineEdit->font());
    int pixelsWide = fm.width(text);
    lineEdit->setFixedWidth(pixelsWide);
    adjustSize();
}