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 check if the parent widget of a certain type?
Forum Updated to NodeBB v4.3 + New Features

How to check if the parent widget of a certain type?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 7 Posters 1.4k 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.
  • A Offline
    A Offline
    abdoemr11
    wrote on 26 Oct 2021, 19:35 last edited by
    #1

    Hi
    Say I have MyWidgetContainer that is subclassed of QWidget

    class MyWidgetContainer: public QWidget
    {
    .....
    }
    

    and I have CustomWidget, this widget will do special behaviour if it is only a child of MyWidgetContainer.

    void CustomWidget::doSpecialBehaviour()
    {
     //if parent widget == MyWidgetContainer
    // do the special behaviour 
    //else 
    //do the normal behaviour 
    }
    

    How can I achieve something like this?

    E J 2 Replies Last reply 26 Oct 2021, 19:42
    0
    • A abdoemr11
      26 Oct 2021, 19:35

      Hi
      Say I have MyWidgetContainer that is subclassed of QWidget

      class MyWidgetContainer: public QWidget
      {
      .....
      }
      

      and I have CustomWidget, this widget will do special behaviour if it is only a child of MyWidgetContainer.

      void CustomWidget::doSpecialBehaviour()
      {
       //if parent widget == MyWidgetContainer
      // do the special behaviour 
      //else 
      //do the normal behaviour 
      }
      

      How can I achieve something like this?

      E Offline
      E Offline
      eyllanesc
      wrote on 26 Oct 2021, 19:42 last edited by
      #2

      @abdoemr11 Use

      if(MyWidgetContainer *w = qobject_cast<MyWidgetContainer *>(parentWidget())){
          // FIXME
      }

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • A abdoemr11
        26 Oct 2021, 19:35

        Hi
        Say I have MyWidgetContainer that is subclassed of QWidget

        class MyWidgetContainer: public QWidget
        {
        .....
        }
        

        and I have CustomWidget, this widget will do special behaviour if it is only a child of MyWidgetContainer.

        void CustomWidget::doSpecialBehaviour()
        {
         //if parent widget == MyWidgetContainer
        // do the special behaviour 
        //else 
        //do the normal behaviour 
        }
        

        How can I achieve something like this?

        J Offline
        J Offline
        JonB
        wrote on 26 Oct 2021, 19:42 last edited by
        #3

        @abdoemr11
        You can do this with casting, but you are not supposed to. It is not considered good practice to write child code to depend on what its parent is, why do you want to design like this?

        J 1 Reply Last reply 26 Oct 2021, 20:18
        3
        • J JonB
          26 Oct 2021, 19:42

          @abdoemr11
          You can do this with casting, but you are not supposed to. It is not considered good practice to write child code to depend on what its parent is, why do you want to design like this?

          J Offline
          J Offline
          JoeCFD
          wrote on 26 Oct 2021, 20:18 last edited by JoeCFD
          #4

          @JonB create a virtual function specialBehaviour in parent class and override it in all children classes.
          do something inside specialBehaviour() of custom widget
          leave it empty in other widget classes.

          J 1 Reply Last reply 27 Oct 2021, 05:28
          0
          • J JoeCFD
            26 Oct 2021, 20:18

            @JonB create a virtual function specialBehaviour in parent class and override it in all children classes.
            do something inside specialBehaviour() of custom widget
            leave it empty in other widget classes.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 27 Oct 2021, 05:28 last edited by
            #5

            @JoeCFD This thread is not about inheritence, but parent <-> child in Qt.

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

            1 Reply Last reply
            1
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 27 Oct 2021, 05:33 last edited by
              #6
              if(parentWidget->metaObject()->className() == "MyWidgetContainer") {
                  // FIXME
              }
              

              But the solution proposed by @eyllanesc is much better - less error prone and compiler will be able to check it more.

              (Z(:^

              P 1 Reply Last reply 28 Oct 2021, 17:14
              0
              • S sierdzio
                27 Oct 2021, 05:33
                if(parentWidget->metaObject()->className() == "MyWidgetContainer") {
                    // FIXME
                }
                

                But the solution proposed by @eyllanesc is much better - less error prone and compiler will be able to check it more.

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 28 Oct 2021, 17:14 last edited by
                #7

                But the solution proposed by @eyllanesc is much better - less error prone and compiler will be able to check it more.

                As some other replies have mentioned, the casting although technically feasible (you have some code snippets already) might not be a good design approach.
                Let's say in a month you want the same custom behavior when the container widget is MyUncleContainer, not only MyParentContainer...

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1

                1/7

                26 Oct 2021, 19:35

                • Login

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