i have three files main.cpp ,lnflogic.cpp and listmodel.cpp this are the constructors of listlogic.cpp
i instantiate lnflogic like this in my main.cpp
LnfLogic logic;
i want to access a class in m_lnflistmodel called themesinfo (it is in its header created as a QList called m_themes )
when debugging i am able to see that m_themes is already created (shown in the picture below how do i access it ?
here are the files
LnfLogic.cpp
#include "lnflogic.h"
#include "lnflistmodel.h"
LnfLogic::LnfLogic()
: m_themeName(QStringLiteral("org.kde.breeze.desktop")),
m_lnfListModel(new LnfListModel(this)),
m_needsSave(false)
{
m_package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
}
LnfLogic::~LnfLogic()
{
}
LnfListModel *LnfLogic::lnfList()
{
return m_lnfListModel;
}
**listmodel Constructors it has two classes themesinfo and lnflistmodel **
#include "lnflistmodel.h"
LnfListModel::LnfListModel( QObject *parent )
: QAbstractListModel( parent )
{
m_roleNames.insert(Qt::DisplayRole, "displayRole");
m_roleNames.insert(PackageNameRole, "packageNameRole");
m_roleNames.insert(PackageDescriptionRole, "packageDescriptionRole");
m_roleNames.insert(PackageAuthorRole, "packageAuthorRole");
m_roleNames.insert(PackageVersionRole, "packageVersionRole");
reload();
}
LnfListModel::~LnfListModel()
{
clearThemeList();
}
This is the header file for listmodel
#ifndef LNFLISTMODEL_H
#define LNFLISTMODEL_H
#include <QAbstractItemView>
namespace Plasma
{
}
//Theme selector code by Andre Duffeck (modified to add package description)
//not sure if i would need this
class ThemeInfo
{
public:
QString name;
QString package;
QString description;
QString author;
QString version;
QString themeRoot;
};
class LnfListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
enum { PackageNameRole = Qt::UserRole,
PackageDescriptionRole = Qt::UserRole + 1,
PackageAuthorRole = Qt::UserRole + 2,
PackageVersionRole = Qt::UserRole + 3
};
explicit LnfListModel(QObject *parent = nullptr);
~LnfListModel() override;
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QModelIndex indexOf(const QString &path) const;
bool themeExists(const QString &name) const;
void reload();
void clearThemeList();
int count() const
{
return rowCount();
}
Q_INVOKABLE QVariantMap get(int index) const;
Q_SIGNALS:
void countChanged();
private:
QHash<int, QByteArray> m_roleNames;
QList<ThemeInfo> m_themes;
};
The lnflogic header file
#define LNFLOGIC_H
#include <QAbstractListModel>
#include <kpackage/package.h>
class LnfListModel;
class LnfLogic : public QObject
{
Q_OBJECT
Q_PROPERTY(LnfListModel *lnfList READ lnfList CONSTANT)
Q_PROPERTY(QString theme READ theme WRITE setTheme NOTIFY themeChanged)
Q_PROPERTY(bool isWritable READ isWritable NOTIFY themeChanged)
Q_PROPERTY(QString themeFolder READ themeFolder NOTIFY themeChanged)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString comment READ comment WRITE setComment NOTIFY commentChanged)
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
Q_PROPERTY(QString version READ version WRITE setVersion NOTIFY versionChanged)
Q_PROPERTY(QString website READ website WRITE setWebsite NOTIFY websiteChanged)
Q_PROPERTY(QString license READ license WRITE setLicense NOTIFY licenseChanged)
Q_PROPERTY(QString thumbnailPath READ thumbnailPath NOTIFY themeChanged)
Q_PROPERTY(bool performLayoutDump READ performLayoutDump WRITE setPerformLayoutDump NOTIFY performLayoutDumpChanged)
Q_PROPERTY(bool performDefaultsDump READ performDefaultsDump WRITE setPerformDefaultsDump NOTIFY performDefaultsDumpChanged)
Q_PROPERTY(bool needsSave READ needsSave NOTIFY needsSaveChanged)
public:
enum ErrorLevel
{
Info,
Error
};
Q_ENUMS(ErrorLevel)
LnfLogic();
~LnfLogic() override;
LnfListModel *lnfList();
bool themeExists(const QString &plugin);
void setTheme(const QString &theme);
QString theme() const;
bool isWritable() const;
QString themeFolder() const;
QString name() const;
void setName(const QString &name);
QString comment() const;
void setComment(const QString &comment);
QString author() const;
void setAuthor(const QString &author);
QString email() const;
void setEmail(const QString &email);
QString version() const;
void setVersion(const QString &version);
QString website() const;
void setWebsite(const QString &website);
QString license() const;
void setLicense(const QString &license);
bool performLayoutDump() const;
void setPerformLayoutDump(bool dump);
bool performDefaultsDump() const;
void setPerformDefaultsDump(bool dump);
QString thumbnailPath() const;
void dumpPlasmaLayout(const QString &pluginName);
bool needsSave();
Q_INVOKABLE void save();
Q_INVOKABLE void createNewTheme(const QString &pluginName, const QString &name, const QString &comment, const QString &author, const QString &email, const QString &license, const QString &website);
Q_INVOKABLE void processThumbnail(const QString &path);
Q_INVOKABLE QString openFile();
void dumpCurrentPlasmaLayout();
void dumpDefaultsConfigFile(const QString &pluginName);
Q_SIGNALS:
void themeChanged();
void messageRequested(ErrorLevel level, const QString &message);
void needsSaveChanged();
void nameChanged();
void commentChanged();
void authorChanged();
void emailChanged();
void versionChanged();
void websiteChanged();
void licenseChanged();
void performLayoutDumpChanged();
void performDefaultsDumpChanged();
private:
QString m_themeName;
KPackage::Package m_package;
LnfListModel *m_lnfListModel;
QHash<QString, QString> m_tempMetadata;
bool m_performLayoutDump : 1;
bool m_performDefaultsDump : 1;
bool m_needsSave;
};
#endif // LNFLOGIC_H
Now that I can see the header file I see that m_lnfListModel
is a pointer, not an object. I also see that there is a method to access this ponter lnfList
. So to access the pointer it is just logic.lnfList()
.
Now remember that what lnfList
is returning is a pointer, so if you wanted to say reload the list (whatever that means) it would be
logic.lnfList()->reload();
->
is used because we're dealing with a pointer.
If you want to save the list in a variable then that would be
LnfListModel* ptr = logic.lnfList();
and then (using the reload example again)
ptr->reload();
But note that ptr
here is a pointer, so you are not copying the list out of the logic
variable, what you have here is a way of accessing the list that is part of the logic
variable.
I hope that's clear. It's obvious that this is code you have acquired from somewhere rather than code you've written yourself. In order to get the most out of this you should probably spend some time studying C++ on it's own terms, rather than just trying to work with this code. Any introductory C++ book will cover the topics I've very briefly mentioned here. You can find some book recommendations here.