Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Parent in PyQt5 with QML

Parent in PyQt5 with QML

Scheduled Pinned Locked Moved Solved Language Bindings
qmlpyqt5
2 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliReza Beytari
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved