How to hide QToolButton from a layout
-
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 -
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); }
-
@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?
-
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); }
-
@Chris-Kawa It worked wonderfully! thank you very much!