Call "Fit to page" in python?

In a python plugin, I need a way to simulate a press of “2” (fit to page).

There doesn’t seem to be an action for that, so I’m stuck.

I’m doing an action which: 1) calls show_canvas_only 2) minimizes any windows that are always-on-top 3) calls fit to page (I’m missing this part).

Edit: maybe simulate a keypress with Qt?

Thanks for advice

Tried the following but does not work:

     wi = app.activeWindow()        
     ev = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_2, Qt.NoModifier)
     QApplication.postEvent(wi.qwindow(), ev)

At the very least, there’s a ['toggle_zoom_to_fit', 'Toggle Zoom Fit to Page'] action, which works a bit weirdly but might be good enough for your use case.

I’ll see if I can implement Fit to Page as a native action later.

It seems to work! Thank you :slight_smile:

Well, it doesn’t always work, but a workaround is to call zoom_to_100pct and then toggle_zoom_to_fit, in sequence.

I’ve submitted a merge request implementing actions for Fit to Page, Fit to Page Width, and Fit to Page Height, so that a workaround shouldn’t be necessary as of 5.2, and so that these actions can be accessed through additional means.

That’s awesome Tom, thank you!