Trouble with Q_PROPERTY()
-
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?
-
Hi,
It's a extern method named foo. Your call means "execute the foo method from class TheHeader".
-
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?
-
Hi,
It's a extern method named foo. Your call means "execute the foo method from class TheHeader".
-
Well, the first thing is: why is it declared as external ?
-
@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' -
I meant why are you using it like that ?
Where does that method come from originally ? -
@SGaist Well it's declared in TheHeader.cpp
bool foo() { return true }I am just trying to learn how to use it...
-
@RobM
Did you try just calling it viafoo()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.