Vanilla C++ struct to valid Qt QObject

80 views Asked by At

Can someone help me with my problem (it's about C++ and Qt)? I have struct: bunch of field with different types. I want to proxy the object fields, other words, make the struct objects valid QObjects (create getter, setter, signal and Q_PROPERTY for each object field). So how can I create such proxy for any struct? There is actually a lot of structs, so I want to automate process of creation such proxies. Any tools or method how to do such thing? I tried to make it work, but there is a problem that the programming language does not have proper reflection and you can't create classes based on other classes.

I want to pass C++ object to QML to be able to change it from there, but I didn't get how to make it work.

I don't know how to build such architecture to be able to communicate from QML and vanilla C++ struct object.

Any ideas would be helpful!

P.S. I would like to give additional information on the problem I'm trying to solve.

I have several structs (lets say 100)

// fizzbuzz.h
#ifndef FIZZBUZZ_H
#define FIZZBUZZ_H

struct FizzBuzz
{
  int a;
  double b;
  bool c;
};

#endif // FIZZBUZZ_H

// foobar.h
#ifndef FOOBAR_H
#define FOOBAR_H

enum FooBarEnum
{
  FOO = 1,
  BAR = 12
};

struct FooBar
{
  bool k;
  FooBarEnum d;
  double c;
};

#endif // FOOBAR_H

and I want to get from given class something like that:

class {CLASS_NAME}_QObject : public QObject {
  Q_OBJECT
  
  // there will be as much setters, getters, signals and 
  // property macros as passed object has fields

  // property macro
  Q_PROPERTY({FIELD_TYPE} {FIELD_NAME} READ {FIELD_NAME} WRITE set{CAPITALIZED_FIELD_NAME})
public:
  // constructor
  explicit {CLASS_NAME}_QObject({CLASS_NAME} *object, QObject *parent = nullptr) 
    : QObject(parent)
    , m_object(object) 
  {
  
  }

  // getter
  {FIELD_TYPE} {FIELD_NAME}() const {
    return m_object->{FIELD_NAME};
  }

  // setter
  void set{CAPITALIZED_FIELD_NAME}({FIELD_TYPE} {FIELD_NAME}) {
    m_object->{FIELD_NAME}= {FIELD_NAME};
  }

signals:
  // signal
  void {FIELD_NAME}Changed({FIELD_TYPE} {FIELD_NAME});

private:
  {CLASS_NAME} *m_object;
}

Of cause it is not valid C++ code but there is comment to make it more clear.

Does anybody have suggestions how to create something like that? Maybe is it possible to solve this somehow more simple?

0

There are 0 answers