QFile file(filePath); if (not file.open(QFile::ReadOnly)) { return; } 1.直接初始化 QDataStream stream(file); 2.如果不方便直接初始化 QByteArray byteArray = file.readAll(); QBuffer buffer(byteArray); buffer.open(QFile::ReadOnly);//这里要和上面一样,要不然会无法读取 stream.setDevice(&buffer); QIODevice *device = stream.device();