Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to update the value of a struct using Q_PROPERTY and stylesheet?
QtWS25 Last Chance

How to update the value of a struct using Q_PROPERTY and stylesheet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 554 Views
  • 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.
  • R Offline
    R Offline
    Rua3n
    wrote on 14 Jul 2023, 02:35 last edited by Rua3n
    #1

    In this example, I'm writting the "stylesheet" on a QTextEdit and applying it to the QMainWindow

    myStruct.a: 10;
    myStruct.b: "hello world";
    

    setStyleSheet(textEdit->toPlainText());

    I tried to follow this question:
    https://forum.qt.io/topic/82325/best-way-to-access-a-cpp-structure-in-qml/6

    But none of the functions is being called setA getA getMyStruct or setMyStruct when the stylesheet is applied.

    What the proper way to update the values of MyStruct using the stylesheet?

    struct MyStruct
    {
        Q_GADGET
        int m_a;
        QString m_b;
    
        Q_PROPERTY(int a READ getA WRITE setA)
        Q_PROPERTY(QString b MEMBER m_b)
    
        void setA(int a) 
        { 
          m_a = a; 
        }
        int getA() const
        { 
            return m_a;
        }
    };
    Q_DECLARE_METATYPE(MyStruct)
    
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        Q_PROPERTY(MyStruct myStruct READ getMyStruct WRITE setMyStruct NOTIFY myStructChanged)
    public:
        MainWindow() : QMainWindow(nullptr), ui(new Ui::MainWindow())
        {
            ui->setupUi(this);
    
            QVBoxLayout* layout = new QVBoxLayout();
            ui->centralWidget->setLayout(layout);
    
            QTextEdit* textEdit = new QTextEdit(this);
            textEdit->setText(R"(
                myStruct.a: 10;
                myStruct.b: "hello world";
            )");
    
            QPushButton* button = new QPushButton("update", this);
            connect(button, &QPushButton::clicked, [=] { setStyleSheet(textEdit->toPlainText()); });
            layout->addWidget(textEdit);
            layout->addWidget(button);
        }    
    
        MyStruct myStruct;
    
        MyStruct getMyStruct() const
        {
            return myStruct;
        }
    
        void setMyStruct(MyStruct val)
        {
            myStruct = val;
            emit myStructChanged();
        }
    
        void changeEvent(QEvent* event)
        {
            if (event->type() == QEvent::StyleChange) {	qDebug() << "style changed"; }
        }
    
    signals:
        void myStructChanged();
    
    private:
        Ui::MainWindow* ui;
    };
    
    P 1 Reply Last reply 14 Jul 2023, 05:44
    0
    • R Rua3n
      14 Jul 2023, 02:35

      In this example, I'm writting the "stylesheet" on a QTextEdit and applying it to the QMainWindow

      myStruct.a: 10;
      myStruct.b: "hello world";
      

      setStyleSheet(textEdit->toPlainText());

      I tried to follow this question:
      https://forum.qt.io/topic/82325/best-way-to-access-a-cpp-structure-in-qml/6

      But none of the functions is being called setA getA getMyStruct or setMyStruct when the stylesheet is applied.

      What the proper way to update the values of MyStruct using the stylesheet?

      struct MyStruct
      {
          Q_GADGET
          int m_a;
          QString m_b;
      
          Q_PROPERTY(int a READ getA WRITE setA)
          Q_PROPERTY(QString b MEMBER m_b)
      
          void setA(int a) 
          { 
            m_a = a; 
          }
          int getA() const
          { 
              return m_a;
          }
      };
      Q_DECLARE_METATYPE(MyStruct)
      
      
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
          Q_PROPERTY(MyStruct myStruct READ getMyStruct WRITE setMyStruct NOTIFY myStructChanged)
      public:
          MainWindow() : QMainWindow(nullptr), ui(new Ui::MainWindow())
          {
              ui->setupUi(this);
      
              QVBoxLayout* layout = new QVBoxLayout();
              ui->centralWidget->setLayout(layout);
      
              QTextEdit* textEdit = new QTextEdit(this);
              textEdit->setText(R"(
                  myStruct.a: 10;
                  myStruct.b: "hello world";
              )");
      
              QPushButton* button = new QPushButton("update", this);
              connect(button, &QPushButton::clicked, [=] { setStyleSheet(textEdit->toPlainText()); });
              layout->addWidget(textEdit);
              layout->addWidget(button);
          }    
      
          MyStruct myStruct;
      
          MyStruct getMyStruct() const
          {
              return myStruct;
          }
      
          void setMyStruct(MyStruct val)
          {
              myStruct = val;
              emit myStructChanged();
          }
      
          void changeEvent(QEvent* event)
          {
              if (event->type() == QEvent::StyleChange) {	qDebug() << "style changed"; }
          }
      
      signals:
          void myStructChanged();
      
      private:
          Ui::MainWindow* ui;
      };
      
      P Online
      P Online
      Pl45m4
      wrote on 14 Jul 2023, 05:44 last edited by
      #2

      @Rua3n

      it's quite old but look here

      • https://forum.qt.io/topic/53620/solved-set-dynamic-property-by-stylesheet

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      R 1 Reply Last reply 14 Jul 2023, 13:47
      0
      • P Pl45m4
        14 Jul 2023, 05:44

        @Rua3n

        it's quite old but look here

        • https://forum.qt.io/topic/53620/solved-set-dynamic-property-by-stylesheet
        R Offline
        R Offline
        Rua3n
        wrote on 14 Jul 2023, 13:47 last edited by Rua3n
        #3

        @Pl45m4 I read it but i still didn't understand what's wrong and how its related to a struct
        The struct contains Q_PROPERTY

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rua3n
          wrote on 15 Jul 2023, 12:49 last edited by
          #4

          I wrote this based on; https://forum.qt.io/topic/82325/best-way-to-access-a-cpp-structure-in-qml/6
          Why the struct is not getting called?

          S 1 Reply Last reply 15 Jul 2023, 16:03
          0
          • R Rua3n
            15 Jul 2023, 12:49

            I wrote this based on; https://forum.qt.io/topic/82325/best-way-to-access-a-cpp-structure-in-qml/6
            Why the struct is not getting called?

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 15 Jul 2023, 16:03 last edited by
            #5

            Hi,

            What is your goal exactly ? A stylesheet and QML are completely unrelated things.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply 15 Jul 2023, 22:39
            0
            • S SGaist
              15 Jul 2023, 16:03

              Hi,

              What is your goal exactly ? A stylesheet and QML are completely unrelated things.

              R Offline
              R Offline
              Rua3n
              wrote on 15 Jul 2023, 22:39 last edited by Rua3n
              #6

              @SGaist update the values of a struct from a stylesheet, is it possible?

              struct MyStruct
              {
                   int b;
              }
              
              class Foo : public QObject
              {
                    Q_OBJECT
                    Q_PROPERTY(int a MEMBER A)
                    Q_PROPERTY(MyStruct myStruct MEMBER myStruct)
              public:
                    Foo();
                    MyStruct myStruct;
                    int a;
              }
              

              With this stylesheet, i can modify the value of a :

              Foo
              {
                  qproperty-a: 10;
              }
              

              But how to modify b?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 17 Jul 2023, 19:47 last edited by
                #7

                I currently don't know if it's possible to use stylesheets to change the value of a gadget from a property.

                However, can you please explain why you want to start altering such complex properties from within a stylesheet ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                5/7

                15 Jul 2023, 16:03

                • Login

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