2015-05-04 238 views
3

我正在嘗試使用lineedit和按鈕做一個小部件。如果按鈕被點擊,它應該打開一個filedialog,我可以選擇一個文件。文件名應該顯示在lineedit中。這是我到目前爲止: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中。但調整大小不起作用。任何人都可以借我一隻手嗎?

+0

如何在正確的道路來顯示它的lineedit,當你不把它叫做是'setText'方法嗎?還有你調用'connect'到'textChanged(QString)'信號在錯誤的地方 - 你應該在構造函數中調用它 – Amartel

+0

抱歉,我的錯,setText當然是在我的原始代碼中,我只是在寫這個問題時忘了它。我將textChanged-connect移入構造函數中之一。但它仍然無法正常工作... – yangsunny

回答

7

首先,您的代碼存在一些格式問題,所以我編輯了它們並添加了一些我自己的代碼。我用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(); 
} 
+0

thx爲您的答案,我只是試過你的方法。當路徑變長時,例如從C:\ image.jpg到D:\ image \ icon \ icon.bmp,它可以正常工作。但不是相反。當我將長名稱改爲短名稱時。 lineedit在左側和右側空間集中。 – yangsunny

+0

只需在'resize_to_content()'函數中添加'Widget_openimage :: adjustSize();'。我已經更新了我的答案以反映這一點。 –

+0

其工作。非常感謝你!! – yangsunny