Skip to content
  • 0 Votes
    7 Posts
    1k Views
    K

    @raven-worx i cannot specify Layout.alignment in string creating Text because i get error that this property doesn't exist

  • 0 Votes
    2 Posts
    542 Views
    rrlopezR

    Hi @Kyeiv, I just did a quick test application and it worked for me:

    main.qml

    import QtQuick 2.9 import QtQuick.Window 2.2 import "someJSscriptFile.js" as Jss Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Rectangle { id: rect color: Jss.getCurrentColor() anchors.fill: parent } }

    someJSScriptFile.js

    function getCurrentColor() { return __getColor(rect.currentValue) } function __getColor(value) { if (rect.pressed) { return "grey" } if(rect.activated === false) { return "white" } if(value ===0) { return "red"; } return "green"; }

    Is the variable "control" defined in your QML? Or what are you trying to use it for?

  • 0 Votes
    6 Posts
    2k Views
    devDawgD

    @KillerSmath Its working! NOTIFY did the trick. The final touch that I also included was to use a Q_INVOKABLE getModuleAt(i) instead of a Q_INVOKABLE getTextBodyAt(i); this allowed me direct access to the DataModule's property:

    Text{ text: dataModel.getModuleAt(index).TextBody }

    portion of datamodule.h:

    class DataModule: public QObject { Q_OBJECT Q_PROPERTY(QString TextBody MEMBER textBody NOTIFY textBodyChanged) . . . signals: void newVal(DataPiece*); void textBodyChanged(QString newText);

    Q_INVOKABLE method inside of the QML context class datamodel.h:

    Q_INVOKABLE DataModule *getModuleAt(int i);

    Brilliant! Thanks for the help mate. Hopefully this is helpful to others as well.

  • 0 Votes
    1 Posts
    930 Views
    No one has replied
  • 0 Votes
    2 Posts
    2k Views
    Marco PellinM

    In the end everything was fine with the example above.
    I just had to update and release my translation files, because in the meantime I changed the name of a .qml file, and the filename is directly linked into the .ts files created by linguist and used by Qt.
    I feel a bit ashamed but yes, it was really trivial and kinda stupid :)

  • 0 Votes
    2 Posts
    2k Views
    P

    I eventually found a solution/workaround to the problem. I am posting it here if anybody ever needs it.

    The main issue it seems is wanting to filter the C++ model in QML/JavaScript. Getting elements out of the C++ model into QML seems to be possible as a returning a QVariantMap maps to a JS object in the QML engine. Using this returned object as a model proved not to work directly. This might work with further effort but due to time constraints this approach was abandoned.

    The new approach was to create a C++ model derived from QAbstractItemModel and reimplementing the necessary functions. This model is essentially a filtering proxy for Model_A and Model_B.

    The filtering is implemented in QAbstractItemModel::getData() and the elements are exposed through a reimplementation of QAbstractItemModel::roleNames().

    For this filtering proxy to change state when the underlying data changes the dataChanged() signals of the underlying models was connected to a method in the proxy model computing the correct index invoking emit dataChanged(index).

    The QML delegate reuse was then implemented as follows:

    Rectangle { // some rendering of Model_A Repeater { delegate: Delegate_B{} model: Item{ property var propertyDelegateB: model.propertyDelegateB // mulitple properties possible } } }

    This worked for my use case as I needed only one element in the model. For multiple elements this approach might not be ideal.