Parent in PyQt5 with QML
Solved
Language Bindings
-
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?!
-
Hello,
Well it's says there in the question. Don't pass aQWindow
to theQFileDialog::getOpenFileName
function. A window is not a widget and it expectsQWidget
. You could just use NULL (or whatever its equivalent in python is).Kind regards.