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. Can I find the layout a given widget is a member of?
Forum Updated to NodeBB v4.3 + New Features

Can I find the layout a given widget is a member of?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 4.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.
  • cerrC Offline
    cerrC Offline
    cerr
    wrote on last edited by aha_1980
    #1

    I have a QButton (which has a label that names the actual item) and a QCheckBox to enable/disable the item named (CheckBox only has Enable in it's text string).
    I thought the a solution to extract the name of the item (on click of the checkbox) might be if I add them into a QHBoxLayout. But turns out that layouts don't work as the parent of children. Is there another way, that I can find the layout of a particular item (in this case, I want to find the layout the QCheckBox is a member of)?
    Then I could easily find the button that's in the layout too with QLayout::itemAt() and extract its text. but I'm still missing to find the correct QLayout - any ideas and/or hints?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      why do you care what layout a widget is in? there are plenty of inqury methods to determine what widget is in play:
      sender(),
      parent(),
      findChildren()
      children()

      but if you must get the layout then remember that a widget can only have one active layout and do

      QLayout* layout{dynamic_cast<QWidget*>(parent())->layout()};
      

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      2
      • cerrC Offline
        cerrC Offline
        cerr
        wrote on last edited by
        #3

        I think I have a better idea of how to do this:
        I create my own class that features a
        QButton,a QCheckBox , a QHBoxLayout and a QString to name it (and set the button's caption) and two function pointers, one that's hooked up with the button click and the other one that's hooked up with the checkbox click.

        Is this a good way to solve this?

        JKSHJ 1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          If I understand what you are trying to do, then yes, encapsulating the checkbox and button in a single class makes sense. the layout thing is totally irrelevant.

          So, you only want the button active if the checkbox is checked? is that the gist of what you are trying to do? If so then create the class and implement signals that can be sent from it when the button is pushed. the checkbox doesn't even have to be visible at the broader scope level then.

          I light my way forward with the fires of all the bridges I've burned behind me.

          cerrC 1 Reply Last reply
          2
          • Kent-DorfmanK Kent-Dorfman

            If I understand what you are trying to do, then yes, encapsulating the checkbox and button in a single class makes sense. the layout thing is totally irrelevant.

            So, you only want the button active if the checkbox is checked? is that the gist of what you are trying to do? If so then create the class and implement signals that can be sent from it when the button is pushed. the checkbox doesn't even have to be visible at the broader scope level then.

            cerrC Offline
            cerrC Offline
            cerr
            wrote on last edited by
            #5

            @kent-dorfman said in Can I find the layout a given widget is a member of?:

            If I understand what you are trying to do, then yes, encapsulating the checkbox and button in a single class makes sense. the layout thing is totally irrelevant.

            But that will nicely align the elements horizontally automatically (which is what I want) as I create an instance of my new class

            So, you only want the button active if the checkbox is checked? is that the gist of what you are trying to do?
            Correct

            If so then create the class and implement signals that can be sent from it when the button is pushed. the checkbox doesn't even have to be visible at the broader scope level then.

            Why not? I actually want to store and apply the setting to a different module as well, i.e. I need to know when the checkbox gets selected/cleared too... I'll have to play around a bit but I think I got the gist of how to get it done! :)

            1 Reply Last reply
            0
            • cerrC cerr

              I think I have a better idea of how to do this:
              I create my own class that features a
              QButton,a QCheckBox , a QHBoxLayout and a QString to name it (and set the button's caption) and two function pointers, one that's hooked up with the button click and the other one that's hooked up with the checkbox click.

              Is this a good way to solve this?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @cerr said in Can I find the layout a given widget is a member of?:

              I think I have a better idea of how to do this:
              I create my own class that features a
              QButton,a QCheckBox , a QHBoxLayout and a QString to name it (and set the button's caption) and two function pointers, one that's hooked up with the button click and the other one that's hooked up with the checkbox click.

              Is this a good way to solve this?

              By "function pointer", do you mean "signal"? If so, then yes.

              However, you don't need to store a QHBoxLayout or QString.

              Why not? I actually want to store and apply the setting to a different module as well, i.e. I need to know when the checkbox gets selected/cleared too

              @Kent-Dorfman is saying that your checkbox should be private inside your custom class; it should not be accessible to anyone outside your class. This is the concept of encapsulation in object-oriented programming.

              You can connect signals from the checkbox and button to your own custom signals, and you can add implement your own functions that operate on your inner widgets. You don't need to expose the inner widgets to the outside world.

              MyWidget : public QWidget
              {
                  Q_OBJECT
              
              public:
                  MyWidget(const QString &caption = "Click Me", QWidget *parent = nullptr) : QWidget(parent),
                          checkBox(new QCheckBox("Enabled")),
                          pushButton(new QPushButton(caption))
                  {
                      connect(checkBox, &QCheckBox::toggled,
                              button, &QPushButton::setEnabled);
                      connect(checkBox, &QCheckBox::toggled,
                              this, &MyWidget::checkBoxToggled);
                      connect(button, &PushButton::clicked,
                              this, &MyWidget::buttonClicked);
                              
                      auto layout = new QHBoxLayout;
                      layout->addWidget(checkBox);
                      layout->addWidget(button);
                      this->setLayout(layout);
                  }
                  
                  QString caption() const { return button->text(); }
                  void setCaption(const QString &caption) { button->setText(caption); }
                  
                  bool isChecked() const { return checkBox->isChecked(); }
                  void setChecked(bool ch) { checkBox->setChecked(ch); }
                  
              signals:
                  void checkBoxToggled(bool) const;
                  void buttonClicked() const;
                  
              private:
                  QCheckBox *checkBox;
                  QPushButton *button;    
              };
              

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              4

              • Login

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