在 Qt 中打开文件
Open files in Qt
我想:
当用户从 QFileDialog
中选取一个文件时,
我希望文件名出现在 QLineEdit
以及我想显示的任何信息,如尺寸和类型等。
一种获取用户文件的方式(类似于C++中的cin
)
所以我可以控制此信息或将其放入读取文件功能
我在网上到处搜索
但他们谈的一般,不详。
- when the user pick up a file from
QFileDialog
, I want the name of the file appeared in the QLineEdit
and any info I want to show like size & type, etc.
要获取所选文件的文件信息,可以使用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
- a way to take the user's files (Like cin in C++) so I can control this info or put it in Read file function
我听不懂你的意思。
#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
我想:
当用户从
QFileDialog
中选取一个文件时, 我希望文件名出现在QLineEdit
以及我想显示的任何信息,如尺寸和类型等。一种获取用户文件的方式(类似于C++中的
cin
) 所以我可以控制此信息或将其放入读取文件功能
我在网上到处搜索 但他们谈的一般,不详。
- when the user pick up a file from
QFileDialog
, I want the name of the file appeared in theQLineEdit
and any info I want to show like size & type, etc.
要获取所选文件的文件信息,可以使用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
- a way to take the user's files (Like cin in C++) so I can control this info or put it in Read file function
我听不懂你的意思。
#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