【问题标题】:Open files in Qt [closed]在Qt中打开文件[关闭]
【发布时间】:2016-09-01 16:13:08
【问题描述】:

我想:

  1. 当用户从QFileDialog获取文件时, 我希望文件名出现在QLineEdit 以及我想显示的任何信息,例如尺寸和类型等。

  2. 一种获取用户文件的方法(如 C++ 中的cin) 所以我可以控制这些信息或将其放入读取文件功能中

我在网络上到处搜索 但他们谈的一般,不详细。

【问题讨论】:

    标签: c++ qt file qt-creator


    【解决方案1】:
    #include <QApplication>
    
    #include <QFileDialog>
    
    class Tester : public QWidget
    {
     public:
    
       void openFile()
       {
           QFileDialog::getOpenFileName( this, tr("Open Document"), 
                                         QDir::currentPath(), 
                                         tr("Document files (*.doc *.txt);;All files (*.*)"), 
                                         0, QFileDialog::DontUseNativeDialog );
    
           QString filename = QFileDialog::getOpenFileName( 
           this, 
           tr("Open Document"), 
           QDir::currentPath(), 
           tr("Document files (*.doc *.rtf);;All files (*.*)") );
    
           if( !filename.isNull() )
           {
               qDebug( filename.toAscii() );
           }
        }
    
       void openFiles()
       {
          QStringList filenames = QFileDialog::getOpenFileNames( 
           this, 
           tr("Open Document"), 
           QDir::currentPath(), 
           tr("Documents (*.doc);;All files (*.*)") );
    
          if( !filenames.isEmpty() )
          {
             qDebug( filenames.join(",").toAscii() );
          }
        }
    
        void openDir()
        {
          QString dirname = QFileDialog::getExistingDirectory( 
             this, 
             tr("Select a Directory"), 
             QDir::currentPath() );
    
           if( !dirname.isNull() )
           {
              qDebug( dirname.toAscii() );
           }
         }
    }
    

    这里是来源: Open File Dialog

    【讨论】:

    • 非常感谢您的帖子,很抱歉给您带来困扰,谢谢谢谢
    【解决方案2】:
    1. 当用户从QFileDialog 获取文件时,我希望文件名出现在QLineEdit 中,以及我想显示的任何信息,例如大小和类型等。

    要获取选中文件的文件信息,可以使用QFileInfo

    // Absolute address of the selected file
    QString file = QFileDialog::getOpenFileName(this, "Open file");
    
    // Object for getting file info
    QFileInfo info(file);
    
    // File name
    info.fileName();
    
    // File owner
    info.owner();
    
    // File size
    info.size();
    
    // etc
    
    1. 一种获取用户文件的方法(如 C++ 中的 cin),以便我可以控制此信息或将其放入读取文件函数中

    我不明白你的意思。

    【讨论】:

    • 非常感谢,我真的很困扰你不要理会我的问题它需要掌握 Qt 但我会告诉你只是要知道,我想要一种方法或方法来跟踪任何操作在 Qt 中,所以我可以在我想要的其他地方使用此信息,例如 QFileDialog 和 QLineEdi,再次感谢您
    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 2023-04-03
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2015-01-11
    • 1970-01-01
    相关资源
    最近更新 更多