QDataStream未按预期工作

时间:2017-11-23 12:18:44

标签: qt qt4 qt5 qdatastream

我将一些数据存储在QDataStream中并立即获取数据,但计数在重新执行时显示为零。代码看起来很好,但意外的行为

//Overloading
QDataStream& operator<< (QDataStream& writeTO, const CascadeJobInfo& data)
{
    writeTO << data.m_infoJobType << data.m_connectionName << data.m_submitJobId << data.m_submitJobStat;
    return writeTO;
}

QDataStream& operator>> (QDataStream& readIn, CascadeJobInfo& data)
{
    readIn >> data.m_infoJobType >> data.m_connectionName >> data.m_submitJobId >> data.m_submitJobStat;
    return readIn;
}

void Fun()
{
        // Code Starts here
    projectFileName = /*Path to folder*/
    QFile file(projectFileName);
    file.open(QFile::ReadWrite);
    file.close();

    QDataStream dStream(&file);
    int jobLstCount = /*Get the Count, assume 4*/
    dStream << jobLstCount;

    for(int i = 0; i < jobLstCount; i++)
    {
    JobInfo.m_infoJobType = jobFlowItem->getJobType();
    JobInfo.m_connectionName = submitItem->connectionName();
    JobInfo.m_submitJobId = submitItem->jobID();
    JobInfo.m_submitJobStat = submitItem->jobState();
        // All valid data stored here
    }

    file.close();

    QDataStream dStreamOut(&file);
    dStreamOut >> jobLstCount; /*Count returns zero here insted of 4*/
    CascadeJobInfo jobInfo;
    // Why jobLstCount is getting zero here
    for(int i = 0 ; i < jobLstCount ; i++)
    {
    dStreamOut >> jobInfo;
    } 
}

1 个答案:

答案 0 :(得分:0)

ab=add_data_to_dataframe(r'D:\Users\pankaj-m\Desktop\Stack overflow questions\xml\data.xml')
print ab

您在打开文件时正在关闭文件,因此基本上您正在使用无效的文件描述符,这将无法正常工作。完成后,将file.close()放在代码的末尾。