QDeclarativeView problem



  • Hi everyone,

    in main.cpp

    @QDeclarativeView view;

    view.rootContext()->setContextProperty("numFromCPP", QVariant("3"));
    view.setSource(QUrl("/mainPage.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.showFullScreen()@

    the property value is from another thread (another signal will send the value).

    The problem is: I want to set property when the value is available . But setContextProperty is not a slot, i can not connect it to the signal.

    or, do you have some solutions? for example, is there any ways that i can update the loaded qml's property?

    I am really confused with the problem. Thank you very much



  • What you could do, is create a simple QObject class that works as a proxy for the property value. Create a property, and make the setter method a slot while you emit a signal when the value changes (see Q_PROPERTY). Connect the slot to the signal from the other thread, and then expose the QObject to your QML using a new QDeclarativeContext and it's setContextObject method.



  • Hi,

    Thank you very much.

    i have tried your solution. what i can't understand is this sentence

    "expose the QObject to your QML using a new QDeclarativeContext and it’s setContextObject method"

    what i want to change in the mainPage.qml is just a property, not an QObject.

    Here is the code, i dont know how to do it for the next step
    @ Proxy proxy;
    QObject::connect(&parserDirects,SIGNAL(allFinished (int)),&proxy,SLOT(setNumber(int)));

    QDeclarativeView view;
    
    view.rootContext()->setContextProperty("numFromCPP", QVariant("3"));
    view.setSource(QUrl("/home/zhiheng/Projets/LequipeOVI-build-simulator/qml/LequipeOVI/mainPage.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.showFullScreen();@
    

    [quote author="Andre" date="1299243800"]What you could do, is create a simple QObject class that works as a proxy for the property value. Create a property, and make the setter method a slot while you emit a signal when the value changes (see Q_PROPERTY). Connect the slot to the signal from the other thread, and then expose the QObject to your QML using a new QDeclarativeContext and it's setContextObject method.[/quote]



  • [quote author="huluyige" date="1299248555"]what i want to change in the mainPage.qml is just a property, not an QObject.
    [/quote]

    The point I was trying to make, is that you could make a QObject in order to expose your value to the QML. I understand that you currently don't have a QObject, but that is exactly your problem I think. A QObject can have a slot (you said you need one to connect to a signal in your value-providing thread), and it can be exposed to QML if you give it properties. Sounds like the bridge you need to me...



  • Thank you very much.

    Now, I totally understand your point.

    By doing this, I have others problems (i am new to QT):

    1. "A new QDeclarativeContext " why i have to use a NEW QDeclarativeContext.

    2. the method "setContextObject" is to set another object to the Context ? if it is yes, how can i connect the new object to the former objet(apprarently, it's the object of mainPage.qml)

    Thanks

    [quote author="Andre" date="1299250351"][quote author="huluyige" date="1299248555"]what i want to change in the mainPage.qml is just a property, not an QObject.
    [/quote]

    The point I was trying to make, is that you could make a QObject in order to expose your value to the QML. I understand that you currently don't have a QObject, but that is exactly your problem I think. A QObject can have a slot (you said you need one to connect to a signal in your value-providing thread), and it can be exposed to QML if you give it properties. Sounds like the bridge you need to me...[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.