qlineedit 自动调整到内容 [英] qlineedit auto resize to content

查看:104
本文介绍了qlineedit 自动调整到内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 lineedit 和按钮来制作一个小部件.如果单击按钮,它应该打开一个文件对话框,我可以在其中选择一个文件.然后文件名应该显示在 lineedit 中.这是我目前得到的:

I'm trying to do a small widget with a lineedit and a pushbutton. If the button is clicked, it should open a filedialog where I can select a file. The file name should then showed in the lineedit. Here is what i got so far:

#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中也显示了正确的路径.但是调整大小不起作用.谁能帮我一把?

the openfile function of the button works fine, and the right path is shown in the lineedit too. however the resize doesnt work. can anyone lend me a hand?

推荐答案

首先,您的代码存在一些格式问题,所以我编辑了它们并添加了一些我自己的.我使用 setFixedSize() 而不是 resize() 因为用户可以决定最小化窗口,如果发生这种情况,那么你为什么要费心显示完整的文件路径(我是猜测您出于某种原因想要始终显示完整路径,并且用户无法将窗口最小化到 lineedit 中的所有文本均未显示的程度.

First of all, there are some formatting issues with your code so i edited them and added some of my own. I used setFixedSize() instead of resize() because the user can decide to minimize the window and if that happens then why must you bother showing the full file path (I am guessing you want to show the full path at all times for a reason and not have the user being able to minimize the window to a point where not all the text in the lineedit shows.

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();
}

这篇关于qlineedit 自动调整到内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆