Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Get qml signals
QtWS25 Last Chance

[SOLVED] Get qml signals

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaf84
    wrote on last edited by
    #1

    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";
    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chaf84
      wrote on last edited by
      #2

      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);
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved