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?!


  • Qt Champions 2016

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.