我尝试学习PySide-QML连接并尝试创建一个简单的应用程序,如添加。如:a + b = c。如何连接按钮来计算两个输入? 感谢
编辑:我的代码算法:当按下按钮时,它只是键入10.当在窗口中的某个位置单击时,它会键入" hello man"翻译。
Python代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
class MainWindow(QDeclarativeView):
now=Signal(str)
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.setWindowTitle("Main Widnowww")
self.setSource(QUrl.fromLocalFile('view.qml'))
self.setResizeMode(QDeclarativeView.SizeRootObjectToView)
#self.rootObject().messageRequired.connect(self.echo)
self.rootObject().messageRequired.connect(self.emit_now)
self.rootObject().clicked.connect(self.printThat)
self.now.connect(self.rootObject().updateMessage)
def printThat(self):
print "hello man"
#print str(bt)
def emit_now(self):
a=6
b=4
c=a+b
#formatted_date = v.toString()
formatted_date = str(c)
self.now.emit(formatted_date)
def echo(self,a_text,b_text):
an=float(a_text or 0)
bn=float(b_text or 0)
ce=an + bn
c=str(ce)
self.rootObject().updateAnswer(str(c))
if __name__ == '__main__':
app=QApplication(sys.argv)
window=MainWindow()
window.show()
sys.exit(app.exec_())
QML代码:
import QtQuick 1.1
Rectangle {
id: rectangle1
width: 480
height: 272
gradient: Gradient {
GradientStop {
id: gradientStop1
position: 0
color: "#ffffff"
}
GradientStop {
position: 1
color: "#abc09f"
}
}
function updateScoreA(string ) {
score_a.text = string
}
function updateScoreB(string ) {
score_b.text = string
}
Text {
id: score_a
x: 45
y: 8
width: 131
height: 48
text: qsTr("Text")
verticalAlignment: Text.AlignVCenter
font.pixelSize: 12
}
Text {
id: score_b
x: 303
y: 8
width: 131
height: 48
text: qsTr("Text")
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignRight
font.pixelSize: 12
}
Text {
id: dash
x: 232
y: 5
text: qsTr("-")
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 47
}
MouseArea {
id: a_scored
x: 303
y: 200
}
Rectangle {
id: team_a
x: 45
y: 218
width: 127
height: 46
color: "#4e3a3a"
radius: 10
TextInput {
id: team_a_txt
x: 24
y: 13
width: 80
height: 20
text: qsTr("A")
horizontalAlignment: TextInput.AlignHCenter
font.pixelSize: 12
}
MouseArea {
id: team_a_score_ma
x: 0
y: 0
width: 126
height: 46
onClicked: {
qScoreInterface.aScored()
}
}
}
Rectangle {
id: team_b
x: 307
y: 218
width: 127
height: 46
color: "#4e3a3a"
radius: 10
TextInput {
id: team_b_txt
x: 24
y: 13
width: 80
height: 20
text: qsTr("B")
horizontalAlignment: TextInput.AlignHCenter
font.pixelSize: 12
}
MouseArea {
id: team_b_score_ma
x: 0
y: 0
width: 126
height: 46
onClicked: {
qScoreInterface.bScored()
}
}
}
Rectangle {
id: startstopgame
x: 177
y: 113
width: 127
height: 46
color: "#4e3a3a"
radius: 10
TextInput {
id: startstopgame_txt
x: 24
y: 13
width: 80
height: 20
text: qsTr("Start Game")
horizontalAlignment: TextInput.AlignHCenter
font.pixelSize: 12
}
MouseArea {
id: startstopgame_ma
x: 1
y: 0
width: 126
height: 46
onClicked: {
qScoreInterface.startMatch()
}
}
}
}