Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Trouble with Q_PROPERTY()
Qt 6.11 is out! See what's new in the release blog

Trouble with Q_PROPERTY()

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 1.5k 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.
  • R RobM

    I have a method from a header file that I want to access from QML: foo() and here is how that method is defined:

    extern bool foo();
    

    in the header: TheHeader.h

    So I have a "shell" code built by someone else and I want to add a new Q_PROPERTY() that will allow the front end (QML) to see if foo() is returning true or false, here is the "shell" code:

    #pragma once
    #include "UiControls/Presentation/Presenter.h"
    namespace TheSpace
    {
      namespace  TheCore
      {
        class SettingsInterface;
      }
      namespace Ui
      {
        class GuiSettings : public UiControls::Presenter
        {
            Q_OBJECT
            Q_PROPERTY(TheSpace::TheCore::SettingsInterface* model READ model CONSTANT)
            TheSpace::TheCore::SettingsInterface* m_model;
          public:
            GuiSettings(TheCore::SettingsInterface* _model);
            TheSpace::TheCore::SettingsInterface* model() const
            {
              return m_model;
            }
        };
      }
    }
    

    So I included the header and added the following:

    #pragma once
    #include "UiControls/Presentation/Presenter.h"
    #include <TheHeader.h>
    
    namespace TheSpace
    {
      namespace  TheCore
      {
        class SettingsInterface;
      }
      namespace Ui
      {
        class GuiSettings : public UiControls::Presenter
        {
            Q_OBJECT
            //added
            Q_PROPERTY(bool fooBool READ fooBool)
            
            Q_PROPERTY(TheSpace::TheCore::SettingsInterface* model READ model CONSTANT)
    
            TheSpace::TheCore::SettingsInterface* m_model;
    
          public:
            GuiSettings(TheCore::SettingsInterface* _model);
            //added
            bool fooBool() const;
    
            TheSpace::TheCore::SettingsInterface* model() const
            {
              return m_model;
            }
          //added
          private:
            bool m_fooBool = false;
        };
      }
    }
    

    Then in the .cpp file:

    #include "GuiSettings.h"
    #include "TheCore/Interfaces/SettingsInterface.h"
    #include <QtCore/QSettings>
    
    namespace TheSpace
    {
      namespace Ui
      {
        GuiSettings::GuiSettings(TheCore::SettingsInterface* _model)
          : m_model(_model)
        {
        }
      }
    }
    

    I added:

    #include "GuiSettings.h"
    #include "Core/Interfaces/SettingsInterface.h"
    #include <QtCore/QSettings>
    
    namespace TheSpace
    {
      namespace Ui
      {
        GuiSettings::GuiSettings(TheCore::SettingsInterface* _model)
          : m_model(_model)
         //added
          ,m_fooBool(false)
        {
        }
        //added
        bool GuiSettings::fooBool() const
        {
          if(TheHeader::foo() == true)
          {
            return !m_fooBool;
          }
          else
          {
            return m_fooBool;
          }
        }
      }
    }
    

    the problem is that when I compile I get an error:

    undefined reference to `TheHeader::foo()`
    

    I am rusty with C++ and new with connect QML too C++ so if someone could tell me what I am doing wrong here?

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

    @RobM said in Trouble with Q_PROPERTY():

    extern bool foo();

    If it's just written like that then TheHeader isn't relevant, you just want
    if(foo() == true)

    Or am I misunderstanding something? Is this a header file from a C library?

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      It's a extern method named foo. Your call means "execute the foo method from class TheHeader".

      R Offline
      R Offline
      RobM
      wrote on last edited by RobM
      #4

      @SGaist I am not sure what to do. Are you saying I need to redefine the foo() method inside of the header file?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Well, the first thing is: why is it declared as external ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply
        0
        • SGaistS SGaist

          Well, the first thing is: why is it declared as external ?

          R Offline
          R Offline
          RobM
          wrote on last edited by
          #6

          @SGaist I think it means I need to make a declaration of the function if I want to use it then I don't have to import the header?

          "the extern keyword extends the the function’s visibility to the whole program, the function can be used (called) anywhere in any of the files of the whole program, provided those files contain a declaration of the function" -- GeeksForGeeks

          but I tried that:

          TheHeader::foo() {}
          

          and I got:

          GuiSettings.cpp:16:20: error: cannot define or redeclare 'foo' here because namespace 'Ui' does not enclose namespace 'TheHeader'
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            I meant why are you using it like that ?
            Where does that method come from originally ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            0
            • SGaistS SGaist

              I meant why are you using it like that ?
              Where does that method come from originally ?

              R Offline
              R Offline
              RobM
              wrote on last edited by RobM
              #8

              @SGaist Well it's declared in TheHeader.cpp

              bool foo()
              {
                return true
              }
              

              I am just trying to learn how to use it...

              JonBJ 1 Reply Last reply
              0
              • R RobM

                @SGaist Well it's declared in TheHeader.cpp

                bool foo()
                {
                  return true
                }
                

                I am just trying to learn how to use it...

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

                @RobM
                Did you try just calling it via foo() like I said?

                I assume this not inside any class. If it is, then you need to call on an instance. But I don't know why you'd have extern bool foo() inside a class in a header.

                R 1 Reply Last reply
                0
                • JonBJ JonB

                  @RobM
                  Did you try just calling it via foo() like I said?

                  I assume this not inside any class. If it is, then you need to call on an instance. But I don't know why you'd have extern bool foo() inside a class in a header.

                  R Offline
                  R Offline
                  RobM
                  wrote on last edited by RobM
                  #10

                  @JonB Yes that's the first thing I tried :(

                  use of undeclared identifier `foo`

                  JonBJ 1 Reply Last reply
                  0
                  • R RobM

                    @JonB Yes that's the first thing I tried :(

                    use of undeclared identifier `foo`

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

                    @RobM
                    Perhaps you ought show more of TheHeader.h because I can't figure what's going on from your extracts.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      RobM
                      wrote on last edited by
                      #12

                      I figured it out. I needed to include a reference in the .pro file

                      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
                      • Unsolved