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. Peculiar problem with QUndoCommand in a new class files.
Forum Updated to NodeBB v4.3 + New Features

Peculiar problem with QUndoCommand in a new class files.

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 815 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.
  • T Offline
    T Offline
    TheCrowKaka
    wrote on last edited by TheCrowKaka
    #1

    Hello,

    I have a project that was originally created in Qt 5.5 or older version. I have implemented the Undo/Redo Framework and that works like a charm.

    However, i am facing a peculiar problem that i am unable to understand the reason.

    At present I have around 15 files where different classes subclassing the QUndoCommand are created and working.
    Say for example, the file abc1.h and abc1.c where 2 classes subclassing the QUndoCommand are defined.

    The problem happens when i create a new class header and c file and define a class subclassing the QUndoCommand.
    Say for example, i create a file modifyprop.h and modifyprop.c and define the subclass of QUndoCommand. Giving here the typical class.
    Header file modifypropcmd.h is as follows:

    #ifndef MODIFYPROPCMD_H
    #define MODIFYPROPCMD_H
    
    #include <QUndoCommand>
    #include "basegraphicentity.h"
    #include "mymainview.h"
    
    class modifypropcmd: public QUndoCommand
    {
    public:
        modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);
    
        void undo();
        void redo();
    
    private:
        mymainview *myGraphview;
        baseblockentity *myitem;
    
    };
    
    #endif // MODIFYPROPCMD_H
    
    

    The CPP File "modifypropcmd.cpp is as follows:

    #include "modifypropcmd.h"
    
    modifypropcmd::modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent)
        :QUndoCommand(parent),myGraphview(thisview),myitem(item)
    {
    }
    
    void modifypropcmd::undo()
    {
    
    }
    
    void modifypropcmd::redo()
    {
    }
    
    

    This structure of a class is identical for over 100 different actions with undo commands.

    This class when contained in a new class files does not work. It throws so many exceptions that i cannot understand. Here is a typical exception thrown.

    C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
    .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
         mymainview *myGraphview;
         ^~~~~~~~~~
         mymainscene
    

    However, if i copy the entire class without any modification into another header and cpp file abc1.h and abc1.cpp which already has a similar class, the project compiles like a charm... without giving any errors.

    Though I am able to continue working by creating these classes in existing files, I just dont know what could be the problem and i intend to solve it with help of experts here.

    A Qt Enthusiastic...

    JonBJ 1 Reply Last reply
    0
    • T TheCrowKaka

      Hello,

      I have a project that was originally created in Qt 5.5 or older version. I have implemented the Undo/Redo Framework and that works like a charm.

      However, i am facing a peculiar problem that i am unable to understand the reason.

      At present I have around 15 files where different classes subclassing the QUndoCommand are created and working.
      Say for example, the file abc1.h and abc1.c where 2 classes subclassing the QUndoCommand are defined.

      The problem happens when i create a new class header and c file and define a class subclassing the QUndoCommand.
      Say for example, i create a file modifyprop.h and modifyprop.c and define the subclass of QUndoCommand. Giving here the typical class.
      Header file modifypropcmd.h is as follows:

      #ifndef MODIFYPROPCMD_H
      #define MODIFYPROPCMD_H
      
      #include <QUndoCommand>
      #include "basegraphicentity.h"
      #include "mymainview.h"
      
      class modifypropcmd: public QUndoCommand
      {
      public:
          modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);
      
          void undo();
          void redo();
      
      private:
          mymainview *myGraphview;
          baseblockentity *myitem;
      
      };
      
      #endif // MODIFYPROPCMD_H
      
      

      The CPP File "modifypropcmd.cpp is as follows:

      #include "modifypropcmd.h"
      
      modifypropcmd::modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent)
          :QUndoCommand(parent),myGraphview(thisview),myitem(item)
      {
      }
      
      void modifypropcmd::undo()
      {
      
      }
      
      void modifypropcmd::redo()
      {
      }
      
      

      This structure of a class is identical for over 100 different actions with undo commands.

      This class when contained in a new class files does not work. It throws so many exceptions that i cannot understand. Here is a typical exception thrown.

      C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
      .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
           mymainview *myGraphview;
           ^~~~~~~~~~
           mymainscene
      

      However, if i copy the entire class without any modification into another header and cpp file abc1.h and abc1.cpp which already has a similar class, the project compiles like a charm... without giving any errors.

      Though I am able to continue working by creating these classes in existing files, I just dont know what could be the problem and i intend to solve it with help of experts here.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @TheCrowKaka
      What are you doing?! :confused:

      [BTW your problem won't be to do with QUndoCommand.]

      class modifyblklaserpropcmd : public QUndoCommand
      {
      public:
          modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);
      

      This declares a new class named modifyblklaserpropcmd and then declares a constructor for class modifypropcmd and no constructor for modifyblklaserpropcmd....

      And then in the .cpp you have the same: constructor for modifypropcmd, rest of methods for modifyblklaserpropcmd.

      Can you start by sorting this out correctly, before we deal with #ifndef MODIFYPROPCMD_H which presumably should read #ifndef MODIFYBLKLASERPROPCMD_H, and why you have class modifyblklaserpropcmd in files named modifyprop...{cpp,h} and then there is a modifypropcmd.h file....

      If this is all "deliberate" then I leave you to figure, because the architecture is too hard for me to get my head around.

      If it's complaining error: 'mymainview' does not name a type then it has not met a class mymainview when it hits that line. If that is supposed to come from #include "mymainview.h" then my guess is that file has some complex #ifndef ... which in your scheme causes class mymainview not to have been encountered....

      T 1 Reply Last reply
      0
      • JonBJ JonB

        @TheCrowKaka
        What are you doing?! :confused:

        [BTW your problem won't be to do with QUndoCommand.]

        class modifyblklaserpropcmd : public QUndoCommand
        {
        public:
            modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);
        

        This declares a new class named modifyblklaserpropcmd and then declares a constructor for class modifypropcmd and no constructor for modifyblklaserpropcmd....

        And then in the .cpp you have the same: constructor for modifypropcmd, rest of methods for modifyblklaserpropcmd.

        Can you start by sorting this out correctly, before we deal with #ifndef MODIFYPROPCMD_H which presumably should read #ifndef MODIFYBLKLASERPROPCMD_H, and why you have class modifyblklaserpropcmd in files named modifyprop...{cpp,h} and then there is a modifypropcmd.h file....

        If this is all "deliberate" then I leave you to figure, because the architecture is too hard for me to get my head around.

        If it's complaining error: 'mymainview' does not name a type then it has not met a class mymainview when it hits that line. If that is supposed to come from #include "mymainview.h" then my guess is that file has some complex #ifndef ... which in your scheme causes class mymainview not to have been encountered....

        T Offline
        T Offline
        TheCrowKaka
        wrote on last edited by
        #3

        @JonB SORRY for the trouble..

        Happened due to the copy pasting affair.
        I have corrected the post.

        A Qt Enthusiastic...

        JonBJ 1 Reply Last reply
        0
        • T TheCrowKaka

          @JonB SORRY for the trouble..

          Happened due to the copy pasting affair.
          I have corrected the post.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:

          @JonB SORRY for the trouble..
          Happened due to the copy pasting affair.

          Grrrrrrr :@ Do you realize how many times I have answered people's posts, which are said to be a "copy/paste" of their code, and then turn out not to be, and I have spent all the time to answer...?

          So we go back to my last paragraph:

          If it's complaining error: 'mymainview' does not name a type then it has not met a class mymainview when it hits that line. If that is supposed to come from #include "mymainview.h" then my guess is that file has some complex #ifndef ... which in your scheme causes class mymainview not to have been encountered....

          #include "mymainview.h"
          
          class modifypropcmd: public QUndoCommand
          {
          private:
              mymainview *myGraphview;
          
          C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
          .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'?
               mymainview *myGraphview;
               ^~~~~~~~~~
          

          So mymainview is not declared when it gets to modifypropcmd.h:19. So what exactly is in your "mymainview.h", if that is where it is supposed to come from ... ?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB said in Peculiar problem with QUndoCommand in a new class files.:

            mymainview

            simply forward declare the mymainview class.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @JonB said in Peculiar problem with QUndoCommand in a new class files.:

              mymainview

              simply forward declare the mymainview class.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Christian-Ehrlicher
              Since he has #include "mymainview.h" at the top of the file prior to mymainview *myGraphview; (as I pasted), don't you think that is supposed to bring in its declaration without any need/intention for forward declarations?? Which is why we need to see that file before deciding what the best "fix" is....

              Christian EhrlicherC 1 Reply Last reply
              0
              • JonBJ JonB

                @Christian-Ehrlicher
                Since he has #include "mymainview.h" at the top of the file prior to mymainview *myGraphview; (as I pasted), don't you think that is supposed to bring in its declaration without any need/intention for forward declarations?? Which is why we need to see that file before deciding what the best "fix" is....

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB said in Peculiar problem with QUndoCommand in a new class files.:

                don't you think that is supposed to bring in its declaration without any need/intention for forward declarations??

                No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @JonB said in Peculiar problem with QUndoCommand in a new class files.:

                  don't you think that is supposed to bring in its declaration without any need/intention for forward declarations??

                  No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:

                  No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                  If that is the case your answer makes sense. But how in the world do you know that, since he has never shown mymainview.h?? Unless I am going mad and haven't spotted something.... [Or you are clairvoyant...]

                  Christian EhrlicherC 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:

                    No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                    If that is the case your answer makes sense. But how in the world do you know that, since he has never shown mymainview.h?? Unless I am going mad and haven't spotted something.... [Or you are clairvoyant...]

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JonB said in Peculiar problem with QUndoCommand in a new class files.:

                    Unless I am going mad and haven't spotted something....

                    I don't know it but it's a common error when you see such an error (you include a header, use the class but the compiler don't know about it).
                    There is no need to include a header file in another one when just the pointer to the class is used - use forward decls for such kind - it avoids circular dependency errors and speed up compilation.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    JonBJ 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      @JonB said in Peculiar problem with QUndoCommand in a new class files.:

                      Unless I am going mad and haven't spotted something....

                      I don't know it but it's a common error when you see such an error (you include a header, use the class but the compiler don't know about it).
                      There is no need to include a header file in another one when just the pointer to the class is used - use forward decls for such kind - it avoids circular dependency errors and speed up compilation.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:

                      I don't know

                      Ah ha! Better :) So you never intended "No because he includes", rather "If he includes"...

                      @TheCrowKaka

                      • If @Christian-Ehrlicher has been correctly clairvoyant about your mymainview.h having #include "modifypropcmd.h" in it then his is your answer.

                      • OTOH, if you need a mere mortal like myself --- no super-powers --- to help still, unlike @Christian-Ehrlicher I need to actually see what is in your mymainview.h file....

                      :)

                      1 Reply Last reply
                      2
                      • T Offline
                        T Offline
                        TheCrowKaka
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher, @JonB
                        Thanks for your answers.

                        I was about to type the elaborate answer, but while doing that i realised the error.

                        As @Christian-Ehrlicher indicated,
                        No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                        That was the exact problem.
                        I moved the modifypropcmd.h include from mymainview.h to mymainview.cpp and the solution compiled.

                        Thanks both for helping me out.

                        A Qt Enthusiastic...

                        JonBJ 1 Reply Last reply
                        0
                        • T TheCrowKaka

                          @Christian-Ehrlicher, @JonB
                          Thanks for your answers.

                          I was about to type the elaborate answer, but while doing that i realised the error.

                          As @Christian-Ehrlicher indicated,
                          No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency

                          That was the exact problem.
                          I moved the modifypropcmd.h include from mymainview.h to mymainview.cpp and the solution compiled.

                          Thanks both for helping me out.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:

                          As @Christian-Ehrlicher indicated,
                          No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
                          That was the exact problem.

                          I give up! If @Christian-Ehrlicher has enough telepathy to answer questions here without even seeing the code, what is the point in my even trying.... :( :) !!

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:

                            As @Christian-Ehrlicher indicated,
                            No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
                            That was the exact problem.

                            I give up! If @Christian-Ehrlicher has enough telepathy to answer questions here without even seeing the code, what is the point in my even trying.... :( :) !!

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @JonB said in Peculiar problem with QUndoCommand in a new class files.:

                            what is the point in my even trying.... :( :) !!

                            :D

                            This is just more than 20 years of experience - don't know how often I hit this problem by myself until I switched to forward decl. nearly everything what's possible :)

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            2

                            • Login

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