File is not defined.
-
Looks like somewhere you are doing the cycle.
- Info.h is included in one.h.
- one.h is included in two.h and
- two.h includes info.h again.
-
@dheerendra
Would your situation actually result inReferenceError: Info is not defined
? I would have thought it would be more like "duplicate definition found"?If this does turn out to be the case, the OP needs to put in
#ifndef
guards to prevent re-inclusion. -
@dheerendra Nope, it is included only once... inside my info.cpp file and i have only one header file and one source file as of now (Info.cpp and Info.h)
-
That's a JS/QML error, where does it occur and can you share some code ?
-
This is the actual error:
qrc:/Theme/Icons.qml:33: ReferenceError: Info is not defined qrc:/Theme/Font.qml:53: ReferenceError: Info is not defined
and in the lines of these files, following lines of code is there
@qrc:/Theme/Icons.qml:33: readonly property string imagePath: Info.getQMLResourcePath() + "Theme/Images/" @qrc:/Theme/Font.qml:53: source: Info.getQMLResourcePath() + "Theme/Fonts/ProLight.otf";
-
Did you use
qmlRegisterSingletonType
on yourInfo
type ?
And then added an import line for the module in your qml file ? -
Here is the code inside my "Info.cpp" file for your reference.
// own header #include "Info.h" #include <QCoreApplication> #include <QQmlEngine> #include <QDir> Q_LOGGING_CATEGORY(LC_Info, "Info") // Root direcoty const QString Info::ASSETS_FOLDER_NAME = "assets"; //// System & User directories const QString Info::SYSTEM_ASSETS_FOLDER_NAME = "system"; const QString Info::USER_ASSETS_FOLDER_NAME = "user"; const QString Info::SETTINGS_ASSETS_FOLDER_NAME = "settings"; const QString Info::RESOURCE_ASSETS_FOLDER_NAME = "resources"; //// System directories const QString Info::THERAPY_ASSETS_FOLDER_NAME = "therapies"; const QString Info::LANGUAGE_ASSETS_FOLDER_NAME = "lang"; //// User directories const QString Info::STATISTICS_FOLDER_NAME = "statistics"; const QString Info::EMG_DATA_FOLDER_NAME = "emg-data"; const QString Info::FAVORITES_FOLDER_NAME = "favorites"; const QString Info::PATIENT_PROGRAMS_FOLDER_NAME = "patient-programs"; const QString Info::LOG_FOLDER_NAME = "log"; //// Static filenames const QString Info::DEFAULT_THERAPY_VALUES_FILE_NAME = "therapies.json"; Info::Info() : QObject(), m_buildInQmlResource(true) { QString envAssetsPath = qgetenv("ASSETS_PATH"); QStringList assetsSearchPaths; if (!envAssetsPath.isEmpty()) { assetsSearchPaths.append(envAssetsPath); } if (QCoreApplication::instance() != nullptr) { assetsSearchPaths.append(QCoreApplication::applicationDirPath()); } assetsSearchPaths.append(QDir::currentPath()); foreach (QString path, assetsSearchPaths) { QDir d(path + QDir::separator() + ASSETS_FOLDER_NAME); // Found Assets folder: if (d.exists()) { m_assetsPath = d.absolutePath() + QDir::separator(); break; } } // User assets directory name can be adjusted through environment // variable. This is mainly for defining a spearate path for testing // purposes (e.g. unit tests) QByteArray envUserAssetsName = qgetenv("USER_ASSETS_NAME"); m_userAssetsFolderName = envUserAssetsName.isEmpty() ? USER_ASSETS_FOLDER_NAME : envUserAssetsName; } bool Info::createPath(const QString & path) { QDir d(path); if (d.exists()) { return true; } return d.mkpath(path); } Info *Info::getInstance() { static Info info; return &info; } QObject *Info::getInstanceQml(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) QObject * inst = Info::getInstance(); QQmlEngine::setObjectOwnership(inst, QQmlEngine::CppOwnership); return inst; } QString Info::getUserPath() { return getInstance()->m_assetsPath + getInstance()->m_userAssetsFolderName + QDir::separator(); } QString Info::getUserSettingsPath() { return getInstance()->getUserPath() + Info::SETTINGS_ASSETS_FOLDER_NAME + QDir::separator(); } QString Info::getStatisticsPath() { return getInstance()->getUserPath() + Info::STATISTICS_FOLDER_NAME + QDir::separator(); } QString Info::getEmgDataPath() { return getInstance()->getUserPath() + Info::EMG_DATA_FOLDER_NAME + QDir::separator(); } QString Info::getFavoritesPath() { return getInstance()->getUserPath() + Info::FAVORITES_FOLDER_NAME + QDir::separator(); } QString Info::getLogPath() { return getInstance()->getUserPath() + Info::LOG_FOLDER_NAME + QDir::separator(); } QString Info::getPatientProgramsPath() { return getInstance()->getUserPath() + Info::PATIENT_PROGRAMS_FOLDER_NAME + QDir::separator(); } QString Info::getSystemPath() { return getInstance()->m_assetsPath + Info::SYSTEM_ASSETS_FOLDER_NAME + QDir::separator(); } QString Info::getAssetsPath() { return getInstance()->m_assetsPath + QDir::separator(); } QString Info::getLangPath() { return getInstance()->getSystemPath() + Info::LANGUAGE_ASSETS_FOLDER_NAME + QDir::separator(); } QString Info::getResourcePath() { return getInstance()->getSystemPath() + Info::RESOURCE_ASSETS_FOLDER_NAME + QDir::separator(); } QString Info::getQMLResourcePath() { return getInstance()->m_buildInQmlResource ? "qrc:/" : ("file:/" + Info::getResourcePath()); } QString Info::getSystemTherapyPath() { return getInstance()->getSystemPath() + Info::THERAPY_ASSETS_FOLDER_NAME + QDir::separator(); } QString Info::getSystemSettingsPath() { return getInstance()->getSystemPath() + Info::SETTINGS_ASSETS_FOLDER_NAME + QDir::separator(); } bool Info::init() { bool ok = true; qCDebug(LC_Info) << "************************************"; qCDebug(LC_Info) << "Initialising Neurotab Assets File System:"; qCDebug(LC_Info) << "Assets directory: " << getInstance()->m_assetsPath; // Assets path MUST exist!!! QDir ntSysPath(getSystemPath()); if (!ntSysPath.exists()) { qCCritical(LC_Info) << "Assets / System directory MUST exist! (" << getSystemPath() << ")"; return false; } if (!createPath(getUserPath())) { qCCritical(LC_Info) << "could not create " << getUserPath(); ok = false; } if (!createPath(getLogPath())) { qCCritical(LC_Info) << "could not create " << getLogPath(); ok = false; } if (!createPath(getUserSettingsPath())) { qCCritical(LC_Info) << "could not create " << getUserSettingsPath(); ok = false; } if (!createPath(getStatisticsPath())) { qCCritical(LC_Info) << "could not create " << getStatisticsPath(); ok = false; } if (!createPath(getEmgDataPath())) { qCCritical(LC_Info) << "could not create " << getEmgDataPath(); ok = false; } if (!createPath(getPatientProgramsPath())) { qCCritical(LC_Info) << "could not create " << getPatientProgramsPath(); ok = false; } if (!createPath(getFavoritesPath())) { qCCritical(LC_Info) << "could not create " << getFavoritesPath(); ok = false; } qCDebug(LC_Info) << "done\n"; return ok; } void Info::resetAllUserAssets() { QDir d(getUserPath()); d.removeRecursively(); init(); } void Info::useBuildInQml(bool buildIn) { getInstance()->m_buildInQmlResource = buildIn; }
-
This class looks great. This is the c++ claas. If u want to see this class at qml side, it needs to be registered to qml using qmlregistertype etc methods. Looks like your class is not exposed to qml. Please check and if not please do so. Search in Google or qt assistant for how to expose c++ class to qml.
-
I have registered to qml using qmlregistertype method and included it inside my qml file as follows. But it gives following errors when i build.
inside my main.cpp file
qmlRegisterType <Info>("NT.Cpp",1,0, "Info");
inside my Icons.qml file it is included as
import NT.Cpp 1.0
But many errors appear as follows:
/opt/Qt/5.6.3/gcc_64/include/QtQml/qqmlprivate.h:93: error: within this context class QQmlElement : public T ^ /Info.h:98: error: ‘Info::Info()’ is private Info(); ^ /opt/Qt/5.6.3/gcc_64/include/QtQml/qqmlprivate.h:93: ‘QQmlPrivate::QQmlElement<Info>::QQmlElement()’ is implicitly deleted because the default definition would be ill-formed: class QQmlElement : public T ^
-
Looks like you have private constructor in Info class. Can you show the info.h file ? Above post only has implementation file.
-
Yes it looks like a private constructor, and with a
getInstance
method to get the singleton instance.Use
qmlRegisterSingletonType
and notqmlRegisterType
. If you use this instance in c++ too, don't forget to set the object ownership (withsetObjectOwnership
) toQQmlEngine::CppOwnership
.