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 call a QProperty binding from a subclass?
Forum Updated to NodeBB v4.3 + New Features

How to call a QProperty binding from a subclass?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 348 Views 1 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.
  • K Offline
    K Offline
    Kattia
    wrote on last edited by
    #1

    How i could call the test binding in this case without calling
    qobject_cast to transform the QWidget back to PushButton?

    qDebug() << listWidget[0]->property("test"); // return invalid
    qDebug() < listWidget[0]->test; // error as test is not a property of a QWIDGET
    qDebug() << qobject_cast<PushButton*>(listWidget[0])->test; // works
    
    class PushButton : public QPushButton
    {
    	Q_OBJECT
    public:
    
        QProperty<QString> test;
        PushButton(QWidget* parent = nullptr) : QPushButton(parent)
        {
           test.setBinding([&]() 
           { 
               QString string = "OK";
               return string;
           });
        }
    }
    
    PushButton* pushButton = new PushButton;
    QList<QWidget*> listWidget { pushButton };
    
    M 1 Reply Last reply
    0
    • K Kattia

      How i could call the test binding in this case without calling
      qobject_cast to transform the QWidget back to PushButton?

      qDebug() << listWidget[0]->property("test"); // return invalid
      qDebug() < listWidget[0]->test; // error as test is not a property of a QWIDGET
      qDebug() << qobject_cast<PushButton*>(listWidget[0])->test; // works
      
      class PushButton : public QPushButton
      {
      	Q_OBJECT
      public:
      
          QProperty<QString> test;
          PushButton(QWidget* parent = nullptr) : QPushButton(parent)
          {
             test.setBinding([&]() 
             { 
                 QString string = "OK";
                 return string;
             });
          }
      }
      
      PushButton* pushButton = new PushButton;
      QList<QWidget*> listWidget { pushButton };
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @Kattia said in How to call a QProperty binding from a subclass?:

      How i could call the test binding in this case without calling
      qobject_cast to transform the QWidget back to PushButton?

      If your list constains only PushButton objects, you can try:

      QList<PushButton*> listWidget { pushButton };

      Otherwise a cast is needed.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kattia
        wrote on last edited by Kattia
        #3

        I'm using setProperty to call functions in subclassed classes to avoid casting the widget/including its classes in other files.

        Example

        class PushButton : public QPushButton
        {
        	Q_OBJECT
                Q_PROPERTY(bool etc MEMBER etc WRITE callMyFunction);
        public:
        
            bool etc; // unused
            void callMyFunction(bool)
             {
                  myFunction();
             }
        }
        

        Where w is a QWidget* but from type PushButton*

        w->setProperty("etc", true); // The etc variable and its value is ignored, i just want it to call a specific function
        

        The other way would be including "pushbutton.h" casting the QWidget to PushButton and
        pushButton->myFunction();

        There's a better way to do this?

        jsulmJ 1 Reply Last reply
        0
        • K Kattia

          I'm using setProperty to call functions in subclassed classes to avoid casting the widget/including its classes in other files.

          Example

          class PushButton : public QPushButton
          {
          	Q_OBJECT
                  Q_PROPERTY(bool etc MEMBER etc WRITE callMyFunction);
          public:
          
              bool etc; // unused
              void callMyFunction(bool)
               {
                    myFunction();
               }
          }
          

          Where w is a QWidget* but from type PushButton*

          w->setProperty("etc", true); // The etc variable and its value is ignored, i just want it to call a specific function
          

          The other way would be including "pushbutton.h" casting the QWidget to PushButton and
          pushButton->myFunction();

          There's a better way to do this?

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

          @Kattia said in How to call a QProperty binding from a subclass?:

          There's a better way to do this?

          No, because there is no virtual function named myFunction anywhere in the class hierarchy. It is defined in PushButton, so you have to cast.

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

          K 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Kattia said in How to call a QProperty binding from a subclass?:

            There's a better way to do this?

            No, because there is no virtual function named myFunction anywhere in the class hierarchy. It is defined in PushButton, so you have to cast.

            K Offline
            K Offline
            Kattia
            wrote on last edited by
            #5

            @jsulm I have found a "better way" using QMetaObject::invokeMethod and i also can get the return value of the function!
            There's any "cons" in calling functions with invokeMethod?

            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