Qt文件对话框类 QFileDialog

用到的头文件:

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

 

Qt中,QFileDialog是一个文件对话框,用于打开和保存文件。它提供了一种方便的方式来与用户交互,以选择文件路径和文件名。引用和引用展示了如何使用QFileDialog来打开文件和获取用户选择的文件路径。在示例程序中,我们可以看到使用QFileDialog的静态函数getOpenFileName()来打开文件对话框,并返回用户选择的文件路径。通过设置参数来指定对话框的标题、默认打开的目录、过滤器等信息。如果用户取消选择文件,函数将返回一个空字符串。 同样,QFileDialog也提供了保存文件的功能。引用展示了如何使用QFileDialog来保存文件。可以使用setAcceptMode()设置对话框的模式为AcceptSave,然后使用selectedFiles()函数获取用户选择的保存文件路径。 总结来说,QFileDialogQt中用于打开和保存文件,通过设置不同的参数和调用不同的函数,可以实现打开和保存文件的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Qt中的QFileDialog](https://blog.csdn.net/SlowIsFastLemon/article/details/104611243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Qt开发之路10---文件对话框QFileDialog](https://blog.csdn.net/weixin_43229139/article/details/110235717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值