-1

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 the instantiated logic

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
Ezike Ebuka
  • 137
  • 2
  • 8
  • 2
    I'm not quite clear about the problems you have. If you have a member variable `m_lnfListModel`, you simply "access" it like any other member variable? What you tried yourself? What problems you have with your attempt? Please [edit] your question to add more information. Also please take some time to refresh [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 05 '20 at 11:07
  • The header file we need to see is `"lnflogic.h"` that's the one that will show how to access `m_lnflistmodel`. – john Aug 05 '20 at 11:23
  • @Someprogrammerdude i just started using c++, and i am not that good with it ,but this is what i tried in the main `QList tmp = logic.lnflist().m_themes ;`,`Qlist tmp = logic.lnflist(m_themes); `both dont work as i am not to familiar with the syntax – Ezike Ebuka Aug 05 '20 at 11:41
  • @john i have added the `lnflogic.h` file – Ezike Ebuka Aug 05 '20 at 11:42
  • What is `logic.lnflist()`? What does it return? Please try to create a [mcve] of your code, with emphasis on both the *minimal* and *complete* parts. – Some programmer dude Aug 05 '20 at 11:43
  • @Someprogrammerdude it returns `m_lnflistmodel` created in the constructor . thanks for the links this is like my thrid question on stack overflow still get used to it , i have also added it to main question too. i also tried this ` LnfListModel t = &logic.lnfList();` but it says `expression must be an lvalue or a function designator` if i remove the "& " it says `no suitable constructor exists to convert from "LnfListModel *" to "LnfListModel"` because the constructor is a pointer – Ezike Ebuka Aug 05 '20 at 12:08
  • A proper c++ tutorial can help you more than Stack Overflow in this case. – scopchanov Aug 05 '20 at 12:24
  • @scopchanov stackoverflow is used for asking questions to problems you are facing in any code , that is what i did if i want a tutorial i know where to get one, even if i dont understand it that is the point of stack over flow , instead of answering the question you just go off topic , that is not how to help and the comment did not contribute to anything whatsoever . oh well have a nice day, some one else has already helped me by explaining it rather than refferals – Ezike Ebuka Aug 05 '20 at 18:07
  • _because the constructor is a pointer_ this does not make sense. You are missing a basic terminology and SO is not the place to learn it. – scopchanov Aug 05 '20 at 18:12
  • To better understand the purpose of SO and spare yourself the troubles in the future, please read the [accepted answer](https://meta.stackoverflow.com/a/261593/5366641) to the question _How much research effort is expected of Stack Overflow users?_. Pay a close attention to this sentence: _Asking a question on Stack Overflow should be the last step in your process for finding an answer - if the information that you need already exists, then you very much want to find it._ Another helpful source of information is [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – scopchanov Aug 05 '20 at 18:41
  • _some one else has already helped me by explaining it rather than refferals_ The accepted answer makes the same statement as mine: _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._ – scopchanov Aug 05 '20 at 18:45
  • this is would have been a better response than your first , and not sound off putting – Ezike Ebuka Aug 05 '20 at 20:36

1 Answers1

1

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • thanks this is what i want, as you said i did not write it as its an open source code and i am trying to use it to learn c++ while contributing to it – Ezike Ebuka Aug 05 '20 at 18:02