简体   繁体   中英

How to connect a qml child component signal to a c++ slot

I want to connect a child item component signal to a c++ slot but it is not working. I have one file ButtonItem.qml in which the code is like

Item {

    id: button

    property string label
    property alias cellColor : rectangle.color

    Rectangle {
        id : rectangle
        objectName : "rectangle"
        height : 40
        width : 50
        radius: 10
        color: "gray"

        Text {
            anchors.centerIn: parent
            font.pixelSize: 20
            text: button.label
            color: "white"
        }
    }
}

and the main file is button.qml

Rectangle {
    id : rect
    width: systemWidth
    height: systemHeight.getHeight()

    Text{
        id : text
        objectName : "text"
        height : 20
        width : 10
        anchors.centerIn : parent
        text : systemHeight.getText()
    }
        ButtonItem {
            signal qmlMsg(string msg)
            objectName : "button"
            id : button3
            cellColor : "blue"
            label : "3"

            MouseArea {
                anchors.fill : parent
                onClicked : button3.qmlMsg("Hello World")
            }
        }
}

and in my main source file the code is

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{

    QDeclarativeView *qmlView = new QDeclarativeView;

    qmlView->rootContext()->setContextProperty("systemWidth", 1000);

    Sample sObj;
    qmlView->rootContext()->setContextProperty("systemHeight", &sObj);
    this->setCentralWidget(qmlView);
    qmlView->setSource(QUrl::fromLocalFile("E:/samplecode/qmlsample/button.qml"));
    QObject *obj = qmlView->rootObject();
    QObject *childObj = obj->findChild<QObject *>("button");

    connect(childObj, SIGNAL(qmlMsg(QString)), this, SLOT(printData(QString)));
}


void MainWindow::printData(QString message)
{

    qDebug()<<message;
}

but no slot is getting call. It is working fine if i connect parent signal to c++ slot.

The problem is not in the child's signal/slot, it's the MouseArea which has zero size. Add width and height to the root item in ButtonItem.qml .

Item {   
    id: button
    width: 40  // add this
    height: 40 // add this
    ...    
}

Or you add them directly to button.qml too

ButtonItem {
    signal qmlMsg(string msg)
    objectName : "button"
    id : button3
    cellColor : "blue"
    label : "3"
    width: 40  // add this
    height: 40 // add this
    ...    
}

To find QML child item using QObject::findChild() , it should have a name. So it should be like :

Item {

    id: button
    objectName: "button"

    ...

}

Now you can access it by :

QObject *obj = qmlView->rootObject();
QObject *childObj = obj->findChild<QObject *>("button");
if (childObj)
{
    connect(childObj, SIGNAL(qmlMsg(QString)), this, SLOT(printData(QString)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM