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 last edited by fire121
    #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
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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
      2
      • M Offline
        M Offline
        mpergand
        wrote on 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
        0
        • M mpergand

          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 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
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #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
            2
            • Chris KawaC Chris Kawa

              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 last edited by
              #5

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

              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