[SOLVED] Get qml signals



  • Hi
    I have a problem to connect to my qml signals. I have a qdeclarative view that have main.qml as source. I use a qdeclarativeitem and qdeclarativecomponent to add new qml items to the declarative view. My qml signals is in the components that are added to the declarativeview. Does the rootobject get signals from my added qml item? Are there a way for the added qml item to pass the signals to rootobject?

    This is my set up
    QDeclarative view
    -Main.qml

    QDeclarativeItem is added to view->rootobject
    QDeclarativeItem
    -testHelper.qml (Its here where the signals are)

    Main.qml
    @Rectangle {
    id: container
    width: 800
    height: 600
    color: "white"
    }@

    Qml file for item where the signals are.
    @
    import QtQuick 1.1

    Rectangle {
    id: rect
    objectName: "Button"
    signal newPositionSignal(string position)
    signal deleteSignal(string objectName)
    signal selectedSignal(string objectName)

    onNewPositionSignal: {
    console.log('newPositionSignal: ' + position);
    }

    onDeleteSignal: {
    console.log('deleteSignal: object "' + objectName + '"" deleted');
    }
    onSelectedSignal: {
    console.log('selectedSignal: object "'+ rect.objectName + '"" selected');
    content.border.color = "blue"
    }

    Rectangle {
    id: content

    width: childrenRect.width
    height: childrenRect.height
    border.color: "black"
    border.width: 2
    color: "transparent"

    Component.onCompleted: {
    var component = Qt.createComponent("../Indicators/TankLevel.qml");
    if (component.status === Component.Ready) {
    var button = component.createObject(content);
    }
    }

    MouseArea {
    id: contentMouseArea
    anchors.fill: parent
    drag.target: parent
    onClicked: {
    rect.selectedSignal(rect.objectName);
    }
    onReleased: {
    rect.newPositionSignal(rect.objectName+','+ parent.x+','+ parent.y+','+ parent.z);
    }
    }
    }

    Rectangle {
    id: remove
    width: 20
    height: 20
    color: "black"
    anchors.right: content.right
    anchors.top: content.top

    Text {
    anchors.centerIn: parent
    text: "X"
    color: "white"
    }

    MouseArea {
    anchors.fill: parent
    onClicked: {
    rect.deleteSignal(rect.objectName);
    rect.destroy();
    }
    }
    }
    }
    @

    Setting up declarativeview
    @
    qmlPath = path;
    view = new QDeclarativeView(this);
    view->setSource(QUrl::fromLocalFile(path));

    view->setResizeMode(QDeclarativeView::SizeViewToRootObject);

    viewerManager vManager(cMan);
    vManager.setRootObject(view->rootObject());
    QObject::connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
    

    @

    Function for inserting item to the view
    @
    QDeclarativeComponent testcomponent(view->engine());
    testcomponent.loadUrl(QUrl::fromLocalFile("testHelper.qml"));
    QDeclarativeItem* testcomp = qobject_cast<QDeclarativeItem *>(testcomponent.create());
    testcomp->setParent(view->rootObject());
    testcomp->setParentItem(qobject_cast<QDeclarativeItem *>(view->rootObject()));
    QDeclarativeEngine::setObjectOwnership(testcomp, QDeclarativeEngine::JavaScriptOwnership);
    @

    Class that handles qml signals
    @
    viewerManager::viewerManager(QObject *parent) :
    QObject(parent), mRoot(0)
    {
    }

    viewerManager::viewerManager(componentManager *component, QObject *parent) :
    QObject(parent), mRoot(0)
    {
    mComponent = component;
    }

    void viewerManager::setRootObject(QObject *root){
    if (mRoot != 0) disconnect(mRoot);

    mRoot = root;
    
    if(mRoot){
        connect(root, SIGNAL(newPositionSignal(QString)), this, SLOT(newPositionSignal(QString)));
    }
    

    }
    void viewerManager::newPositionSignal(QString pos){
    qWarning() << "Got signal";
    }
    @



  • I solved the issue, I connected the signals in when I added the declarativecomponent.

    @
    QDeclarativeComponent testcomponent(view->engine());
    testcomponent.loadUrl(QUrl::fromLocalFile("testHelper.qml"));
    QDeclarativeItem* testcomp = qobject_cast<QDeclarativeItem *>
    (testcomponent.create());
    //Added all signals here
    QObject::connect(testcomp,SIGNAL(test()),this,SLOT(test()));

    testcomp->setParent(view->rootObject());
    testcomp->setParentItem(qobject_cast<QDeclarativeItem *>(view->rootObject()));
    QDeclarativeEngine::setObjectOwnership(testcomp, QDeclarativeEngine::JavaScriptOwnership);
    @


Log in to reply
 

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