zoukankan      html  css  js  c++  java
  • Qt——文件对话框

    教程:https://www.devbean.net/2012/09/qt-study-road-2-file-dialog/

    代码如下:

    //mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class QTextEdit;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void openFile();
        void saveFile();
        
    private:
        QAction *openAction;
        QAction *saveAction;
    
        QTextEdit *textEdit;
    };
    
    #endif // MAINWINDOW_H
    //mainwindow.cpp
    #include <QtGui>
    #include <QtWidgets>
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        openAction = new QAction(QIcon(":/images/file-open"), tr("&Open..."), this);
        openAction->setShortcuts(QKeySequence::Open);
        openAction->setStatusTip(tr("Open an existing file"));
        connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
    
        saveAction = new QAction(QIcon(":/images/file-save"), tr("&Save..."), this);
        saveAction->setShortcuts(QKeySequence::Save);
        saveAction->setStatusTip(tr("Save a new file"));
        connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
    
        QMenu *file = menuBar()->addMenu(tr("&File"));
        file->addAction(openAction);
        file->addAction(saveAction);
    
        QToolBar *toolBar = addToolBar(tr("&File"));
        toolBar->addAction(openAction);
        toolBar->addAction(saveAction);
    
        statusBar() ;
    
        textEdit = new QTextEdit(this);
        setCentralWidget(textEdit);
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::openFile()
    {
        QString path = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("Text Files(*.txt)"));
        if(!path.isEmpty()) {
            QFile file(path);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QMessageBox::warning(this, tr("Read File"), tr("Cannot open file:
    %1").arg(path));
                return;
            }
            QTextStream in(&file);
            textEdit->setText(in.readAll());
            file.close();
        } else {
            QMessageBox::warning(this, tr("Path"), tr("You did not select any file."));
        }
    }
    
    void MainWindow::saveFile()
    {
        QString path = QFileDialog::getSaveFileName(this, tr("Save File"), ".", tr("Text Files(*.txt)"));
        if(!path.isEmpty()) {
            QFile file(path);
            if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                QMessageBox::warning(this, tr("Write File"), tr("Cannot open file:
    %1").arg(path));
                return;
            }
            QTextStream out(&file);
            out << textEdit->toPlainText();
            file.close();
        } else {
            QMessageBox::warning(this, tr("Path"), tr("You did not select any file."));
        }
    }

    函数说明:

    QString getOpenFileName(QWidget * parent = 0,                        //父窗口
                            const QString & caption = QString(),         //对话框标题
                            const QString & dir = QString(),             //打开的默认目录
                            const QString & filter = QString(),          //过滤器
                            QString * selectedFilter = 0,                //默认选择的过滤器
                            Options options = 0)                         //对话框的一些参数设定,比如只显示文件夹等等,它的取值是enum 

    运行截图:

  • 相关阅读:
    Typekit在线字库及使用方法
    SVG基础图形和D3.js
    CSS3 媒体查询@media 查询(响应式布局)
    Bootstrap 栅格系统(布局)
    CSS——图片替换方法:Fahrner图片替换法(FIR)
    CSS sprites(css 精灵):将小图标整合到一张图片上
    [html]点击button后画面被刷新原因:未设置type="button"
    [java]No qualifying bean of type 解决方法
    [eclipse]eclipse设置条件断点Breakpoint Properties
    Intent的setFlag和addFlag有什么区别?
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/7200986.html
Copyright © 2011-2022 走看看