QFileDialog读文件

QWidget 继承于 QObject 和 QPaintDevice

QWigets 为 QMainWindow 的父类

Qwigets 为 QDialog 的父类

 

QFileDialog实现文件选择对话框

QString QFileDialog::getOpenFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
//指定父类、标题、默认打开后显示的目录、右下角的文件过滤器

 This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string.

QString filename;
filename = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image File (*.dat *.img *.tif *.jpg *.png *.bmp)"));
//指定父类、标题、默认打开后显示的目录、右下角的文件过滤器
QByteArray ba = filename.toLatin1();
char*  ch;
ch=ba.data();
const char* pszFile = ch;

  1. tr()函数

本程序中

QString MainWindow::tr ( const char * s, const char * c=_null, int n = -1 )

函数 tr()全名是QObject::tr(),被它处理的 字符串可以 使用工具提 取出来翻译 成其他语言, 也就是做国际化使用。只要记住,Qt 的最佳实践:如果你想让你的程序国际化的话,那么,所有用户可见的字符串都要使用 QObject::tr()!

但是,为什么我们没有写 QObject::tr(),而仅仅是 tr()呢?原来,tr()函数是定义在 Object里面的,所有使用了Q_OBJECT 宏的类都自动具有 tr()函数。

在书中看到的一句话,如果想要你做的程序国际化,就把所有用户可见的字符串写到tr()中去,可直接用工具翻译成多语言的版本

QString QObject::tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )

2.QString::toLatin1(),QString转QByteArray

QByteArray QString::toLatin1() const

Returns a Latin-1 representation of the string as a QByteArray.

The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.

3.QByteArray::data(),QByteArray转char*

char * QByteArray::data()


Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\0'-terminated, i.e. the number of bytes in the returned character string is size() + 1 for the '\0' terminator.

4.const char* sconst = s ,char* 转 const char* 

 

猜你喜欢

转载自blog.csdn.net/baidu_39630688/article/details/81583161