Unsolved QML/C++ application
-
Good afternoon.
Now my application is written in MainWindow + C ++.
I plan to rewrite the design on QML. And as is known with ++ and QML works through Q_INVOKABLE / Q_PROPERTY
So: now I have the calculation functions, the values of variables are taken from doublespinbox
How to properly implement this on QML / C ++?const int size= 5; // 5 вариантов double p_overpressure[size]= // p изб { ui->doubleSpinBox_p_overpressure_1->value(), ui->doubleSpinBox_p_overpressure_2->value(), ui->doubleSpinBox_p_overpressure_3->value(), ui->doubleSpinBox_p_overpressure_4->value(), ui->doubleSpinBox_p_overpressure_5->value() };
double calc_V_gas(double G, double V_accumulated, // V н double p_overpressure, // p изб double p_saturation, // p нас double p_0) // (8,9) { if(p_overpressure>p_saturation) return 0; return G*V_accumulated * p_0/(p_overpressure+p_0)* ( 1-0.5*(p_overpressure/p_saturation+ std::sqrt( 2*p_overpressure/p_saturation - std::pow(p_overpressure/p_saturation,2))) ); }
-
I don't really get what you really want...
The only point with QML is to send the values in the correct format and use them in.
There is more than one way to send it but what i prefer, personally as a rookie, is to instantiate objects in main and send it to be used in QML :
main.cpp
QQmlApplicationEngine engine; Reader reader; ReadWrite readWrite; CardInformation card; engine.rootContext()->setContextProperty("reader", &reader); engine.rootContext()->setContextProperty("readWrite", &readWrite); engine.rootContext()->setContextProperty("card", &card);
You can now call your object reader, readWrite or card in QML.
As a Q_INVOKABLE :.h class ReadWrite : public QObject { Q_OBJECT public: explicit ReadWrite(QObject *parent = nullptr); Q_INVOKABLE QString readChip(); [...] } .cpp QString ReadWrite::readChip() { QString myVar = "nasdrovia"; return myVar; } .qml Text { text: readWrite.readChip() }
Or as a Q_PROPERTY :
.h class Reader : public QObject { Q_OBJECT Q_PROPERTY ( int myInteger \ READ getMyInteger \ WRITE setMyInteger \ NOTIFY myIntegerChanged ) public: int getMyInteger(); void setMyInteger(int newInteger); signals: myIntegerChanged(); private: int m_myInteger; } .cpp int Reader::getMyInteger { return m_myInteger; } void Reader::setMyInteger(int newInteger) { m_myInteger = newInteger; emit myIntegerChanged(); } .qml Rectangle { id: myRectangle //Should be able to detect if it must use READ or WRITE property of the Q_PROPERTY --> here, Read height: reader.myInteger width: 15 MouseArea { //Here, Write onClicked: reader.myInteger = myRectangle.width } }
Then, the format you'll use for it is up to you, take a look at this : Data types conversion between C++ and QML
and this : QML basic types
Does it answer your question a bit ?