Pyside: Qwidget expose in QML with qmlRegisterType
-
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 Locationclass 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.0Rectangle {
//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
NicolaPS: I was copying from this "site":http://kunalmaemo.blogspot.com/2011/07/how-to-display-qwidget-into-qml.html
-
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
-
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