我过去几个小时一直在阅读Qt文档,试图找出一种使用Qt Quick UI (QML)创建的UI与C++代码(函数等)进行通信(交互)的方法。
我已经阅读了这里的5到6个类似问题,但我有些困惑,不知道从哪里开始或者首先要做什么。如果有人能花时间列出所需步骤使其工作,我将不胜感激。
到目前为止,我尝试了... >添加新项目> C++类,但是失败了,并显示错误消息:"无法将一个或多个文件添加到项目中"。看起来文件(.cpp和.h)已经被创建,它们在其他项目文件所在的文件夹中,但未包含在项目中。我想做的只是像通过C++函数或任何其他可能的方式更改textedit的文本这样简单的事情。
//Test.qml (main.qml)
感谢您抽出时间阅读本文和/或任何回复。
我已经阅读了这里的5到6个类似问题,但我有些困惑,不知道从哪里开始或者首先要做什么。如果有人能花时间列出所需步骤使其工作,我将不胜感激。
到目前为止,我尝试了... >添加新项目> C++类,但是失败了,并显示错误消息:"无法将一个或多个文件添加到项目中"。看起来文件(.cpp和.h)已经被创建,它们在其他项目文件所在的文件夹中,但未包含在项目中。我想做的只是像通过C++函数或任何其他可能的方式更改textedit的文本这样简单的事情。
//Test.qml (main.qml)
import QtQuick 2.1
import QtQuick.Window 2.0
Rectangle {
id: rootRect
width: Screen.width/2
height: Screen.height/2
color: "gray"
Button{}
Rectangle{
id: textField
width: 120
height: 40
color: "white"
x:274; y: 61
border.color: "blue"
border.width: 4
radius: 2
}
TextEdit {
id: display
x: 274
y: 61
width: 80
height: 20
text: qsTr("Text Edit")
font.pixelSize: 22
color: "black"
anchors.centerIn: textField
}
Rectangle{
id: inputField
width: textField.width
height: textField.height
border.color: "green"
border.width: 3
color: "white"
x: 140; y: 61
}
TextEdit{
id: input
color: "red"
font.pixelSize: 30
anchors.centerIn: inputField
text: "Some Text"
}
}
//Button.cpl
import QtQuick 2.0
import QtQuick.Window 2.0
Item {
property string defaultText: "New Text"
Rectangle{
id: button
width: rootRect.width/6
height: rootRect.height/8
color: "black"
x: 200; y: 200
radius: 10
}
MouseArea{
id: buttonClickArea
width: 0
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.fill: button
onClicked: {
display.text = defaultText
}
}
}
感谢您抽出时间阅读本文和/或任何回复。
QtQuick.Controls 1.3
中已经有了Button
项目。 - folibis新建文件或项目...>Qt Quick应用程序
。在那里,您可以使用QML和C++类。您在其他答案中找到的代码可以复制到这些源文件中。 - BaCaRoZzo