【问题标题】:QML signal to C++ Slot qt4.8QML 信号到 C++ Slot qt4.8
【发布时间】:2013-01-23 18:38:53
【问题描述】:

我有一个 qt 快速项目,我试图从 QML 发出信号并连接到 C++ 中的插槽。

QML 文件

import QtQuick 1.0
//import com.net 1.0

Column {
id: mainCol
x: 20; y: 80
width: 320
//height: 62
opacity: 0

//NetCom{ id: netcom}


Rectangle{
    signal tcpMsg(string address, int port)
    objectName: "topRec"
    id: topRec
    width: 40; height: 40
    color: "blue"
    Text{
        text: "send tcp msg"; font.pointSize: 14
        anchors.verticalCenter: topRec.verticalCenter
        anchors.left: topRec.right
        anchors.rightMargin: 10
    }
    Text{
        id: statusTxt
        //text: cppInterface.qml_text ; font.pointSize: 12
        text: "status: "; font.pointSize: 12
        anchors.horizontalCenter: topRec.horizontalCenter
        anchors.top: topRec.bottom
        anchors.bottomMargin: 10
    }

    MouseArea {
        id: mouseArea
        transformOrigin: Item.Center
        anchors.fill: parent //anchor all sides of the mouse area to the rectangle's anchors
        onClicked: {
            topRec.tcpMsg("127.0.0.1", 8888)
            console.log("clicked")
        }
    }
}


states:[
   State {
        name: "visible";
        PropertyChanges { target:mainCol; opacity: 1;}
    }
]

transitions:[
  Transition {
        from:""; to: "visible"; reversible: true
        NumberAnimation { properties: "opacity"; duration: 500; easing.type: Easing.InOutQuad }
    }
]

}

C++ 文件

#include <QtGui/QApplication>
#include <QtDeclarative>

#include "qmlcppmediator.h"
#include "qmlapplicationviewer.h"
#include "kernelcom.h"
#include "netcom.h"


Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
//needed to use C++ code in QML
qmlRegisterType<KernelCom>("com.kernel", 1, 0, "KernelCom");
qmlRegisterType<NetCom>("com.net", 1, 0, "NetCom");


QmlApplicationViewer viewer;
viewer.setWindowFlags(Qt::FramelessWindowHint);
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/testMenu/main.qml"));

//make signal from Qml file trigger slots in C++
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("qml/testMenu/TCPmenu.qml"));
QObject *object = view.rootObject();
QObject *child = object->findChild<QObject*>("topRec");

qDebug()<<child;
NetCom client;

qDebug()<<QObject::connect(child,SIGNAL(tcpMsg(QString, quint16)), &client, SLOT(start(QString, quint16)));


viewer.showExpanded();

return app->exec();
}

我不断收到以下错误消息

 QDeclarativeRectangle_QML_3(0x99c5998, name = "topRec") 
 Object::connect: No such signal QDeclarativeRectangle_QML_3::tcpMsg         (QString, quint16)
 Object::connect:  (sender name:   'topRec')
 false 

我尝试使用 QDecarativeEngine,将信号放在主要的 qml 项中,因此我没有使用 get child,并且我不断收到上面的消息。

谢谢

【问题讨论】:

    标签: qt4 signals-slots


    【解决方案1】:

    刚刚想通了,它不会连接,因为我使用的是 qunit16 数据类型而不是 int 我把这条线改成了这个

        connect(child,SIGNAL(tcpMsg(QString, int)), &client, SLOT(start(QString, int))) 
    

    然后它连接了。

    另外我不需要 QDeclarativeView 我已经从 QmlApplicationViewer 查看器中设置了 rootObject;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多