How to Call Function After Window Is Shown

Take action after the main form is shown in a Qt desktop application

You can override showEvent() of the window and call the function you want to be called with a single shot timer :

void MyWidget::showEvent(QShowEvent *)
{
QTimer::singleShot(50, this, SLOT(doWork());
}

This way when the windows is about to be shown, showEvent is triggered and the doWork slot would be called within a small time after it is shown.

You can also override the eventFilter in your widget and check for QEvent::Show event :

bool MyWidget::eventFilter(QObject * obj, QEvent * event)
{
if(obj == this && event->type() == QEvent::Show)
{
QTimer::singleShot(50, this, SLOT(doWork());
}

return false;
}

When using event filter approach, you should also install the event filter in the constructor by:

this->installEventFilter(this);

Execute function after clicking a button in a new window

All you need to do is change your onclick listener:

onclick="window.opener.done();"

Learn more about window.opener

How to call a function of another window in javascript?

var a = window.open("mypage.html", "_blank");
a.focus();

a.addEventListener('load', function(){
a.myfunction("hi");
}, true);

PyQt5 Run function after displaying window

Time-consuming tasks are blocking, and this goes against the natural way of working on the GUI, an option is to use qApp.processEvents(), for example:

def function(self):
self.guiBox.setValue(initData)
code1
QtWidgets.qApp.processEvents()
code2
QtWidgets.qApp.processEvents()
...

How do I call a JavaScript function on page load?

If you want the onload method to take parameters, you can do something similar to this:

window.onload = function() {
yourFunction(param1, param2);
};

This binds onload to an anonymous function, that when invoked, will run your desired function, with whatever parameters you give it. And, of course, you can run more than one function from inside the anonymous function.



Related Topics



Leave a reply



Submit