调用C++;来自QML代码的函数 我想从我的QML代码中调用C++函数。
例如,在下面的代码中,我有一个包含两个输入的窗口:数量和价格 我想调用一个C++函数来评估小计,并给它加上5%的税。 我尝试过搜索很多地方,但无法使用最新版本的QT5获得完整的工作代码。 请告诉我如何从QML中调用C++函数。 main.qml:调用C++;来自QML代码的函数 我想从我的QML代码中调用C++函数。,c++,qml,qt5,C++,Qml,Qt5,例如,在下面的代码中,我有一个包含两个输入的窗口:数量和价格 我想调用一个C++函数来评估小计,并给它加上5%的税。 我尝试过搜索很多地方,但无法使用最新版本的QT5获得完整的工作代码。 请告诉我如何从QML中调用C++函数。 main.qml: import QtQuick 2.2 import QtQuick.Controls 1.1 ApplicationWindow { visible: true width: 640 height: 480 title
import QtQuick 2.2
import QtQuick.Controls 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Column{
Label {
text: qsTr("Enter the number of items purchased: ")
}
TextField {
id: in1
objectName: "in1"
}
Label {
text: qsTr("Enter the price per item ($):")
}
TextField {
id: in2
objectName: "in2"
}
Button {
id: button
objectName: "button"
text: "Compute"
onClicked: {
total.text = "Final bill, including 5% tax, is $" + clickedButton(in1.text, in2.text); // here i'm calling the c++ function
}
}
Label {
id: total
objectName: "total"
text: "Final bill, including 5% tax, is $____"
}
}
}
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
double clickedButton(int number, int price){
const double TAX_rate = 0.05;
double subtotal;
subtotal = price*number;
return (subtotal + subtotal*TAX_rate);
}
#包括
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
qqmlaplicationengine;
发动机负荷(QUrl)(QStringletral(“qrc:///main.qml")));
返回app.exec();
}
双击按钮(整数编号、整数价格){
常数双倍税率=0.05;
双倍小计;
小计=价格*数量;
报税表(小计+小计*税率);
}
只需使用:
engine.rootContext()->setContextProperty("yourName", new yourClass());
在qml中,您可以使用yourname.yourfunction()
另外,在您的类中,您必须使函数
Q\u INVOKABEL
您需要声明clickedButton为Q\u可调用,如下所示:
public:
Q_INVOKABLE void cppMethod(const QString &msg) {
qDebug() << "Called the C++ method with" << msg;
}
公共:
Q_可调用的void cppMethod(const QString&msg){
qDebug()创建一个类,如:
class BillCalculator : public QObject
{
Q_OBJECT
Q_PROPERTY(double totalPrice READ totalPrice WRITE setTotalPrice NOTIFY totalPriceChanged)
public:
BillCalculator(QObject *parent = 0) :
QObject(parent),
mTotalPrice(0.0)
{
}
double totalPrice() const { return mTotalPrice; }
signals:
void totalPriceChanged();
public slots:
void setTotalPrice(const double &arg)
{
if(mTotalPrice != arg)
{
mTotalPrice = arg;
emit totalPriceChanged();
}
}
void calculateTotalPrice(int number, int price)
{
const double TAX_rate = 0.05;
double subtotal;
subtotal = price*number;
setTotalPrice(subtotal + subtotal*TAX_rate);
}
protected:
double mTotalPrice;
};
在main.cpp中,包括
,并按如下所示进行修改
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("billCalculator", new BillCalculator);
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
修改main.qml文件,如下所示
Button {
id: button
objectName: "button"
text: "Compute"
onClicked: {
billCalculator.calculateTotalPrice(parseInt(in1.text), parseInt(in2.text));
}
}
Label {
id: total
objectName: "total"
text: "Final bill, including 5% tax, is $" + (billCalculator.totalPrice > 0 ? billCalculator.totalPrice.toFixed(2) : "____")
}
我想您忘了在类中添加mTotalPrice声明。但是当我添加声明时,它给出了错误:-1:错误:找不到架构x86_64的符号-:-1:错误:链接器命令失败,退出代码为1(使用-v查看调用)是的,你是对的,修复了它;对于你的链接器错误,创建一个单独的billcalculator.h并在那里定义billcalculator类