zoukankan      html  css  js  c++  java
  • 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();
    }

    效果:

  • 相关阅读:
    C语言 va_start 宏
    C语言 strcat_s 函数
    C语言 strcat 函数
    C语言 memcpy_s 函数
    C语言 memcpy 函数
    C语言 strcpy_s 函数
    C语言 strcpy 函数
    C语言 sizeof 函数
    c++实现扫雷游戏 初学
    .Net vs .Net Core,我该如何选择?看这一篇文章就够了
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13265946.html
Copyright © 2011-2022 走看看