Important: Please read the Qt Code of Conduct -

Called function when QWidget gets displayed?

  • Hi,

    I would like to know how I can implements some action whenever a QWidgets gets shown. For example, I have a container QWidget which contains some other QWidgets and some subclassed buttons. These buttons need to do something whenever they are displayed. (At least, the first time, they are displayed()).

    I thought that I can reimplement the show()-function since I call show() on the container widget. However, it seems that show() doesn't call show on its members like I thought it will.

    So which function can I reimplement to add some actions whenever a subclassed widget gets displayed (after being hidden or when viewed the first time.)


  • Qt Champions 2017

    When the parent widget is shown, child windows(QWidgets and subclassed buttons) are shown. You can somehow make those classes to emit signals. I'm thinking of sending some events manually immediately after calling the show. Since all these are your classes, you can implement the event handlers Or you can make some signals and connect to slots.

    e.g - This shows the all the inside buttons as well.

    All this makes button1 to emit the pressed signals.

  • Mhh,

    but there is no way to determin if show() is called on an parent widget without manually passing through signals (or normal functions)?

    I could just reimplement the show()-function to emit signals but I have to forward the signal to the buttons through all the other widgets which wouldn't realy be a nice solution. ;)
    (Well, I would have to connect everything)

  • Lifetime Qt Champion


    Reimplementing show() would not help since it's not a virtual function. You need to implement showEvent.

    Or maybe use an eventFilter

    Hope it helps

  • Qt Champions 2017

    Currently there is no way to know that widget has been shown. It is guaranteed that widgets and children are shown, when the parent widget show is called. We have to do external mechanism do this. You need to some how make your widgets to emit the signal. This is only through you making them to generate signals or events. Based on this, I suggested you to fire the event. show(...) function is not virtual. It will not help there.

Log in to reply