Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt qlineedit自动调整内容大小_Qt_Resize_Qlineedit - Fatal编程技术网

Qt qlineedit自动调整内容大小

Qt qlineedit自动调整内容大小,qt,resize,qlineedit,Qt,Resize,Qlineedit,我正在尝试做一个带有lineedit和按钮的小部件。如果单击该按钮,它将打开一个文件对话框,我可以在其中选择一个文件。然后,文件名应显示在lineedit中。以下是我到目前为止得到的信息: #include "widget_openimage.h" #include <QFontMetrics> Widget_openimage::Widget_openimage(QWidget *parent) : QWidget(parent) { // horizontal layout

我正在尝试做一个带有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中也显示了正确的路径。但是,调整大小不起作用。有人能帮我一把吗?

首先,你的代码有一些格式问题,所以我编辑了它们,并添加了一些我自己的。我使用setFixedSize而不是resize,因为用户可以决定最小化窗口,如果发生这种情况,那么为什么您必须显示完整的文件路径?我猜您希望始终显示完整的路径是有原因的,并且用户无法将窗口最小化到无法显示lineedit中所有文本的程度


当您不调用它的setText方法时,如何在lineedit中显示正确的路径?你在错误的地方调用了connect to textChangedQString信号-你应该在构造函数中调用它。对不起,我的错,setText当然在我的原始代码中,我只是在写问题时忘记了它。现在我将textChanged connect移到构造函数中,紧跟在另一个之后。但它仍然不起作用…谢谢你的回答,我只是尝试了你的方法。当路径变长时,如从C:\image.jpg到D:\image\icon\icon.bmp,它工作正常。但不是相反。当我将长名称的路径更改为短名称时。lineedit变得集中,左边和右边都有空间。只需添加Widget_openimage::adjustSize;在resize_to_content函数中。我更新了我的答案以反映这一点。
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();
}