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(); }
效果: