[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: [PyQt] Instantiate python qobject from qml
From: Fabian Sturm <f () rtfs ! org>
Date: 2015-01-29 14:59:50
Message-ID: 20150129155950.45686jrcmx07yscg () xenon ! rtfs ! org
[Download RAW message or body]
This message is in MIME format.
Hello,
I still haven't really wrapped my head around qml and python qobjetcs
and a model view separation. Here is what I want to do. E.g. I want to
write an application to handle a list of contacts. The contacts are
loaded from file into a python qobject. I also have a qml file to
display a contact and a button "next" to show the next contact.
What I achieved so far is to write a python qobject which is imported
and instantiated in the qml file that is loaded on demand. I can then
also bind Labels to the qobject properties and call slots in the
qobject for the prev and next button.
Next step would be to implement a real new and open menu entry. It
should open a file dialog and instantiate a new qml/qobject whenever
the user selects a new file.
Am I on the right track with this or is it meant to be used completely
different?
Thanks,
Fabian
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
["contacts.qml" (text/x-java)]
import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.2
import Contacts 1.0
TabView {
id: tabView
anchors.fill: parent
/*
anchors.margins: UI.margin
tabPosition: UI.tabPosition
Layout.minimumWidth: 360
Layout.minimumHeight: 360
Layout.preferredWidth: 800
Layout.preferredHeight: 600
*/
Contacts {
id: contacts
}
Tab {
title: "Contacts"
GridLayout {
id: gridLayout
rows: 2
flow: GridLayout.TopToBottom
anchors.fill: parent
Label { text: "Title" }
Label { text: "Type" }
TextField {
}
Text {
id: helloworld
text: contacts.name
}
Button {
text: 'previous'
onClicked: {
contacts.prev_contact()
}
}
Button {
text: 'next'
onClicked: {
contacts.next_contact()
}
}
}
}
Tab {
title: "Other"
}
}
["contacts.py" (text/x-python)]
import sys
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
# This is the type that will be registered with QML. It must be a
# sub-class of QObject.
class Contacts(QObject):
def __init__(self, parent=None):
super().__init__(parent)
# Initialise the value of the properties.
self._name = 'Not set'
# All signals
nameChanged = pyqtSignal()
# Define the getter of the 'name' property. The C++ type of the
# property is QString which Python will convert to and from a string.
@pyqtProperty('QString', notify=nameChanged)
def name(self):
return self._name
# Define the setter of the 'name' property.
@name.setter
def name(self, name):
print('name changed')
if self._name != name:
self._name = name
self.nameChanged.emit()
@pyqtSlot()
def prev_contact(self):
print('handle prev contact')
self.name = 'prev name'
@pyqtSlot()
def next_contact(self):
print('handle next contact')
self.name = 'next name'
["main.qml" (text/x-java)]
import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.2
ApplicationWindow {
id: mainWindow
title: "Contacts program"
width: 600; height: 300
color: "gray"
menuBar: MenuBar {
Menu {
title: "&Contacts"
MenuItem {
text: "&New"
onTriggered: {
pageLoader.source = "contacts.qml"
}
}
MenuItem {
text: "E&xit"
shortcut: StandardKey.Quit
onTriggered: Qt.quit()
}
}
}
Loader {
anchors.fill: parent
id: pageLoader
}
}
["main.py" (text/x-python)]
import os
import sys
from PyQt5 import QtCore, QtQml, QtQuick, QtWidgets
from contacts import Contacts
# FIXME: clean this up
osname = os.name.lower()
sysplatform = sys.platform.lower()
windows = osname == "nt" and sysplatform.startswith("win")
QtQml.qmlRegisterType(Contacts, 'Contacts', 1, 0, 'Contacts')
app = QtWidgets.QApplication(sys.argv)
# Create the QML engine
engine = QtQml.QQmlEngine(app)
engine.quit.connect(app.quit)
component = QtQml.QQmlComponent(engine)
currentFilePath = os.path.dirname(os.path.abspath(__file__))
mainFilepath = os.path.join(currentFilePath, "main.qml")
if windows:
mainFilepath = mainFilepath.replace('\\', '/')
qmlFile = QtCore.QUrl("file:///" + mainFilepath)
component.loadUrl(qmlFile)
topLevelItem = component.create()
topLevelItem.show()
sys.exit(app.exec_())
[Attachment #7 (text/plain)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic