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. [SOLVED] QList<customObcject> saved with QSettings
QtWS25 Last Chance

[SOLVED] QList<customObcject> saved with QSettings

Scheduled Pinned Locked Moved General and Desktop
qsettingsqlistqdeclaremetatyp
7 Posts 4 Posters 3.0k 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.
  • M Offline
    M Offline
    Michal
    wrote on 24 May 2015, 13:28 last edited by Michal
    #1

    There is similar topic on the forum LINK but the guy found out different solution.

    Im trying to save data using QSettings QList<recipe>. I've read that I need to use Q_DECLARE_METATYPE( Type) I deleted unnecesary data.
    Here is my recipe.h

    class recipe
    {
    public:
        recipe();
        recipe(const recipe &obj);
        ~recipe();
    };
    // 1) Q_DECLARE_METATYPE(recipe)
    
    //recipe.cpp
    #include "recipe.h"
    recipe::recipe()
    {
    }
    
    recipe::recipe( const recipe &obj)
    {
    }
    
    recipe::~recipe()
    {
    }
    

    When I uncomment 1) I have an error: D:\Projects\FoodBook\recipe.cpp:3: error: expected constructor, destructor, or type conversion before 'Q_DECLARE_METATYPE'
    Q_DECLARE_METATYPE(recipe).

    What is it that I do wrong? or how would any of you save List of custom class objects to be loaded at startup??

    Thanks for your support.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Charlie_Hdz
      wrote on 24 May 2015, 14:44 last edited by
      #2

      Well, First at all, Q_DECLARE_METATYPE is about the MetaObject System of Qt,this mean that your object "recipe" must be a QObject to work.

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      1 Reply Last reply
      0
      • C Online
        C Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on 24 May 2015, 15:00 last edited by
        #3

        @Charlie_Hdz No, it doesn't have to be a QObject.
        @Michal You don't seem to include <QMetaType> in your header. That's probably it.

        1 Reply Last reply
        1
        • K Offline
          K Offline
          KiwiJeff
          wrote on 24 May 2015, 15:04 last edited by
          #4

          This works for me:

          #ifndef RECIPE_H
          #define RECIPE_H
          
          #include <QMetaType>
          
          class recipe
          {
          public:
              recipe();
              recipe(const recipe &obj);
              ~recipe();
          };
          
          Q_DECLARE_METATYPE(recipe)
          
          #endif // RECIPE_H
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            Michal
            wrote on 25 May 2015, 12:39 last edited by
            #5

            Of course! didn't know macros need includes...thanks!

            recipe newRecipe;
            newRecipe.setName("NotConverted");
            qDebug() << "BeforeConvName: " << newRecipe.Name();
            
            QVariant variant = QVariant::fromValue(newRecipe);
                if(variant.canConvert<recipe>())
                {
                    qDebug() << "Can Convert";
                    recipe anotherNewRecipe = variant.value<recipe>();
                    qDebug()<<"AfterConvName:"<<anotherNewRecipe.Name();
                }
            

            anotherNewRecipe's name shows result: "" . Why?

            And by the way, another question:
            Why does the customObject (in this case recipe) can't be created dynamically?
            If I do so, there is an error and It will not go through.

            Some help under the LINK regarding MetaType.

            1 Reply Last reply
            0
            • C Online
              C Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 25 May 2015, 12:52 last edited by
              #6

              @Michal said:

              Why?

              Because your copy constructor does not copy anything?

              If I do so, there is an error and It will not go through

              What does the error say?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Michal
                wrote on 25 May 2015, 13:02 last edited by
                #7

                error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>' enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)}

                I have just read that: Values of a registered type can be constructed dynamically via QMetaType::construct()
                There is an example in the link above. Thanks for your help! I will figure it out, still missing pure c++ knowledge ;)

                1 Reply Last reply
                0

                7/7

                25 May 2015, 13:02

                • Login

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