Using Q_Property to display properties

789 views Asked by At

I have a GUI displaying a tree architecture as shown here.

enter image description here

Each of those nodes are separate classes that are derived from the node above them. All of them inherit QObject for their implementation. Now I need to add a few properties to be displayed when the user selects Properties under the Right-Click menu of Implicit. Selecting this opens a window with the properties. I added these properties in the header file of Implicit like so :

#ifndef FCIMPLICIT_H
#define FCIMPLICIT_H
#include <QObject>
#include "Interface.h"
#include "ResourceItem.h"
#include "MonWindow.h"
#include "FCTab.h"
#include "ResourceItem.h"
#include "FCAbstract.h"
#include "FCInterface.h"
#include "FCConnections.h"
class CFCImplicit: public CResourceItem
{

    Q_OBJECT
    Q_PROPERTY(int FCPortID READ getPortID )
    Q_PROPERTY(QString Type READ getType )
    Q_PROPERTY(QString Status READ getStat )    
    Q_PROPERTY(int WWNodeNumber READ getNodeNo )
    Q_PROPERTY(int WWPortNumber READ getPortNo )
    Q_PROPERTY(bool AutoActive READ getAuto )
public:
    CFCImplicit(QObject*);
    ~CFCImplicit();

    QString                 getType();
    QString                 getStat();
    int                     getPortID();
    int                     getPortNo();
    int                     getNodeNo();
    bool                    getAuto();


};

FCinterface.h is the header of the FCASM node.

The issue is that only the first property is displayed, as seen in the second picture. Is there a reason why this is happening? Am I supposed to add something to the constructor or a new function?

The constructor for the Implicit class is

CFCImplicit::CFCImplicit(QObject* parent) : CResourceItem(parent)
{

    fnSetProperty("objectName", QString("Implicit"));   
    ((CResourceItem*)parent)->fnAddResources(this);

}

EDIT:

This is the code for all the READ functions

QString CFCImplicit::getType()
{
QString a;
a="Implicit";
return a;
}

QString CFCImplicit::getStat()
{QString a;
a="Idle";
return a;}

int CFCImplicit::getPortID()
{int a;
a=1;
return a;}

int CFCImplicit::getPortNo()
{int a;
a=2;
return a;}

int CFCImplicit::getNodeNo()
{int a;
a=2;
return a;}

bool CFCImplicit::getAuto()
{bool a;
a=true;
return a;}
2

There are 2 answers

0
Michael Thomas On

I found out what I was doing wrong. I assumed that since the properties were read only, I only needed a READ accessor function. By adding the WRITE accessor and adding the required WRITE functions, the properties were displayed. I don't exactly understand why this condition is required (maybe having just READ just makes the properties available for introspection), but it worked! So there you go.

Happy coding everyone!

4
Tomaz Canabrava On

No need to add the READ and the function, you can use the MEMBER to have direct access to the variable.