跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. Mobile and Embedded
  4. Nested class in Qt5
Forum Updated to NodeBB v4.3 + New Features

Nested class in Qt5

已排程 已置頂 已鎖定 已移動 Unsolved Mobile and Embedded
26 貼文 5 Posters 5.3k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • 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 離線
    mrjjM 離線
    mrjj
    Lifetime Qt Champion
    寫於 最後由 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 條回覆 最後回覆
    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 離線
      J 離線
      Juancinho_cardecan
      寫於 最後由 編輯
      #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 條回覆 最後回覆
      0
      • mrjjM 離線
        mrjjM 離線
        mrjj
        Lifetime Qt Champion
        寫於 最後由 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 條回覆 最後回覆
        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 離線
          jsulmJ 離線
          jsulm
          Lifetime Qt Champion
          寫於 最後由 編輯
          #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 條回覆 最後回覆
          3
          • JonBJ 離線
            JonBJ 離線
            JonB
            寫於 最後由 編輯
            #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 條回覆 最後回覆
            2
            • mrjjM 離線
              mrjjM 離線
              mrjj
              Lifetime Qt Champion
              寫於 最後由 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 條回覆 最後回覆
              0

              • 登入

              • Login or register to search.
              • 第一個貼文
                最後的貼文
              0
              • 版面
              • 最新
              • 標籤
              • 熱門
              • 使用者
              • 群組
              • 搜尋
              • Get Qt Extensions
              • Unsolved