将QFile储存在QHash中


我想将数据随机输出到多个文件中,所以将QFile *储存在Qhash中,但是貌似运行存在问题,请大家帮忙指点一下。
下面是的一个测试程序。


 #include <QHash>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
QHash <qint32,QFile *> fileHandHash;
for(qint32 i=0; i<1000; i++){
qint32 id = i % 10;
qDebug() << i << "\t" << id;

if( ! fileHandHash.contains(id) ){
QString filename = id + ".out.txt";
QFile MYFILE(filename);
MYFILE.open(QIODevice::WriteOnly);
fileHandHash.insert(id,&MYFILE);
}

QTextStream OUT(fileHandHash[id]);
OUT << i << "\n";
}
return 1;
}

运行的时候出现下面的错误:

QObject::connect: No such signal QObject::aboutToClose() in ....\include\QtCore\5.3.2\QtCore/private/../../../../../src/corelib/io/qtextstream_p.h:75

谢谢大家!

QT C++

7UP☆波 11 years, 8 months ago

 if( ! fileHandHash.contains(id) ){
QString filename = id + ".out.txt";
QFile MYFILE(filename);
MYFILE.open(QIODevice::WriteOnly);
fileHandHash.insert(id,&MYFILE);
}

这一段里面MYFILE在大括号后就被回收了。你试试改成指针,然后用


 QFile *MYFILE =  new QFile(filename);

最后再一起Delete

老F_混乱状态 answered 11 years, 8 months ago

Your Answer