How to center word document in QAxWidget on first show

489 Views Asked by At

I have prepared this example:

import sys
from PyQt5.QtWidgets    import (QApplication, QMainWindow)
from PyQt5              import uic
from PyQt5.QAxContainer import QAxWidget



class MainForm(QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)
    
        uic.loadUi(r"scroll_bar_test.ui", self)

        self.axWidget = QAxWidget('Microsoft Web Browser')

         path = 'C:/Users/Egon/Documents/FotoViewer/scroll_bar_test.docx'
    
         self.axWidget.setControl(path)
         self.setCentralWidget(self.axWidget)
    
def main():
    app = QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

If you start the program it will show a word document but the left side is hidden: enter image description here

if i use the arrow keys to the right i see the whole document: enter image description here

and from there on it is centered fine. My Question is how can the word document can be shown centered right at the start?

enter image description here after using QTimer.singleShot(2000, self.axWidget.updateGeometry)

Meanwhile i found out that these lines center the document:

        self.setCentralWidget(self.axWidget)
        QTimer.singleShot(5, lambda: self.axWidget.setControl(path))
        QTimer.singleShot(5, self.axWidget.updateGeometry)

The time for the Qtimer has to be the same in both shots. Both has to be done with a timer otherwise it doesnt work. Unfurtunately the user sees first a very narrow document like the last foto and then a flash and then the right presented document. Additionaly this doesnt happen on other computer. I have tryed with 3 other people and they didnt have this problmem.

0

There are 0 best solutions below