python_QML调用基础
This commit is contained in:
parent
da7f446301
commit
001b56683b
20 changed files with 324 additions and 0 deletions
11
README.md
11
README.md
|
@ -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/)
|
||||
|
||||
|
|
|
@ -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 |
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
'''
|
||||
(1)QML显式的调用Python函数
|
||||
|
||||
定义一个类,并继承QtCore.QObject对象,并使用@修饰符修饰pyqtSlot
|
||||
|
||||
创建rootContext对象,并使用setContextProperty(string, 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_()
|
|
@ -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") //QML显式的调用Python函数
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
'''
|
||||
(2)QML显式的调用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_()
|
|
@ -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) //QML显式的调用Python函数
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 53 KiB |
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
'''
|
||||
(3)QML连接信号到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_()
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,29 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
(4)Python调用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_()
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -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_())
|
|
@ -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"
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in a new issue