如何将qml子组件信号连接到c++;狭槽 < >我想把子项组件信号连接到C++插槽,但它不工作。我有一个文件 ButtonItem.qml中的代码如下 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" } } }

如何将qml子组件信号连接到c++;狭槽 < >我想把子项组件信号连接到C++插槽,但它不工作。我有一个文件 ButtonItem.qml中的代码如下 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" } } },c++,qt,qml,C++,Qt,Qml,主文件是 按钮。qml Rectangle { id : rect width: systemWidth height: systemHeight.getHeight() Text{ id : text objectName : "text" height : 20 width : 10 anchors.centerIn : parent text : systemH

主文件是 按钮。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")
            }
        }
}
在我的主源文件中,代码是

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;
}
MainWindow::MainWindow(QWidget*父项):
QMainWindow(父窗口)
{
QDeclarativeView*qmlView=新的QDeclarativeView;
qmlView->rootContext()->setContextProperty(“systemWidth”,1000);
样本sObj;
qmlView->rootContext()->setContextProperty(“系统高度”、&sObj);
此->设置中心Widget(qmlView);
qmlView->setSource(QUrl::fromLocalFile(“E:/samplecode/qmlsample/button.qml”);
QObject*obj=qmlView->rootObject();
QObject*childObj=obj->findChild(“按钮”);
连接(childObj,信号(qmlMsg(QString)),此,插槽(printData(QString));
}
void主窗口::打印数据(QString消息)
{

qDebug()问题不在孩子的信号/插槽中,而是大小为零的MouseArea。将宽度高度添加到按钮项中的根项。qml

Item {   
    id: button
    width: 40  // add this
    height: 40 // add this
    ...    
}
或者您也可以将它们直接添加到按钮.qml

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

要使用
QObject::findChild()
查找QML子项,它应该有一个名称。因此它应该类似于:

Item {

    id: button
    objectName: "button"

    ...

}
现在,您可以通过以下方式访问它:

QObject *obj = qmlView->rootObject();
QObject *childObj = obj->findChild<QObject *>("button");
if (childObj)
{
    connect(childObj, SIGNAL(qmlMsg(QString)), this, SLOT(printData(QString)));
}
QObject*obj=qmlView->rootObject();
QObject*childObj=obj->findChild(“按钮”);
if(childObj)
{
连接(childObj,信号(qmlMsg(QString)),此,插槽(printData(QString));
}