QTextStream文本流

用法

1
2
3
4
5
6
7
8
9
10
QTextStream()  
QTextStream(QIODevice *device)
QTextStream(FILE *fileHandle,
QIODevice::OpenMode openMode = QIODevice::ReadWrite)
QTextStream(QString *string,
QIODevice::OpenMode openMode = QIODevice::ReadWrite)
QTextStream(QByteArray *array,
QIODevice::OpenMode openMode = QIODevice::ReadWrite)
QTextStream(const QByteArray &array,
QIODevice::OpenMode openMode = QIODevice::ReadOnly)

例子:

1
2
3
4
5
6
QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
// writes "Result: 3.14 2.7 "
}

需要格外注意的是,如果使用了运算符重载<<进行数据写入,需要用.flush()方法来刷新缓存才能成功写入。

Contents
  1. 1. 用法