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
Forum Updated to NodeBB v4.3 + New Features

QDataStream on enum

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.4k Views 1 Watching
  • 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 17 Apr 2017, 02:41 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

    J 1 Reply Last reply 18 Apr 2017, 05:14
    0
    • B Offline
      B Offline
      beecksche
      wrote on 17 Apr 2017, 20:39 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
        17 Apr 2017, 02:41

        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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 18 Apr 2017, 05:14 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 20 Apr 2017, 02:00
        3
        • J jsulm
          18 Apr 2017, 05:14

          @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 20 Apr 2017, 02:00 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 ?

          J 1 Reply Last reply 20 Apr 2017, 04:18
          0
          • V VincentLiu
            20 Apr 2017, 02:00

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 20 Apr 2017, 04:18 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

            3/5

            18 Apr 2017, 05:14

            • Login

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