• Qt之QFileDialog


    widget.h:

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include<QString>
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    public slots:
        void openfiledialog();
        void savefiledialog();
    private:
        QString _filename;
    };
    
    #endif // WIDGET_H

    widget.cpp:

    #include "widget.h"
    #include<QPushButton>
    #include<QHBoxLayout>
    #include<QFileDialog>
    #include<QDebug>
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        QHBoxLayout *qh=new QHBoxLayout(this);
        QPushButton *qp1=new QPushButton("file");
        QPushButton *qp2=new QPushButton("file1");
        qh->addWidget(qp1);
        qh->addWidget(qp2);
        connect(qp1,SIGNAL(clicked()),this,SLOT(openfiledialog()));
         connect(qp2,SIGNAL(clicked()),this,SLOT(savefiledialog()));
    }
    
    Widget::~Widget()
    {
    
    }
    void Widget::openfiledialog()
    {
        QString filename=QFileDialog::getOpenFileName(this,"file",_filename);
        if(!filename.isEmpty())
        {
             _filename=filename;
             qDebug()<<"文件路径:"<<filename<<endl;
        }
    }
    void Widget::savefiledialog()
    {
    
            QString filename=QFileDialog::getSaveFileName(this,"file",_filename);
            if(!filename.isEmpty())
            {
                 _filename=filename;
                 qDebug()<<"文件路径:"<<filename<<endl;
            }
    }

    main.cpp:

    #include "widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
    
        return a.exec();
    }

    效果:

  • 相关阅读:
    Mac 虚拟环境搭建使用
    Django-rest-framework --- 分页,过滤,排序组件
    Celery 分布式任务队列 -- 基础入门
    Redis
    列表
    Git
    django 框架模型之models常用的Field,及常见错误原因及处理方案。
    Django-rest-framework --- 总结
    Django-rest-framework --- drf视图的详解
    Django之POST GET与前后端交互
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13265946.html
Copyright © 2020-2023  润新知