QDataStream on enum

  • Hi,

    May I ask for your help on my enum serialization

    I defined a enumeration:

    enum LightType {cplAC, cplDC, walFront, walReel};
    enum LightId {lightId_cplAC, lightId_cplDc, lightId_wal01, lightId_wal02, lightId_wal03, lightId_wal04};

    And override the operators of my customed class LightObj:

    QDataStream &operator<<(QDataStream &out, const LightObj &lightObj)
        qInfo() << "QDataStream &operator<<(QDataStream &out, const LightObj &lightObj)";
        qint32 lightType_int = static_cast<int>(lightObj.getLightType());
        qint32 lightId_int = static_cast<int>(lightObj.getLightId());
        out << lightType_int
            << lightId_int
            << lightObj.getSerialNo()
            << lightObj.getDateTime()
            << lightObj.getCommandCnt();
        QVector<CommandObj*> commandObjs = lightObj.getCommandObjs();
        for (CommandObj *commandObj : commandObjs)
            out << commandObj->getCommandType();
            out << commandObj;
        return out;
    QDataStream &operator>>(QDataStream &in, LightObj &lightObj)
        qint32 lightType_int, lightId_int;
        int serialNo;
        QVector<CommandObj*> commandObjs;
        QDateTime dateTime;
        int commandCnt;
        in >> lightType_int >> lightId_int;
        in >> serialNo >> dateTime >> commandCnt;
        int commandType;
        for (int i = 0; i < commandCnt; i++)
            CommandObj *commandObj;
            in >> commandType;
            qDebug() << "CommandType = " << commandType;
            if (commandType == 0)
                commandObj = new CommandObj();
            else if (commandType == 1)
                commandObj = new AoiCommandObj();
                commandObj = new ReadCommandObj();
            in >> commandObj;
        // lightObj.setLightId(static_cast<LightId>(lightId_int));
        return in;

    if I remove the line in >> lightType_int >> lightId_int;, the program can be sucessfully built and the GUI showed. However, a dummy Qt Creator window show up (I have two qt creator now ~~) and block the whole process if the line in >> lightType_int >> lightId_int; is included. Can anyone help? Thanks

  • Hi,
    maybe changing the static cast from

    qint32 lightType_int = static_cast<int>(lightObj.getLightType());


    qint32 lightType_int = static_cast<qint32>(lightObj.getLightType());


  • Moderators

    @VincentLiu said in QDataStream on enum:

    However, a dummy Qt Creator window show up

    What does this mean? What window? What are you doing when this window shows up?

  • @jsulm I clicked on the run button to run my program in a qt creator, and then a new qt creator was launched, i.e., I have two qt creators since then. However, the new one is disabled with dark face. (A common situation in Ubuntu based on some reasons such as out of resource) Have you met similar situation before ?

  • Moderators

    @VincentLiu Never seen something like this. No idea why a new QtCreator window should be opened.

Log in to reply