Congratulations to our 2022 Qt Champions!

Parent in PyQt5 with QML

  • I'm using PyQt5 with QML. This is my code:

    import sys
    from PyQt5.QtCore import QObject, QUrl, Qt
    from PyQt5.QtWidgets import QApplication, QFileDialog
    from PyQt5.QtQml import QQmlApplicationEngine
    if __name__ == "__main__":
        def myBtnClicked():
            fileDialog = QFileDialog.getOpenFileName(win, 'Open file', '/home')
        app = QApplication(sys.argv)
        engine = QQmlApplicationEngine()
        ctx = engine.rootContext()
        ctx.setContextProperty("main", engine)
        win = engine.rootObjects()[0]    
        myBtn= win.findChild(QObject, "myBtn")

    But when I click on "myBtn", I got this error:
    builtins.TypeError: QFileDialog.getOpenFileName(QWidget parent=None, str caption='', str directory='', str filter='', str initialFilter='', QFileDialog.Options options=0) -> (str, str): argument 1 has unexpected type 'QWindow'

    What is the solution?!

  • Moderators

    Well it's says there in the question. Don't pass a QWindow to the QFileDialog::getOpenFileName function. A window is not a widget and it expects QWidget. You could just use NULL (or whatever its equivalent in python is).

    Kind regards.

Log in to reply