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 Update on Monday, May 27th 2025

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
  • 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 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?

    eyllanescE JonBJ 2 Replies Last reply
    0
    • A abdoemr11

      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?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on 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

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        JoeCFDJ 1 Reply Last reply
        3
        • JonBJ JonB

          @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?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on 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.

          jsulmJ 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on 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(:^

              Pablo J. RoginaP 1 Reply Last reply
              0
              • sierdzioS sierdzio
                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.

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on 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

                • Login

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