[Solved] use the struct (inside a class)



  • Hi friends.

    I want to have a class & some structs inside it.
    In my main class, I want to define a varible of struct.

    For example

    @
    MyClass
    typedef struct MyStruct
    {
    int a;
    int b;
    int c;
    int d;
    }MyStruct;
    Q_DECLARE_METATYPE(MyStruct)
    @

    @
    MainClass
    struct MyStruct xyz;
    @

    Thanks a lot.
    Ya Ali.


  • Moderators

    And do you have any question or problem?

    Just guessing, that something does not work, I'll post a code that should be OK:
    @
    MyClass
    struct MyStruct
    {
    int a;
    int b;
    int c;
    int d;
    };

    MainClass
    MyStruct xyz;
    @



  • Here is an another example:

    mmm.h
    @
    #ifndef MMM_H
    #define MMM_H

    #include <QObject>

    class mmm : public QObject
    {
    Q_OBJECT

    public:
    explicit mmm(QObject *parent = 0);
    typedef struct MyStruct
    {
    int a;
    int b;
    int c;
    int d;
    };

    signals:

    public slots:

    };

    #endif // MMM_H
    @

    mmm.cpp
    @
    #include "mmm.h"

    mmm::mmm(QObject *parent) :
    QObject(parent)
    {

    }
    @

    main.cpp
    @
    #include <QCoreApplication>
    #include "mmm.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    mmm instance;
    instance.MyStruct xyz;  ----------&gt; does not work!
    
    return a.exec&#40;&#41;;
    

    }
    @

    I want to define a variable of MyStruct in main.cpp


  • Moderators

    Remove that "typedef", it is not needed.

    And define the variable using mmm::MyStruct syntax.



  • Hii,
    you can do as well.
    @
    class ABC
    struct EventInfo
    {
    QString mcstrCMD_ID;
    QString mcstrTYPE;
    QString mcsKEYBOARDEVENT;
    QString mcstrX;
    QString mcstrY;
    }eventInfo;

    @
    @
    ABC hh;
    QString strID=hh.eventInfo.mcstrCMD_ID;
    @
    hope it helps!



  • Thank you very very much dear sierdzio.
    Worked!


  • Moderators

    You are welcome, have fun :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.