https://github.com/jaytwolab/qml-bind
QML binding example for personal purpose.
https://github.com/jaytwolab/qml-bind
bind cpp example qml qt qtquick
Last synced: about 2 months ago
JSON representation
QML binding example for personal purpose.
- Host: GitHub
- URL: https://github.com/jaytwolab/qml-bind
- Owner: JayTwoLab
- License: mit
- Created: 2019-07-15T05:02:30.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-03-07T12:26:15.000Z (about 4 years ago)
- Last Synced: 2025-02-01T05:25:50.318Z (4 months ago)
- Topics: bind, cpp, example, qml, qt, qtquick
- Language: C++
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# QML binding example
## Example
- See [bind](bind/) example.
1. set QML properties in C++.
```cpp
int rootObjSize = engine.rootObjects().size();
for ( int ic = 0; ic < rootObjSize ; ic++ )
{
QObject* obj = engine.rootObjects().at( ic );
if ( obj == nullptr )
continue;
// [3] find the root object that has 'objectName'. 'objectName' is 'mainWindow'.
QVariant val = obj->property( "objectName" );
if ( false == val.isNull() &&
val.toString() == QString("mainWindow") )
{
// [4] success to find 'mainWindow'
QObject* mainWindowObj = obj;
mainWindowObj->setProperty( "title", QVariant(QString("HELLO WORLD")) ); // change 'title' of 'mainWindow'
// [5] find child object. which objectName is 'mainText'.
QObject *textObj = mainWindowObj->findChild( "mainText" );
if ( textObj != nullptr )
{
qmlTimer->objText = textObj;// [6] get 'x' from 'mainText'
QVariant valX = textObj->property( "x" );
qDebug() << "x : " << valX;
}
```
2. get C++ properties in QML```cpp
// [1] set context
CppValue* cppValue1 = new CppValue( &app );
engine.rootContext()->setContextProperty( "cppValue1", cppValue1 );
``````cpp
class CppValue : public QObject
{
Q_OBJECT
Q_PROPERTY( QString strCppValue READ getStrCppValue )
public:
explicit CppValue(QObject *parent = nullptr);
QString getStrCppValue();
Q_INVOKABLE QString testCalling(QString param) const;
protected:
QString m_strCppValue;
signals:
public slots:
};
```
```qml
// [2] get property of C++ 'cppValue1'
var tempValue = cppValue1.strCppValue;
console.log( tempValue ); // CppValue : (random number that is made by constructor)
```
3. call C++ function in QML
```qml
Button { // our Button component
id: button
x: 250; y: 12
text: "Push me"
onClicked: {
// [1] call function
var testParam = "COOL";
var tempResult = cppValue1.testCalling( testParam ); // call 'testCalling' of C++ 'cppValue1'
console.log( tempResult ); // tc: COOL, (random number)
```## License
- MIT License