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. QObject::setProperty() always return false
Forum Updated to NodeBB v4.3 + New Features

QObject::setProperty() always return false

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 4.8k 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.
  • G Offline
    G Offline
    gbstack
    wrote on last edited by
    #1

    I defined a User class derived from QObject

    @#ifndef USER_H
    #define USER_H

    #include <string>
    using namespace std;

    #endif // USER_H
    class User : public QObject{
    Q_OBJECT
    Q_PROPERTY(string name READ getName WRITE setName)
    Q_PROPERTY(string mobile)
    public:

    string getName(){return this->m_name;}
    void setName(const string &_name){this->m_name = _name;}
    
    string m_name;
    

    };
    @

    then I use setProperty() to set "name" property
    @User *user = new User();
    if(!user->setProperty("name", "aaa")){
    QMessageBox::information(NULL, "setproperty failed", "failed");
    }@

    but setProperty() always return false...

    Any suggestion is appreciated:)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tilsitt
      wrote on last edited by
      #2

      Hi,

      I am not sure it will solve it, but your read accessor should be declared const and your property should be in the private section.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gbstack
        wrote on last edited by
        #3

        Thanks for your reply, I modifed code as you said, but unfortunatelly the same problem still exists..

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tilsitt
          wrote on last edited by
          #4

          I read the "doc":http://qt-project.org/doc/qt-4.8/properties.html#qt-s-property-system a second time: properties with a custom type (std::string is a custom type for Qt) must be registered with Q_DECLARE_METATYPE to be used with QVariant (see this "section":http://qt-project.org/doc/qt-4.8/properties.html#properties-and-custom-types). That could be your problem.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            As tilsitt has alredy mentioned you will have to declare std::string as a metatype (so it can be used with QVariant).
            @
            class User : public QObject{
            Q_PROPERTY(string name READ getName WRITE setName)
            ...
            };
            Q_DECLARE_METATYPE(string)
            @
            Be aware that string and std::string are two different types (to the metatype system). So it is either <code>Q_PROPERTY(string name ...)</code> and <code>Q_DECLARE_METATYPE(string)</code> or <code>Q_PROPERTY(std::string name ...)</code> and <code>Q_DECLARE_METATYPE(std::string)</code>. You cannot mix both.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #6

              Why not use "QString" instead of "string"?

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gbstack
                wrote on last edited by
                #7

                [quote author="tilsitt" date="1358517715"]I read the "doc":http://qt-project.org/doc/qt-4.8/properties.html#qt-s-property-system a second time: properties with a custom type (std::string is a custom type for Qt) must be registered with Q_DECLARE_METATYPE to be used with QVariant (see this "section":http://qt-project.org/doc/qt-4.8/properties.html#properties-and-custom-types). That could be your problem.[/quote]

                Thanks a lot!
                As you said, custom type "string" is the problem. I tried Q_DECLARE_METATYPE, it still return false. But after using QString instead of "string", it works now!
                My boss is a little hurry, so I decide to use QString

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gbstack
                  wrote on last edited by
                  #8

                  [quote author="Lukas Geyer" date="1358517907"]As tilsitt has alredy mentioned you will have to declare std::string as a metatype (so it can be used with QVariant).
                  @
                  class User : public QObject{
                  Q_PROPERTY(string name READ getName WRITE setName)
                  ...
                  };
                  Q_DECLARE_METATYPE(string)
                  @
                  Be aware that string and std::string are two different types (to the metatype system). So it is either <code>Q_PROPERTY(string name ...)</code> and <code>Q_DECLARE_METATYPE(string)</code> or <code>Q_PROPERTY(std::string name ...)</code> and <code>Q_DECLARE_METATYPE(std::string)</code>. You cannot mix both.[/quote]

                  [quote author="Code_ReaQtor" date="1358519157"]Why not use "QString" instead of "string"?

                  [/quote]

                  thanks all, using QString works.
                  (Q_DECLARE_METATYPE(string) still let QObject::setProperty return false. But my boss is a little hurry, so I decide to use QString)

                  1 Reply Last reply
                  0

                  • Login

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