Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataStream on enum
QtWS25 Last Chance

QDataStream on enum

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VincentLiu
    wrote on last edited by
    #1

    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();
            }
            else
            {
                commandObj = new ReadCommandObj();
            }
    
    
            in >> commandObj;
    
            commandObjs.append(commandObj);
        }
    
        //lightObj.setLightType(static_cast<LightType>(lightType_int));
        // lightObj.setLightId(static_cast<LightId>(lightId_int));
        lightObj.setSerialNo(serialNo);
        lightObj.setCommandObjs(commandObjs);
        lightObj.setDateTime(dateTime);
        lightObj.setCommandCnt();
    
        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

    jsulmJ 1 Reply Last reply
    0
    • beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by
      #2

      Hi,
      maybe changing the static cast from

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

      to

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

      helps

      1 Reply Last reply
      2
      • V VincentLiu

        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();
                }
                else
                {
                    commandObj = new ReadCommandObj();
                }
        
        
                in >> commandObj;
        
                commandObjs.append(commandObj);
            }
        
            //lightObj.setLightType(static_cast<LightType>(lightType_int));
            // lightObj.setLightId(static_cast<LightId>(lightId_int));
            lightObj.setSerialNo(serialNo);
            lightObj.setCommandObjs(commandObjs);
            lightObj.setDateTime(dateTime);
            lightObj.setCommandCnt();
        
            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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply
        3
        • jsulmJ jsulm

          @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?

          V Offline
          V Offline
          VincentLiu
          wrote on last edited by
          #4

          @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 ?

          jsulmJ 1 Reply Last reply
          0
          • V VincentLiu

            @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 ?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved