QT:将 QTextStream 存储在 QList 中

Posted

技术标签:

【中文标题】QT:将 QTextStream 存储在 QList 中【英文标题】:QT: Store QTextStream in QList 【发布时间】:2010-12-14 10:34:22 【问题描述】:

我正在尝试同时打开多个文件(文件数量随机)并将它们的文本流存储在 qlist 中以便在其他代码中简单使用:

QList<QTextStream> files;
QList<QString> fnames;
fnames.append("file1.txt");
fnames.append("file2.txt");
// ..and so on with random iterations

// collect qtextsrams into qlist
foreach (QString file, fnames) 
        QFile f(file);
        f.open(QIODevice::ReadOnly);
        QTextStream textStream(&f);
        files2.append(&textStream);


// use qtextstreams in a loop
QList<QTextStream>::iterator i;
for (i = files.begin(); i != files.end(); ++i) 
      qDebug() << i->readLine();

所以我有一个错误:

/make debug 
make -f Makefile.Debug
make[1]: Entering directory `/home/pixx/Workspace/collocs'
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -Idebug -o debug/main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:128: error: no matching function for call to ‘QList<QTextStream>::append(QTextStream*)’
/usr/include/qt4/QtCore/qlist.h:493: note: candidates are: void QList<T>::append(const T&) [with T = QTextStream]
/usr/include/qt4/QtCore/qlist.h:819: note:                 void QList<T>::append(const QList<T>&) [with T = QTextStream]
main.cpp:117: warning: unused variable ‘cc’
In file included from /usr/include/qt4/QtCore/QList:1,
                 from main.cpp:1:
/usr/include/qt4/QtCore/qtextstream.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QTextStream]’:
/usr/include/qt4/QtCore/qlist.h:695:   instantiated from ‘void QList<T>::detach_helper(int) [with T = QTextStream]’
/usr/include/qt4/QtCore/qlist.h:709:   instantiated from ‘void QList<T>::detach_helper() [with T = QTextStream]’
/usr/include/qt4/QtCore/qlist.h:126:   instantiated from ‘void QList<T>::detach() [with T = QTextStream]’
/usr/include/qt4/QtCore/qlist.h:254:   instantiated from ‘QList<T>::iterator QList<T>::begin() [with T = QTextStream]’
main.cpp:133:   instantiated from here
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private
/usr/include/qt4/QtCore/qlist.h:386: error: within this context
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private
/usr/include/qt4/QtCore/qlist.h:399: error: within this context
make[1]: Leaving directory `/home/pixx/Workspace/collocs'
make[1]: *** [debug/main.o] Error 1
make: *** [debug] Error 2

我应该解决什么问题? 我知道这是一个非常简单的问题,但我找不到谷歌的正确查询:(

【问题讨论】:

【参考方案1】:

第一个错误,即“no matching function for call to ‘QList::append(QTextStream*)’”是由于你在这行使用&运算符引起的:

files2.append(&textStream);

您的列表应该由 QTextStream 对象组成,而不是指向 QTextStream 对象的指针。

但真正的问题在于更深层次。要将对象放入列表中,对象必须具有复制构造函数。 QTextStream 没有,因为不清楚同一文本流的不同副本应如何协同工作。我建议您创建一个指向文本流的指针列表,如“QList”。当然,在这种情况下,不要忘记在不再需要它们时处理它们的销毁:

foreach (QTextStream *cur, files) delete cur;

如果您需要在代码的不同部分之间传递此列表、制作多个副本等,您可能需要智能指针 (QSharedPointer),但我几乎想不出您需要执行的任务对文本流执行此操作。

【讨论】:

也许对于我最初的问题有更简单的解决方案?我需要打开随机数量的文件,然后从其中一些文件中读取单行而不重新打开。 我需要这个来实现将有序文件合并为一个的算法: //- 打开 N 个结果文件 // - 读取每个文件的一行 //- 直到两个文件都被完全读取: // -单词相同吗? // - 是 -> 将单词和总和写入输出文件 // 读取两个文件中的下一行 // - 否 -> 仅将字母表中最低的单词写入输出文件 // 从中读取下一行单词结果文件【参考方案2】:

我找到了解决方案,感谢 Septagram 的想法! QList 文件2; // 要合并的文件列表

QList<QTextStream> files;
QList<QString> fnames;
fnames.append("file1.txt");
fnames.append("file2.txt");
// ..and so on with random iterations
QList<QFile *> files2; // file list to merge
QList<QTextStream *> files3;
foreach (QString file, files) 
    files2.append(new QFile(file)); // create file obj
    files2.last()->open(QIODevice::ReadOnly); // open file
    files3.append(new QTextStream(files2.last())); // create textstream


QList< QTextStream * >::iterator i3;
for (i3 = files3.begin(); i3 != files3.end(); ++i3) 
      qDebug() << (*i3)->readLine();

【讨论】:

以上是关于QT:将 QTextStream 存储在 QList 中的主要内容,如果未能解决你的问题,请参考以下文章

qt QTextStream 写入文件 中文乱码

qt QTextStream 写入文件 中文乱码

Qt笔记——QFile,QDataStream,QTextStream

为跨平台文本编辑器应用程序更改 QTextStream 的 QT End of Line Style

如何使用 QTextStream 而不是 QDataStream 从 QTableView 进行加载保存?

使用 QTextStream 反序列化?