Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Nested class in Qt5
Forum Updated to NodeBB v4.3 + New Features

Nested class in Qt5

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
26 Posts 5 Posters 4.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

    @Juancinho_cardecan Why do you want to use nested classes? Especially if both are QObject?

    J Offline
    J Offline
    Juancinho_cardecan
    wrote on last edited by
    #8

    @jsulm I'm doing nested class for easy access to the variable brightValue inside a different class (In this case brightness class)

    mrjjM J jsulmJ 3 Replies Last reply
    0
    • J Juancinho_cardecan

      @jsulm I'm doing nested class for easy access to the variable brightValue inside a different class (In this case brightness class)

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @Juancinho_cardecan
      Since nested classes will never fly with MOC then you have to use other design.
      Use Friends classes
      https://www.geeksforgeeks.org/friend-class-function-cpp/
      For same level of easy access even its bad for encapsulation
      Using access functions is also a possibility as that is just more clean.

      1 Reply Last reply
      2
      • J Juancinho_cardecan

        @jsulm I'm doing nested class for easy access to the variable brightValue inside a different class (In this case brightness class)

        J Offline
        J Offline
        Juancinho_cardecan
        wrote on last edited by
        #10

        @Juancinho_cardecan Tanks for your support.
        In mi case the variables that I would want to manage are in a public slot.
        Could i change my function brightnessText as a friend in order to take values inside the function belonging to a slot?

        Something such as:

        #ifndef RGBLED_H
        #define RGBLED_H
        
        class brightness : public QObject
        {
            Q_OBJECT
        
        public slots:
            friend void brightnessText(const QString& in);
        

        and then accesss to the variables inside that function in my .cpp calling those variables from the other method rgbRecive:

        void rgbLed::brightness::brightnessText(const QString &in)
        {
            QString Bright = in;
            int brightValue;
            brightValue = Bright.toInt(&flag);
        }
        void rgbLed::rgbRecive(const QString &in)
        {
         
            QStringList listNew = in.split(",");
            QStringList a_list = listNew[0].split("(");
            QStringList b_list = listNew[2].split(")");
            /
         
            red = a_list[1].toInt(&flag);
            green = listNew[1].toInt(&flag);
            blue = b_list[0].toInt(&flag);
         
            r2 = regresion_red(red);
            g2 = regresion_green(green);
            b2 = regresion_blue(blue);
         
            r = (brightValue * r2) / 10 ;
            g = (brightValue * g2) / 10 ;
            b = (brightValue * b2) / 10 ;
         
            qDebug() << "BrightValue from other class:" << brightValue;
         
        }
         
         
        void brightness::brightnessText(const QString &in)
        {
        ;
            QString Bright = in;
            int brightValue;
            brightValue = Bright.toInt(&flag);
        }
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #11

          Hi
          Actually i want you to make the outer class a friend of the inner class so when inner class
          is moved to own files, the outer class can access all private variables also in the (was) nested class
          so that the effect is the same as a nested class.

          Doing
          friend void brightnessText(const QString& in);
          allows that function to access private members (in brightness class) when defined outside but im not sure that is what you want/need
          since a normal slot can do that already.

          I though you wanted something like

          class B;
          
          class A
          {
              int val; // this is private
              friend B;
          };
          
          class B
          {
              A obj; // like the nested class.
              void func()
              {
                  obj.val = 100; // this is not possible without friend
              }
          };
          
          J 1 Reply Last reply
          2
          • J Juancinho_cardecan

            @jsulm I'm doing nested class for easy access to the variable brightValue inside a different class (In this case brightness class)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #12

            @Juancinho_cardecan said in Nested class in Qt5:

            I'm doing nested class for easy access to the variable brightValue inside a different class

            This is really not a reason to use nested classes. You should think about clean design instead. Why not use getter/setter to access private fields?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • mrjjM mrjj

              Hi
              Actually i want you to make the outer class a friend of the inner class so when inner class
              is moved to own files, the outer class can access all private variables also in the (was) nested class
              so that the effect is the same as a nested class.

              Doing
              friend void brightnessText(const QString& in);
              allows that function to access private members (in brightness class) when defined outside but im not sure that is what you want/need
              since a normal slot can do that already.

              I though you wanted something like

              class B;
              
              class A
              {
                  int val; // this is private
                  friend B;
              };
              
              class B
              {
                  A obj; // like the nested class.
                  void func()
                  {
                      obj.val = 100; // this is not possible without friend
                  }
              };
              
              J Offline
              J Offline
              Juancinho_cardecan
              wrote on last edited by
              #13

              @mrjj Hi

              I've been tried to implement your recommendation and also with setters/getters but at time that I want to pass the brightValue from the function void brightness::brightnessText(const QString &in) into the function void rgbLed::rgbRecive(const QString &in) unfortunately the variable b takes a random value inside the method rgbRecive:

              This is the following code:

              class rgbLed;
              
              class brightness : public QObject
              {
                  Q_OBJECT
              public:
                  explicit brightness(QObject *parent = 0);
                  friend rgbLed;
                  void setBrightValue(int newbrightValueMember)
                  {
                      brightValueMember = newbrightValueMember;
                  };
                  int getBrightValue()
                  {
                      return brightValueMember;
                  }
              
                  bool flag = 0;
                  int brightValueMember;
              
              public slots:
                  void brightnessText(const QString& in);
              };
              
              class rgbLed : public QObject
              {
                  Q_OBJECT
                  Q_PROPERTY(int bright READ brigfht WRITE setBright NOTIFY brightChanged)
              public:
                  explicit rgbLed(QObject *parent = nullptr);
                  brightness obj;
                  int regresion_red(int red);
                  int regresion_green(int green);
                  int regresion_blue(int blue);
              
                  Q_INVOKABLE int bright();
                  Q_INVOKABLE int setBright(int x);
                  Q_INVOKABLE void add(rgbLed *x);
              
              
                  int r;
                  int g;
                  int b;
                  int r2;
                  int g2;
                  int b2;
                  int brightValue;
                  int myBrightness;
                  QStringList list;
                  QString my_in;
              
                  int red = 0;
                  int green = 0;
                  int blue = 0;
                  int pos = 0 ;
                  bool flag = 0;
                  int x;
              
              signals:
                  void brightChanged(int x);
              
              public slots:
                  void rgbRecive(const QString &in);
              };
              
              

              and in my .cpp:

              void brightness::brightnessText(const QString &in)
              {
              
                  QString Bright = in;
                  int brightValue;
                  brightValue = Bright.toInt(&flag);
                  setBrightValue(brightValue);
              
                  qDebug() << "bright: " << brightValue;
              }
              
              void rgbLed::rgbRecive(const QString &in)
              {
                  qDebug() << "ColorPicked:" << in;
              
                  QStringList listNew = in.split(",");
                  QStringList a_list = listNew[0].split("(");
                  QStringList b_list = listNew[2].split(")");
              
                  int b = obj.getBrightValue();
                  qDebug() << "Value: " << b;
              }
              

              Could anyone see why b is taken a random value?

              jsulmJ 1 Reply Last reply
              0
              • J Juancinho_cardecan

                @mrjj Hi

                I've been tried to implement your recommendation and also with setters/getters but at time that I want to pass the brightValue from the function void brightness::brightnessText(const QString &in) into the function void rgbLed::rgbRecive(const QString &in) unfortunately the variable b takes a random value inside the method rgbRecive:

                This is the following code:

                class rgbLed;
                
                class brightness : public QObject
                {
                    Q_OBJECT
                public:
                    explicit brightness(QObject *parent = 0);
                    friend rgbLed;
                    void setBrightValue(int newbrightValueMember)
                    {
                        brightValueMember = newbrightValueMember;
                    };
                    int getBrightValue()
                    {
                        return brightValueMember;
                    }
                
                    bool flag = 0;
                    int brightValueMember;
                
                public slots:
                    void brightnessText(const QString& in);
                };
                
                class rgbLed : public QObject
                {
                    Q_OBJECT
                    Q_PROPERTY(int bright READ brigfht WRITE setBright NOTIFY brightChanged)
                public:
                    explicit rgbLed(QObject *parent = nullptr);
                    brightness obj;
                    int regresion_red(int red);
                    int regresion_green(int green);
                    int regresion_blue(int blue);
                
                    Q_INVOKABLE int bright();
                    Q_INVOKABLE int setBright(int x);
                    Q_INVOKABLE void add(rgbLed *x);
                
                
                    int r;
                    int g;
                    int b;
                    int r2;
                    int g2;
                    int b2;
                    int brightValue;
                    int myBrightness;
                    QStringList list;
                    QString my_in;
                
                    int red = 0;
                    int green = 0;
                    int blue = 0;
                    int pos = 0 ;
                    bool flag = 0;
                    int x;
                
                signals:
                    void brightChanged(int x);
                
                public slots:
                    void rgbRecive(const QString &in);
                };
                
                

                and in my .cpp:

                void brightness::brightnessText(const QString &in)
                {
                
                    QString Bright = in;
                    int brightValue;
                    brightValue = Bright.toInt(&flag);
                    setBrightValue(brightValue);
                
                    qDebug() << "bright: " << brightValue;
                }
                
                void rgbLed::rgbRecive(const QString &in)
                {
                    qDebug() << "ColorPicked:" << in;
                
                    QStringList listNew = in.split(",");
                    QStringList a_list = listNew[0].split("(");
                    QStringList b_list = listNew[2].split(")");
                
                    int b = obj.getBrightValue();
                    qDebug() << "Value: " << b;
                }
                

                Could anyone see why b is taken a random value?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #14

                @Juancinho_cardecan Where do you call brightness::brightnessText?
                Do you call it on rgbLed::obj or maybe on another brightness instance?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                J 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Juancinho_cardecan Where do you call brightness::brightnessText?
                  Do you call it on rgbLed::obj or maybe on another brightness instance?

                  J Offline
                  J Offline
                  Juancinho_cardecan
                  wrote on last edited by
                  #15

                  @jsulm I called here after your words but the value doesn't change:

                  void rgbLed::rgbRecive(const QString &in)
                  {
                  qDebug() << "ColorPicked:" << in;

                  QStringList listNew = in.split(",");
                  QStringList a_list = listNew[0].split("(");
                  QStringList b_list = listNew[2].split(")");
                  
                  obj.brightnessText(in);
                  int b = obj.getBrightValue();
                  qDebug() << "Value: " << b;
                  

                  }

                  jsulmJ 1 Reply Last reply
                  0
                  • J Juancinho_cardecan

                    @jsulm I called here after your words but the value doesn't change:

                    void rgbLed::rgbRecive(const QString &in)
                    {
                    qDebug() << "ColorPicked:" << in;

                    QStringList listNew = in.split(",");
                    QStringList a_list = listNew[0].split("(");
                    QStringList b_list = listNew[2].split(")");
                    
                    obj.brightnessText(in);
                    int b = obj.getBrightValue();
                    qDebug() << "Value: " << b;
                    

                    }

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @Juancinho_cardecan Called where? And did you call it on rgbLed::obj?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J 2 Replies Last reply
                    2
                    • jsulmJ jsulm

                      @Juancinho_cardecan Called where? And did you call it on rgbLed::obj?

                      J Offline
                      J Offline
                      Juancinho_cardecan
                      wrote on last edited by
                      #17
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Juancinho_cardecan Called where? And did you call it on rgbLed::obj?

                        J Offline
                        J Offline
                        Juancinho_cardecan
                        wrote on last edited by
                        #18

                        @jsulm No I din't call it on rgbLed::obj, I called it inside the function void rgbLed::rgbRecive

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #19

                          Hi
                          So here

                          void rgbLed::rgbRecive(const QString &in)
                          {
                          obj.brightnessText(in);
                          int b = obj.getBrightValue();
                          

                          b is not what you expect ?

                          J 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            Hi
                            So here

                            void rgbLed::rgbRecive(const QString &in)
                            {
                            obj.brightnessText(in);
                            int b = obj.getBrightValue();
                            

                            b is not what you expect ?

                            J Offline
                            J Offline
                            Juancinho_cardecan
                            wrote on last edited by
                            #20

                            @mrjj yes it's not what I'm expecting, it might be changing all the time but the value is zero always.

                            mrjjM 1 Reply Last reply
                            0
                            • J Juancinho_cardecan

                              @mrjj yes it's not what I'm expecting, it might be changing all the time but the value is zero always.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #21

                              @Juancinho_cardecan
                              I would check

                              brightValue = Bright.toInt(&flag);
                              (in void brightness::brightnessText(const QString &in))
                              and see if flag is false as that mean text to Int failed.

                              J 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @Juancinho_cardecan
                                I would check

                                brightValue = Bright.toInt(&flag);
                                (in void brightness::brightnessText(const QString &in))
                                and see if flag is false as that mean text to Int failed.

                                J Offline
                                J Offline
                                Juancinho_cardecan
                                wrote on last edited by
                                #22

                                @mrjj the flag is true and the value of brightValue is passing correctly, then the text to Int didn't failed, i dont know what is happening because if the values from QML are passing correctly, I'm calling in the wrong way the function brightnessText inside:

                                cpp file:

                                void rgbLed::rgbRecive(const QString &in)
                                {
                                     my_brightness.brightnessText(in);
                                    int b = my_brightness.getBrightValue();
                                    qDebug() << "Value: " << b;
                                }
                                
                                void brightness::brightnessText(const QString &in)
                                {
                                    QString Bright = in;
                                    int brightValue;
                                    brightValue = Bright.toInt(&flag);
                                    qDebug() << "brightValue: " << brightValue;
                                    qDebug() << "flag: " << flag;
                                    setBrightValue(brightValue);
                                
                                    qDebug() << "bright: " << brightValue;
                                }
                                
                                
                                
                                

                                main.cpp:

                                int main(int argc, char *argv[])
                                {
                                
                                    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                                    QGuiApplication app(argc, argv);
                                   
                                
                                    QQmlApplicationEngine engine;
                                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                                    if (engine.rootObjects().isEmpty())
                                            return -1;
                                
                                    //Root Object to find Items in QML
                                    QObject* root = engine.rootObjects()[0];
                                    assert(root != nullptr);
                                
                                    // The QML Item to which we want to inject our QML-Item-loaded-from-file
                                    QQuickItem* colorSelector = qobject_cast<QQuickItem*>(root->findChild<QObject*>("colorSelector"));
                                    assert(colorSelector != nullptr);
                                
                                
                                    // Load the QML file to a component
                                    QString qml_path = "ColorSelector.qml";
                                    QQmlComponent comp(&engine, QUrl::fromLocalFile(qml_path));
                                
                                    QScopedPointer<rgbLed> slider (new rgbLed);
                                    engine.rootContext()->setContextProperty("slider", slider.data());
                                    QObject *topLevel = engine.rootObjects().value(0);
                                    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
                                
                                    // Create an instance of the component
                                    rgbLed my_rgbLed;//Creation an Object instance
                                    brightness my_brightness;
                                
                                    //Code to conecct a signal with the slot
                                    QObject::connect(colorSelector, SIGNAL(colorChanged(QString)), &my_rgbLed, SLOT(rgbRecive(QString)));
                                
                                    // connect our QML signal to our C++ slot
                                      QObject::connect(window, SIGNAL(submitTextField(QString)), &my_brightness, SLOT(brightnessText(QString)));
                                
                                    return app.exec();
                                }
                                
                                
                                

                                header file:

                                class rgbLed;
                                
                                class brightness : public QObject
                                {
                                    Q_OBJECT
                                public:
                                    explicit brightness(QObject *parent = 0);
                                    friend rgbLed;
                                    void setBrightValue(int newbrightValueMember)
                                    {
                                        brightValueMember = newbrightValueMember;
                                    };
                                    int getBrightValue()
                                    {
                                        return brightValueMember;
                                    }
                                
                                    bool flag = 0;
                                    int brightValueMember;
                                
                                public slots:
                                    void brightnessText(const QString& in);
                                };
                                
                                
                                class rgbLed : public QObject
                                {
                                    Q_OBJECT
                                    Q_PROPERTY(int bright READ brigfht WRITE setBright NOTIFY brightChanged)
                                public:
                                    explicit rgbLed(QObject *parent = nullptr);
                                    brightness my_brightness;
                                    int regresion_red(int red);
                                    int regresion_green(int green);
                                    int regresion_blue(int blue);
                                
                                    Q_INVOKABLE int bright();
                                    Q_INVOKABLE int setBright(int x);
                                    Q_INVOKABLE void add(rgbLed *x);
                                
                                
                                    int r;
                                    int g;
                                    int b;
                                    int r2;
                                    int g2;
                                    int b2;
                                    int brightValue;
                                    int myBrightness;
                                    QStringList list;
                                    QString my_in;
                                
                                    int red = 0;
                                    int green = 0;
                                    int blue = 0;
                                    int pos = 0 ;
                                    bool flag = 0;
                                    int x;
                                
                                signals:
                                    void brightChanged(int x);
                                
                                public slots:
                                    void rgbRecive(const QString &in);
                                }
                                
                                1 Reply Last reply
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #23

                                  Hi
                                  I think you have 2 instances of brightness called my_brightness

                                  void rgbLed::rgbRecive(const QString &in)
                                  {
                                  my_brightness.brightnessText(in); <<< is the SAME my_brightness as you have in main.cpp?
                                  int b = my_brightness.getBrightValue();
                                  qDebug() << "Value: " << b;
                                  }

                                  so
                                  rgbLed my_rgbLed;//Creation an Object instance
                                  brightness my_brightness;

                                  how does the my_brightness from outside come into

                                  void rgbLed::rgbRecive(const QString &in) ?
                                  It seems it has its own ?
                                  So one is being updated but other is not ?

                                  In the original code, it was called

                                  class rgbLed : public QObject
                                  {
                                      Q_OBJECT
                                      Q_PROPERTY(int bright READ brigfht WRITE setBright NOTIFY brightChanged)
                                  public:
                                      explicit rgbLed(QObject *parent = nullptr);
                                      brightness obj; <<< the old internal name of the instance
                                  

                                  So it works from QML
                                  as you do
                                  // connect our QML signal to our C++ slot
                                  QObject::connect(window, SIGNAL(submitTextField(QString)), &my_brightness, SLOT(brightnessText(QString)));

                                  and that changes the one in Mian.cpp
                                  but the rgbLed my_rgbLed; and its "obj" is not.

                                  I assume you want the brigness instance inside my_rgbLed to change ?

                                  jsulmJ 1 Reply Last reply
                                  3
                                  • mrjjM mrjj

                                    Hi
                                    I think you have 2 instances of brightness called my_brightness

                                    void rgbLed::rgbRecive(const QString &in)
                                    {
                                    my_brightness.brightnessText(in); <<< is the SAME my_brightness as you have in main.cpp?
                                    int b = my_brightness.getBrightValue();
                                    qDebug() << "Value: " << b;
                                    }

                                    so
                                    rgbLed my_rgbLed;//Creation an Object instance
                                    brightness my_brightness;

                                    how does the my_brightness from outside come into

                                    void rgbLed::rgbRecive(const QString &in) ?
                                    It seems it has its own ?
                                    So one is being updated but other is not ?

                                    In the original code, it was called

                                    class rgbLed : public QObject
                                    {
                                        Q_OBJECT
                                        Q_PROPERTY(int bright READ brigfht WRITE setBright NOTIFY brightChanged)
                                    public:
                                        explicit rgbLed(QObject *parent = nullptr);
                                        brightness obj; <<< the old internal name of the instance
                                    

                                    So it works from QML
                                    as you do
                                    // connect our QML signal to our C++ slot
                                    QObject::connect(window, SIGNAL(submitTextField(QString)), &my_brightness, SLOT(brightnessText(QString)));

                                    and that changes the one in Mian.cpp
                                    but the rgbLed my_rgbLed; and its "obj" is not.

                                    I assume you want the brigness instance inside my_rgbLed to change ?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #24

                                    @mrjj said in Nested class in Qt5:

                                    I think you have 2 instances of brightness called my_brightness

                                    I already tryied to explain that...

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    3
                                    • JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #25

                                      @Juancinho_cardecan
                                      Assuming @jsulm & @mrjj are correct, each time you create a brightness object anywhere why don't you set its https://doc.qt.io/qt-5/qobject.html#objectName-prop (setObjectName()) to a unique string, then you can see whether one instance is different from another. Printing out &brightnessObjectInstance may also tell you the same, I think.

                                      1 Reply Last reply
                                      2
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #26

                                        Hi

                                        Could you try to add

                                        brightness & rgbLed: :getBrightness() {
                                        return obj; // or what you called it
                                        }

                                        to
                                        class rgbLed : public QObject

                                        then in main.cpp

                                        QObject::connect(window, SIGNAL(submitTextField(QString)), my_rgbLed.getBrightness(), SLOT(brightnessText(QString)));

                                        and remove all
                                        brightness my_brightness;
                                        as we dont want to use brightness alone ever, only via rgbLed.

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved