zoukankan      html  css  js  c++  java
  • QDialog, QFileDialog 和 QDesktopServices 的使用方法

    Qt中的QDialog类是用来生成对话框的类,QFileDialog 类是QDialog的衍生类,主要用来生成打开文件,或是打开文件目录的对话框,或者是保存文件的对话框,下面我们一一来看代码:

    1. Load File Dialog

    /**
     * Button event for Load File button
     */
    void QtConfigFile::on_pbLoadFile_clicked() {
        QString fileName = QFileDialog::getOpenFileName(this,
            tr("Open Configuration File"), "",
            tr("Configuration File (*.xml);;All Files (*)"));
    }

    2. Save File Dialog

    /**
     * Button event for Save File button
     */
    void QtConfigFile::on_pbSaveFile_clicked() {
        QString fileName = QFileDialog::getSaveFileName(this,
            tr("Save Configuration File"), "",
            tr("Configuration File (*.xml);;All Files (*)"));
    }

    3. Browse Directory Dialog,其中leVideoDir是QLineEdit类的对象名

    /**
     * Button event for Video Dir Browse button
     */
    void QtConfigFile::on_pbVideoDirBrowse_clicked() {
        QString directory = QFileDialog::getExistingDirectory(this,
            tr("Video Directory"), QDir::currentPath());
    
        if (!directory.isEmpty()) {
            ui.leVideoDir->setText(directory);
        }
    }

    QDesktopServices类是针对操作系统的桌面服务应用的类,我们主要用它来打开文件夹:

    /**
     * Button event for Show Folder button
     */
    void QtConfigFile::on_pbShowFolder_clicked() {
        QDesktopServices::openUrl( QUrl::fromLocalFile( QDir::currentPath() ) );
    }
  • 相关阅读:
    【C++clock()函数学习(计算自己代码运行时间)】
    YCOJ 1041113【最近的回文数】
    计蒜客【汉诺塔II】
    YCOJ【汉诺塔】
    【常用算法总结——递归】
    YCOJ【查找】
    【常用算法总结——分治】
    Redis哨兵机制
    Redis主从复制
    SpringBoot集成Redis
  • 原文地址:https://www.cnblogs.com/grandyang/p/4304644.html
Copyright © 2011-2022 走看看