Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c++ and qt

85 views
Skip to first unread message

bubu

unread,
Jan 16, 2024, 3:53:16 PMJan 16
to
Hi,

Sorry for my bad english and sorry for my bad level in qt.

I would like to use a qml program and c++ libraries (with import).

I use Visual Studio on windows.

I have several problems (and I put an example here-after) :
- how to use c++ with qml
- how to use a library written in c++ to use in a program in QML with
IMPORT statement.
- how to build executable and librarie with cmake?

My example is here :

main.qml

ApplicationWindow {
visible: true
width: 400
height: 300
title: "Calculator"

Calculatrice {
id: calculatrice
}

Column {
anchors.centerIn: parent
spacing: 10

TextField {
id: input1
placeholderText: "Entrez le premier nombre"
validator: DoubleValidator {
bottom: -1000000000.0
top: 1000000000.0
}
}

TextField {
id: input2
placeholderText: "Entrez le deuxième nombre"
validator: DoubleValidator {
bottom: -1000000000.0
top: 1000000000.0
}
}

Row {
spacing: 10

Button {
text: "Additionner"
onClicked: {
resultLabel.text = "Résultat: " +
calculator.add(parseFloat(input1.text), parseFloat(input2.text))
}
}

Button {
text: "Soustraire"
onClicked: {
resultLabel.text = "Résultat: " +
calculator.subtract(parseFloat(input1.text), parseFloat(input2.text))
}
}

Button {
text: "Multiplier"
onClicked: {
resultLabel.text = "Résultat: " +
calculator.multiply(parseFloat(input1.text), parseFloat(input2.text))
}
}
}

Label {
id: resultLabel
text: "Résultat: "
}
}
}




main.cpp

#include <QGuiApplication>#include <QQmlApplicationEngine>#include
<QtCore>//#include "calculator.cpp"int main(int argc, char* argv[]){
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv); QQmlApplicationEngine engine;
//qmlRegisterType<Calculator>("Calculator", 1, 0, "Calculator"); const
QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&amp;engine, &amp;QQmlApplicationEngine::objectCreated,
&amp;app, [url](QObject* obj, const QUrl&amp; objUrl) { if
(!obj &amp;&amp; url == objUrl) QCoreApplication::exit(-1);
}, Qt::QueuedConnection); engine.load(url); return
app.exec();}



calculator.h


// calculator.h#ifndef CALCULATOR_H#define CALCULATOR_Hclass Calculator
{public: double add(double a, double b) const; double subtract(double a,
double b) const; double multiply(double a, double b) const;};#endif //
CALCULATOR_H


calculator.cpp

#include <QObject>class Calculator : public QObject{ Q_OBJECTpublic:
Q_INVOKABLE double add(double a, double b) const { return a + b;
} Q_INVOKABLE double subtract(double a, double b) const { return
a - b; } Q_INVOKABLE double multiply(double a, double b) const {
return a * b; }};



Could you help me, please.
It make errors on building. Why ?
How to build library for qml (using IMPORT) and executable with cmake ?

Sorry. I have a lot of problems. I will have others questions after.

Thanks a lot.



b

unread,
Jan 16, 2024, 4:25:33 PMJan 16
to
Hi,

Sorry for my bad english and sorry for my bad level in qt.

I would like to use a qml program and c++ libraries (with import).

I use Visual Studio on windows.

I have several problems (and I put an example here-after) :
- how to use c++ with qml
- how to use a library written in c++ to use in a program in QML with
IMPORT statement.
- how to build executable and librarie with cmake?

My example is here :

main.qml

import QtQuick 2.15
import QtQuick.Controls 2.15
// import calculator ? ? ? how ?
#include "calculator.cpp"

int main(int argc, char* argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;

qmlRegisterType<Calculator>("Calculator", 1, 0, "Calculator");

QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject* obj, const QUrl& objUrl) {
if (!obj && url == objUrl)

bubu

unread,
Jan 16, 2024, 4:26:24 PMJan 16
to

bubu

unread,
Jan 16, 2024, 4:30:09 PMJan 16
to
Hi,

Sorry for my bad english and sorry for my bad level in qt.

I would like to use a qml program and c++ libraries (with import).

I use Visual Studio on windows.
I want to use QT5 (not qt5)

wij

unread,
Jan 20, 2024, 5:22:41 PMJan 20
to

Check out this site. They are willingly to answer questions about Qt.
ttps://www.qtcentre.org/content/

0 new messages