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

标签 c++ qt resize qlineedit

我正在尝试制作一个带有行编辑和按钮的小部件。如果单击该按钮,它应该打开一个文件对话框,我可以在其中选择一个文件。文件名应该显示在行编辑中。这是我到目前为止得到的:

#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中。但是调整大小不起作用。谁能帮我一下吗?

最佳答案

首先,您的代码存在一些格式问题,因此我对其进行了编辑并添加了一些我自己的代码。我使用 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++ - qlineedit 自动调整内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031103/

相关文章:

c++ - 我们是否需要在 C++ 中进行矢量化,或者 for 循环是否已经足够快?

c++ - Mac OS X 上的异步 I/O

c++ - 使用QT在屏幕上绘制文本

css - 背景图像不会调整大小

c++ - InternetReadFile 问题(错误 87 - 参数不正确)

c++ - 错误在 `main' : free(): invalid pointer (though no free or delete used)

c++ - Qt 使用 exec() 循环测试 QMessageBoxes(或通用 QDialog)

qt - PySide/PyQt根据minimumSize截断QLabel中的文本

php - 如何从url调整图像大小并缩小图像大小

javascript - 谷歌图表重绘/缩放窗口调整大小