c++ qlineedit自动调整大小为内容

ql3eal8s  于 2023-04-01  发布在  其他
关注(0)|答案(3)|浏览(400)

我试图做一个小部件与lineedit和按钮.如果按钮被点击,它应该打开一个filedialog在那里我可以选择一个文件.文件名应该然后显示在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());
}

按钮的打开文件功能工作正常,正确的路径也显示在行编辑器中。但是调整大小不起作用。有人能帮我吗?

k3bvogb1

k3bvogb11#

首先你的代码有一些格式问题,所以我编辑了它们并添加了一些我自己的。我使用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();
}
ws51t4hk

ws51t4hk2#

我这样做使用适当的字体,只改变宽度:

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

tez616oj3#

这适用于Qt 6.3:

auto const edit = new QLineEdit;
connect(edit, &QLineEdit::textChanged, [edit](QString const& text){
    auto const text_size = edit->fontMetrics().size(0, text);
    auto const tm = edit->textMargins();
    auto const tm_size = QSize(tm.left() + tm.right(), tm.top() + tm.bottom());
    auto const cm = edit->contentsMargins();
    auto const cm_size = QSize(cm.left() + cm.right(), cm.top() + cm.bottom());
    auto const extra_size = QSize(8, 4); // hard coded stuff in Qt
    auto const contents_size = text_size + tm_size + cm_size + extra_size;

    QStyleOptionFrame op;
    op.initFrom(edit);
    auto const perfect_size =
        edit->style()->sizeFromContents(QStyle::CT_LineEdit, &op, contents_size);

    edit->setMinimumSize(perfect_size);
});

如果您还希望根据文本缩小编辑,请将setMinimumSize替换为setFixedSize

相关问题