将QList <int>写入文件

时间:2015-07-20 16:24:25

标签: c++ qt file

所以这里有一小段代码用于将一些信息写入文件

void Receiver::fileWrite1(int dd, int mm, int yy, int h, int m, int s, QString commandID, QList<int> parameters)
{
    QString v = "Date - ";
    QString w = "Time - ";
    QString port = "Port = ";
    QString x = "Command = ";
    QString y = "Parameters = ";

    QFile file("E:/practica/data.txt");
    if(!file.open(QIODevice::Append)) {
        std::cerr << "Cannot open file for writing: "
                  << qPrintable(file.errorString()) << std::endl;

        return;
    }

    QTextStream out(&file);
    out << v << dd << "/" << mm << "/" << yy << "\r\n";
    out << w << h << " : " << m << " : " << s << "\r\n";
    out << port << c << "\r\n";
    out << x << commandID << "\r\n";
    out << y << << parameters << "\r\n";
    out << "\r\n";
}

问题是,&#34;参数&#34;是动态的,可以改变它的成员数量,QTextStream与QList不兼容

1 个答案:

答案 0 :(得分:1)

  1. 您可以通过迭代QList<int>

  2. 以所需格式逐个添加参数的内容
  3. 根据您希望参数内容为apppear的格式,请尝试这样的事情

    QTextStream out(&file);
    out << v << dd << "/" << mm << "/" << yy << "\r\n";
    out << w << h << " : " << m << " : " << s << "\r\n";
    out << port << c << "\r\n";
    out << x << commandID << "\r\n";
    out << y;
    //Iterate through your QList and write the contents
    for(int i=0;i<parameters.length();i++){
    out<< QString::number(parameters.value(i));
    out << "Parameter Seperator here";
    }
    
    out<< "\r\n";
    out << "\r\n";