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 hide QToolButton from a layout
Forum Updated to NodeBB v4.3 + New Features

How to hide QToolButton from a layout

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • F Offline
    F Offline
    fire121
    wrote on 8 May 2022, 10:49 last edited by fire121 5 Aug 2022, 11:23
    #1

    Re: How to hide a QPushButton in a QToolBar?

    Hi,
    I have read the post that I'm replying to but didn't get the answers I'm looking for there, So I'm trying my luck here in the new post.

    I have a different situation, I'm adding a couple of QToolButtons (not QPushButtons) to a layout (Not to a toolbar) of a frame inside a widget.
    After I added all the buttons, I have some logical situation, in which I would like to hide one of the previously added buttons.
    Inside the c++ code of the widget which holds the frame (that holds all the buttons) I have overridden the ShowEvent method, in which I check the logical condition that I have interest in,
    and when it's not filled, I make QToolButtonObj->Hide() (the button I created previously).
    But the button is still displayed,
    I even tried adding repaint() after QToolButtonObj->Hide(), without any luck.
    I cant understand why the button is not getting hidden and how to fix it (without rewriting the whole widgets code to change it into a toolbar).

    By the way,
    I have placed BP in the showEvent method, and I'm sure it's being called.
    Secondly, I checked for QToolButtonObj->isHidden() and got True.
    But as said above, the button is still shown.

    Thank you in advance,
    Arie

    1 Reply Last reply
    1
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 8 May 2022, 15:51 last edited by Chris Kawa 5 Aug 2022, 15:55
      #4

      Index of a child is not the same as index in a layout, so you're hiding the wrong widget. Also you're doing a lot (a lot!) of unnecessary work, like tons of string conversions and copying items (calling children() creates a new collection every time).

      QToolButton* btn = trgts_frame->findChild<QToolButton*>(SOME TARGET);
      if (btn)
      {
         btn->setVisible(SOME CONDITION TO SHOW BUTTON);
      }
      
      F 1 Reply Last reply 9 May 2022, 09:29
      2
      • M Offline
        M Offline
        mpergand
        wrote on 8 May 2022, 11:24 last edited by
        #2

        Seems you have a different instance, check the address of your button in the constructor and in the showEvent.

        F 1 Reply Last reply 8 May 2022, 13:20
        0
        • M mpergand
          8 May 2022, 11:24

          Seems you have a different instance, check the address of your button in the constructor and in the showEvent.

          F Offline
          F Offline
          fire121
          wrote on 8 May 2022, 13:20 last edited by
          #3

          @mpergand Thank you for the quick response.
          it's strange because I already checked what you suggested and have seen the addresses are the same.
          I managed to solve this with the following workaround:

          int i = 0;
          	for (i = 0; i < trgts_frame->children().count(); ++i) {
          		if (0 == strcmp(trgts_frame->children()[i]->objectName().toStdString().c_str(),
          						target2String(SOME TARGET).c_str())) {
          			break;
          		}
          	}
          
          	 if (true == SOME CONDITION TO SHOW BUTTON) {
          		 trgts_frame->layout()->itemAt(i - 1)->widget()->show();
          	 }
          	 else {
          		 trgts_frame->layout()->itemAt(i - 1)->widget()->hide();
          	 }
          

          Is it looking ok? is there prettier way to achieve the goal?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 8 May 2022, 15:51 last edited by Chris Kawa 5 Aug 2022, 15:55
            #4

            Index of a child is not the same as index in a layout, so you're hiding the wrong widget. Also you're doing a lot (a lot!) of unnecessary work, like tons of string conversions and copying items (calling children() creates a new collection every time).

            QToolButton* btn = trgts_frame->findChild<QToolButton*>(SOME TARGET);
            if (btn)
            {
               btn->setVisible(SOME CONDITION TO SHOW BUTTON);
            }
            
            F 1 Reply Last reply 9 May 2022, 09:29
            2
            • C Chris Kawa
              8 May 2022, 15:51

              Index of a child is not the same as index in a layout, so you're hiding the wrong widget. Also you're doing a lot (a lot!) of unnecessary work, like tons of string conversions and copying items (calling children() creates a new collection every time).

              QToolButton* btn = trgts_frame->findChild<QToolButton*>(SOME TARGET);
              if (btn)
              {
                 btn->setVisible(SOME CONDITION TO SHOW BUTTON);
              }
              
              F Offline
              F Offline
              fire121
              wrote on 9 May 2022, 09:29 last edited by
              #5

              @Chris-Kawa It worked wonderfully! thank you very much!

              1 Reply Last reply
              0

              1/5

              8 May 2022, 10:49

              • Login

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