python_QML调用基础

This commit is contained in:
Lin JH 2018-04-16 23:17:02 +08:00
parent da7f446301
commit 001b56683b
20 changed files with 324 additions and 0 deletions

View file

@ -54,9 +54,20 @@
- [4.8 折叠动画效果](partner_625781186/2.折叠控件/)
- [4.9 水波纹进度条](界面美化/水波纹进度条)
### [5.QML](partner_625781186/QML_QtQuick_PY)
- [python_QML调用基础](partner_625781186\QML_QtQuick_PY\python_QML调用基础)
- QWidget窗体中嵌入qml界面
- [QDialog中嵌入qml窗体并缩放](partner_625781186\QML_QtQuick_PY\QDialog中嵌入qml窗体并缩放)
- [QQmlApplicationEngine之qml嵌入qtwidget_qt5.8以上](partner_625781186\QML_QtQuick_PY\QQmlApplicationEngine之qml嵌入qtwidget_qt5.8以上)
# QQ群
- [PyQt & PySide](https://jq.qq.com/?_wv=1027&k=50LWvn9)
- [PyQt学习互助](https://jq.qq.com/?_wv=1027&k=5QVVEdF)
- [PyQt5小组](https://jq.qq.com/?_wv=1027&k=5cI3oRz)
# [Donate-打赏](Donate/)

View file

@ -0,0 +1,3 @@
http://blog.csdn.net/eric6_17/article/details/71622951
http://www.cnblogs.com/hhh5460/p/4237863.html

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
'''
1QML显式的调用Python函数
定义一个类并继承QtCore.QObject对象并使用@修饰符修饰pyqtSlot
创建rootContext对象并使用setContextPropertystring, object注册对象
这样在QML中就可以调用这个函数了
这个例子运行后如果点击鼠标的话会在控制台打印字符串
'''
from PyQt5.QtCore import QUrl, QObject, pyqtSlot
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
class MyClass(QObject):
@pyqtSlot(str) # 输入参数为str类型
def outputString(self, string):
print(string)
if __name__ == '__main__':
app = QGuiApplication([])
path = 'test.qml' # 加载的QML文件
con = MyClass()
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
context = view.rootContext()
context.setContextProperty("con", con)
view.show()
app.exec_()

View file

@ -0,0 +1,19 @@
import QtQuick 2.0
Rectangle {
width: 320; height: 240
color: "lightblue"
Text {
id: txt
text: "Clicked me"
font.pixelSize: 20
anchors.centerIn: parent
}
MouseArea {
id: mouse_area
anchors.fill: parent //
onClicked: {
con.outputString("Hello, Python3") //QMLPython
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View file

@ -0,0 +1,33 @@
#!/usr/bin/env python
'''
2QML显式的调用Python函数并有返回
这个例子跟上一个相类似只是这次调用Python的函数具有返回值功能
运行程序后点击鼠标左上角会显示数字30
'''
from PyQt5.QtCore import QUrl, QObject, pyqtSlot
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
class MyClass(QObject):
@pyqtSlot(int, result=str) # 声明为槽输入参数为int类型返回值为str类型
def returnValue(self, value):
return str(value+10)
if __name__ == '__main__':
path = 'test2.qml' # 加载的QML文件
app = QGuiApplication([])
con = MyClass()
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
context = view.rootContext()
context.setContextProperty("con", con)
view.show()
app.exec_()

View file

@ -0,0 +1,26 @@
import QtQuick 2.0
Rectangle {
id: root
width: 320; height: 240
color: "lightgray"
Text {
id: txt
text: "Clicked me"
font.pixelSize: 20
anchors.centerIn: parent
}
Text {
id: txt1
text: "..."
font.pixelSize: 20
}
MouseArea {
id: mouse_area
anchors.fill: parent //
onClicked: {
console.log("test...") //
txt1.text = con.returnValue(20) //QMLPython
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View file

@ -0,0 +1,31 @@
#!/usr/bin/env python
'''
3QML连接信号到Python
当QML触发事件的时候发射一个信号给Python此时Python调用一个函数
先在QML中定义一个信号
然后在捕获事件的时候发射信号
最后Python中创建一个rootObject对象然后连接这个对象
这个例子中当点击鼠标的时候控制台会打印信息
'''
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
def outputString(string):
print(string)
if __name__ == '__main__':
path = 'test3.qml' # 加载的QML文件
app = QGuiApplication([])
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
view.show()
context = view.rootObject()
context.sendClicked.connect(outputString) # 连接QML信号sendCLicked
app.exec_()

View file

@ -0,0 +1,22 @@
import QtQuick 2.0
Rectangle {
id: root
width: 320; height: 240
color: "lightgray"
signal sendClicked(string str) //
Text {
id: txt
text: "Clicked me"
font.pixelSize: 20
anchors.centerIn: parent
}
MouseArea {
id: mouse_area
anchors.fill: parent //
onClicked: {
root.sendClicked("Hello, Python3")//Python
}
}
}

View file

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
'''
4Python调用QML函数
QML中创建一个函数
Python中创建一个rootObject对象并连接这个函数
例子中每隔1s指针会旋转45 deg;
'''
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
if __name__ == '__main__':
path = 'test4.qml' # 加载的QML文件
app = QGuiApplication([])
view = QQuickView()
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
view.show()
timer = QTimer()
timer.start(2000)
root = view.rootObject()
timer.timeout.connect(root.updateRotater) # 调用QML函数
app.exec_()

View file

@ -0,0 +1,24 @@
import QtQuick 2.0
Rectangle {
id: page
width: 500; height: 200
color: "lightgray"
function updateRotater() {//
rotater.angle += 5
}
Rectangle {
id: rotater
property real angle : 0
x: 240; y: 95
width: 100; height: 5
color: "black"
transform: Rotation {
origin.x: 10; origin.y: 5
angle: rotater.angle
}
}
}

View file

@ -0,0 +1,63 @@
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QRectF, Qt, QUrl
from PyQt5.QtGui import QColor, QGuiApplication, QPainter, QPen
from PyQt5.QtQml import qmlRegisterType
from PyQt5.QtQuick import QQuickPaintedItem, QQuickView
class PieChart(QQuickPaintedItem):
chartCleared = pyqtSignal() # 定义信号
@pyqtProperty(str)
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@pyqtProperty(QColor)
def color(self):
return self._color
@color.setter
def color(self, color):
self._color = QColor(color)
def __init__(self, parent=None):
super(PieChart, self).__init__(parent)
self._name = ''
self._color = QColor()
def paint(self, painter):
painter.setPen(QPen(self._color, 2))
painter.setRenderHints(QPainter.Antialiasing, True)
rect = QRectF(0, 0, self.width(), self.height()).adjusted(1, 1, -1, -1)
painter.drawPie(rect, 90 * 16, 290 * 16)
@pyqtSlot()
def clearChart(self):
self.color = QColor(Qt.transparent)
self.update()
self.chartCleared.emit() # 发射信号
if __name__ == '__main__':
import os
import sys
app = QGuiApplication(sys.argv)
qmlRegisterType(PieChart, "Charts", 1, 0, "PieChart")
view = QQuickView()
view.setResizeMode(QQuickView.SizeRootObjectToView)
view.setSource(
QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__), 'test5.qml')))
view.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,25 @@
import Charts 1.0
import QtQuick 2.0
Item {
width: 300; height: 200
PieChart {
id: aPieChart
anchors.centerIn: parent
width: 100; height: 100
color: "red"
onChartCleared: console.log("The chart has been cleared") //?
}
MouseArea {
anchors.fill: parent
onClicked: aPieChart.clearChart()
}
Text {
anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 }
text: "Click anywhere to clear the chart"
}
}