【问题标题】:qlineedit auto resize to contentqlineedit 自动调整到内容
【发布时间】:2015-07-13 21:30:33
【问题描述】:

我正在尝试使用 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中也显示了正确的路径。但是调整大小不起作用。谁能帮帮我?

【问题讨论】:

  • 当你不调用它的 setText 方法时,如何在 lineedit 中显示正确的路径?此外,您在错误的位置调用 connecttextChanged(QString) 信号 - 您应该在构造函数中调用它。
  • 对不起,我的错,setText当然在我的原始代码中,我只是在写问题时忘记了它。现在我将 textChanged-connect 移到另一个之后的构造函数中。但它仍然不起作用......

标签: qt resize qlineedit


【解决方案1】:

首先,您的代码存在一些格式问题,因此我对其进行了编辑并添加了一些我自己的。我使用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();
}

【讨论】:

  • 谢谢你的回答,我刚试过你的方法。当路径变长时,比如从 C:\image.jpg 到 D:\image\icon\icon.bmp,它工作正常。但不是反过来。当我将名称长的路径更改为短名称时。 lineedit 变得集中,左侧和右侧有空间。
  • 只需在resize_to_content() 函数中添加Widget_openimage::adjustSize();。我已经更新了我的答案以反映这一点。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2015-02-13
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2011-03-22
    • 2013-02-09
    • 2010-10-18
    • 1970-01-01
    相关资源
    最近更新 更多