Skip to content
  • 0 Votes
    2 Posts
    115 Views
    oblivioncthO

    Well... if you want it done right do it yourself XD

    Obviously, Qt has it's pedigree and Qt Bindable Properties are impressive in their own right, and I would never claim to be better than a team of veteran professionals; but, in this one case this (and a few other) quirk(s) of Qt Bindable Properties ended up bothering me enough that I decided to go nuts and take a stab at them myself. Although my system doesn't have quite the same level of polish as Qt's, I'm very satisfied with how they turned out.

    So, if it happens that any one else:

    Doesn't need QML integration (i.e. C++ properties only) Doesn't need to access properties abstractly through the meta-object system

    and

    Wants their binding evaluations to only ever run once per-update and never read stale values, such that the evaluation of the above example is always: 1) pStruct 2) pEmpty 3) pHello

    with almost the same API, feel free to partake in my experiment :)

    Qx Bindable Properties

    There's an easier to follow example in the documentation for this above under "Advantages".

  • 0 Votes
    4 Posts
    566 Views
    R

    Thanks @VRonin and @KroMignon for your insights!

  • 0 Votes
    2 Posts
    735 Views
    KroMignonK

    @Kyeiv said in ListView model from Q_PROPERTY problems:

    What am i doing wrong that the view is empty?

    The problem is that QList<QString>() is not a valid type for a QML model!

    QML model can be QList<QObject*>() or QStringList() (cf https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html).

    Change your Q_PROPERTY from QList<QString> to QStringList and it will work as expected.

  • 0 Votes
    8 Posts
    2k Views
    KroMignonK

    @HenkKalkwater said in QML: QMetaProperty::read unable to handle for some enums defined in C++, but not all:

    Anyways, a huge thank you for your help. I´ve been stuck on this for about 2 days.

    Your welcome, I am glad I could help you :)

  • 0 Votes
    1 Posts
    441 Views
    No one has replied
  • 0 Votes
    6 Posts
    4k Views
    Q

    @Ronak5 no, I didn't. that's probably not the problem any current test is trying to share a bool with QML and the problem is the same. but I'll remember to try it when sharing a string :)

    So here is a simpler project and use a Singleton. One of the problem was that I have to declare my object in the QML when using qmlRegisterType. And I don't want to. So here is the new code, the error is the same.
    I followed the example of the doc here https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html

    miniModel.h (the singleton)

    #ifndef _MINIMODEL_H_ #define _MINIMODEL_H_ #include <stdlib.h> #include <stdio.h> #include <iostream> #include <string> #include <QtGui/qguiapplication.h> #include <QtQml/qqmlcontext.h> #include <QtQml/qqmlapplicationengine.h> #include <QtCore/qdebug.h> #include <QtCore/qobject.h> #include <QtCore/qvariant.h> class MiniModel : public QObject { Q_OBJECT Q_PROPERTY(bool miniboule READ miniboule WRITE setMiniboule NOTIFY minibouleChanged) public: MiniModel(); bool miniboule(); void setMiniboule(bool bouboule); signals: void minibouleChanged(); private: bool m_miniboule; }; #endif

    main.cpp v1 : singleton using a QObject

    #include "miniModel.h" //defining a miniModel instance as a singleton static QObject* mp_singleton(QQmlEngine* engine, QJSEngine* scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) MiniModel* miniSingleton = new MiniModel(); return miniSingleton; } int main(int argc=0, char* argv[]=nullptr) { printf("\n launching \n"); QGuiApplication app(argc, argv); qmlRegisterSingletonType<MiniModel>("myModel.miniModel", 1, 0, "MiniModel",mp_singleton); QQmlApplicationEngine engine; engine.addImportPath(QStringLiteral("..\\..\\..\\..\\Tools\\Qt\\5.12.0\\x64\\5.12.0\\msvc2017_64\\qml")); engine.load(QUrl(QStringLiteral("..\\..\\..\\miniModel.qml"))); return app.exec(); }

    main.cpp v2 : singletin using QJSValue

    #include "miniModel.h" static QJSValue m_singletonModel(QQmlEngine* engine, QJSEngine* scriptEngine) { Q_UNUSED(engine) static bool m_miniboule; QJSValue miniModel = scriptEngine->newObject(); miniModel.setProperty("miniboule", m_miniboule); return miniModel; } int main(int argc = 0, char* argv[] = nullptr) { printf("\n launching \n"); QGuiApplication app(argc, argv); qmlRegisterSingletonType("myModel.miniModel", 1, 0, "MiniModel", m_singletonModel); QQmlApplicationEngine engine; engine.addImportPath(QStringLiteral("..\\..\\..\\..\\Tools\\Qt\\5.12.0\\x64\\5.12.0\\msvc2017_64\\qml")); engine.load(QUrl(QStringLiteral("..\\..\\..\\miniModel.qml"))); return app.exec(); }

    and the QML. be careful, it's tough

    import QtQuick 2.5 import QtQuick.Window 2.5 import QtQuick.Controls 1.4 import myModel.miniModel 1.0 as MyModel ApplicationWindow { id: root width: 300 height: 480 visible:true Text{ id: textTest x: 62 y: 75 color: "#d21616" text: "vanilla" visible: false//the text is supposed to appear when clicking in the mouseArea } MouseArea{ anchors.fill: parent onClicked: textTest.visible= MyModel.Minimodel.miniboule//the boolean I want to acess, defined to true } }

    now, the error changed, since I called MyModel.MiniModel.miniboule instead of just MiniModel.miniboule
    the error is TypeError: Cannot read property 'miniboule' of undefined

  • 0 Votes
    4 Posts
    2k Views
    SGaistS

    ClassB, you are using it in a property.

    By the way, why not use QObjectList and a Q_INVOKABLE method ? Might be more straightforward.

    More information on how to integrate CPP with QML here.

  • 0 Votes
    5 Posts
    2k Views
    Gianluca86G

    I would have preferred a quicker way using Q_PROPERTY, but I think the only way is what you've suggested to me.
    Thanks so much

  • 0 Votes
    5 Posts
    2k Views
    mongrelmacM

    I see. Thanks @raven-worx @SGaist!

  • 0 Votes
    1 Posts
    854 Views
    No one has replied
  • 0 Votes
    5 Posts
    8k Views
    kshegunovK

    @mcleary

    When one declares a property with MEMBER feature the QMetaProperty is reporting that the property is writable.

    This is quite strange indeed.

    I solve my problem removing the MEMBER declaration!

    This is certainly a possibility. Still I'm glad it's working for you,

  • 0 Votes
    8 Posts
    3k Views
    ArieA

    Any other idea why the WRITE function is not called at the widget’s pseudo state change when the stylesheet has different qproperty values defined for the states?

  • 0 Votes
    3 Posts
    14k Views
    SGaistS

    Hi,

    Q_INVOKABLE is used to make functions accessible throughout Qt meta object system. Q_PROPERTY is much more complex and offers additional features

  • 0 Votes
    6 Posts
    4k Views
    jsulmJ

    You don't have to mix anything.
    What you use in your enum are hexadecimal values not binary.
    That means (left side hex, right side binary): 0x0 == 0, 0x1 == 1, 0x10 == 10000, 0x100 == 100000000!
    I guess what you actually want is what Chris Kawa said: 0x01 == 1, 0x02 == 10, 0x04 == 100, 0x08 == 1000, 0x10 == 10000

  • 0 Votes
    2 Posts
    13k Views
    M

    After scouring the web, it appears that the only way to do this is the very complex technique of subclassing the widget. It's easier to just put another button on it, but then I had the problem where I couldn't pass a hover event to the QPushButton underneath it. This lead me to learn the technique on StackOverflow of building an eventFilter.

  • 0 Votes
    3 Posts
    1k Views
    L

    Thanks! I fix the problem. All works fine, but I have a Rectangle over that hide the window background and that was the problem.

  • 0 Votes
    1 Posts
    762 Views
    No one has replied
  • 0 Votes
    4 Posts
    1k Views
    p3c0P

    @vishnu Works on Ubuntu 14.04 with Qt 5.4.1
    Edit: Works on Windows 7 64 bit with Qt 5.4.1 and MSVC 2010 too.
    Try searching https://bugreports.qt.io for possible bugs if any.