Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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)
    
        engine.load('test.qml')
    
        win = engine.rootObjects()[0]    
    
        myBtn= win.findChild(QObject, "myBtn")
        myBtn.clicked.connect(myBtnClicked)
    
        win.show()
        sys.exit(app.exec_())
    

    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

    Hello,
    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