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. Pyside: Qwidget expose in QML with qmlRegisterType
Forum Updated to NodeBB v4.3 + New Features

Pyside: Qwidget expose in QML with qmlRegisterType

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 5.3k 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.
  • U Offline
    U Offline
    ultimodruido
    wrote on last edited by
    #1

    Hello everybody!
    i was trying to use the qmlRegisterType function in order to see how does t work.
    And as always, I'm such a good programmer that it didn't work :)

    here is the main python file:

    @#!/usr/bin/python

    -- coding: utf-8 --

    import sys
    from PySide import QtCore
    from PySide import QtGui
    from PySide import QtDeclarative
    #from QtMobility import Location

    class QMLButton(QtDeclarative.QDeclarativeItem):
    def init(self, parent = 0):
    super(QMLButton, self).init()
    self.my_button = QtGui.QPushButton("Settings")
    self.my_button.clicked.connect(self.my_btn_clicked)
    self.my_proxy = QtGui.QGraphicsScene().addWidget(self.my_button)
    #print dir (self.my_proxy)
    #self.my_proxy.show()

    def my_btn_clicked(self):
        print "done"
    

    class MainWindow(QtDeclarative.QDeclarativeView):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("test QML")
    
        self.register = QtDeclarative.qmlRegisterType(QMLButton, "MyPython", 1, 0, "MyButton")
    
        # Renders 'view.qml'
        self.setSource(QtCore.QUrl.fromLocalFile('qml/maemodrive/MD_qml.qml'))
        # QML resizes to main window
        self.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
    

    if name == 'main':
    # Create the Qt Application
    app = QtGui.QApplication(sys.argv)
    # Create and show the main window
    window = MainWindow()
    window.showMaximized()
    # Run the main Qt loop
    sys.exit(app.exec_())@

    and here the qml file:
    @import QtQuick 1.0
    import MyPython 1.0

    Rectangle {

    //anchors.fill: parent
    color: "lightblue"
    width: 800
    height: 480
    
    MyButton {
        //show: true
        id: forse
        height: 300
        width: 150
        x:100
        y: 100
        anchors.centerIn: parent
    }
    
    Image {
        id: info
        source: "icons/ui/btn_min.png"
        anchors.bottom: parent.bottom
        anchors.left: parent.left
    }
    

    }@

    The program runs it shows the qml (light blue rectangle and the image in the lower left corner) but there is no button :)

    I did discover that, if I add the show() line to the QMLbutton class:
    @class QMLButton(QtDeclarative.QDeclarativeItem):
    def init(self, parent = 0):
    super(QMLButton, self).init()
    self.my_button = QtGui.QPushButton("Settings")
    self.my_button.clicked.connect(self.my_btn_clicked)
    self.my_proxy = QtGui.QGraphicsScene().addWidget(self.my_button)
    self.my_proxy.show()@

    I get this error:
    @ File "./maemodriveQML01.py", line 17, in init
    self.my_proxy.show()
    RuntimeError: Internal C++ object (PySide.QtGui.QGraphicsProxyWidget) already deleted.@
    So it seems tha I did loose the reference in python and the element has been deleted? But the instance should be generated from the qml file... how do I save the instance? I tried giving an Id the MyButton but no success.
    Thank you very much for the help
    Nicola

    PS: I was copying from this "site":http://kunalmaemo.blogspot.com/2011/07/how-to-display-qwidget-into-qml.html

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on last edited by
      #2

      I think this a error on pyside:

      try change your code for something like:

      @
      class QMLButton(QtDeclarative.QDeclarativeItem):
      def init(self, parent = 0):
      super(QMLButton, self).init()
      self.my_button = QtGui.QPushButton("Settings")
      self.my_button.clicked.connect(self.my_btn_clicked)
      scene = QtGui.QGraphicsScene()
      self.my_proxy = scene.addWidget(self.my_button)
      self.my_proxy.show()
      @

      This is related with pyside bug: http://bugs.pyside.org/show_bug.cgi?id=915

      1 Reply Last reply
      0
      • U Offline
        U Offline
        ultimodruido
        wrote on last edited by
        #3

        Hi Renato!
        Thanks for the tip! It did solve the issue with the RuntimeError, but unfortunately I still don't see the button in the QML scene :(
        If I discover how to make it work I will post it...
        ciao
        nic

        1 Reply Last reply
        0

        • Login

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