用到的头文件:
#include <QLineEdit> //单行文本控件类
#include <QPushButton> //按钮类
#include <QFileDialog> //引用文件浏览对话框类
成员变量和槽函数:
private:
QLineEdit *filename;
QPushButton *button;
private slots:
void showFiles();
构造函数中加入如下代码:
//实例单行文本控件
filename = new QLineEdit(this);
//位置
filename->setGeometry(QRect(50,50,230,25));
//实例按钮
button = new QPushButton(this);
//按钮位置
button->setGeometry(QRect(280,50,80,25));
//按钮名
button->setText("scan");
//按钮点击事件
connect(button,SIGNAL(clicked()),this,SLOT(showFiles()));
槽函数:
//按钮点击方法
void MainWindow::showFiles()
{
//定义变量 str 接收 QFileDialog 对话框获取的文件路径
QString str = QFileDialog::getOpenFileName(this,"open file","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)");
//将变量绑定 QLineEdit 控件
filename->setText(str.toUtf8());
}