Hide All function?



  • Hey,
    Is there is a function which hide all widgets on window? All labels, all buttons, all text fields, all LCDNumbers. All this stuff. I wanna clear my window and show only background. Is there is a function to do it or i have to do it myself?



  • I don't think that there is that functionality in Qt, but one idea is to iterate over the "QObjectList":http://doc.qt.nokia.com/latest/qobject.html#children that contains the children of that QWidget and call hide on them.
    This approach will hide the children that are windows too, so maybe someone comes with a better idea.

    Also if you use a QMainWindow you can change the centralWidget.

    LE: tell us what are you trying to achieve, why are you trying to hide everything? This will help everybody give better advices



  • I try to write a game and if player win or lose I wanna add MessageBox with "You win, yeah" or "Oh noes! you lose" and clear all the area.

    Maybe without all objects it will look better than with it?:)

    Or if there is no that function I'll cover the whole screen (with QPaint or something) with black or grey color and the effect will be the same, i think.

    But, the best way is to hide all, i think.



  • well, all these widgets have common parent, haven't they? so you can iterate over widget->children() and set all of them invisible



  • Or just hide the parent widget :) (if the parent is a main window, you can always add a frame to it that will hold all the widgets)



  • if you want to hide all objects use
    @foreach(QWidget obj, findChildren<QWidget>())
    {
    obj->setVisible(false);
    }@

    even it's possible to limit the control to only one type of object for example by using QPushButton instead of QWidget.

    But i don't recommend it for your application.



  • doesn't it better to use QML for creating your game?



  • I would use a "QStackedWidget":http://doc.trolltech.com/4.7/qstackedwidget.html with one page containing the actual UI and an empty page to be shown when your message box is running.



  • [quote author="mohsen" date="1296156799"]if you want to hide all objects use
    @foreach(QWidget obj, findChildren<QWidget>())
    {
    obj->setVisible(false);
    }@

    even it's possible to limit the control to only one type of object for example by using QPushButton instead of QWidget.
    [/quote]

    This works for the hiding part, but gives you problems when the widgets should be shown again. Many widgets contain subwidgets (eg. a list view in a combo box) which are found by findChildren() too and may be erroneously shown when setVisible(true) with the above code.

    If you need such functionality put all the actual UI into a single container widget and hide this one or use a QStackedWidget and switch pages.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.