2017-05-06 1 views
1

QML 신호를 C++ 함수에 연결하려고합니다. 나는 내가 어떻게 올바른 물건을 얻을 수 있는지 정말로 모른다. 지금은 (쉬운 방법이있을 것입니다, 또한?) 작동하지 않는 경우 다음과 같은 방법에 그것을 시도, 여기에 내가 노력 코드입니다 :QML 및 C++이 신호에 연결

MAIN.CPP :

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include "include/myclass.h" 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 
    QGuiApplication app(argc, argv); 

    QQmlApplicationEngine engine; 
    engine.load(QUrl(QLatin1String("qrc:/main.qml"))); 

    QQmlComponent component(&engine, "qrc:/Page1.qml"); 
    QObject *item = component.create(); 
    MyClass myClass; 
    QObject::connect(item, SIGNAL(testSignal()),&myClass,SLOT(cppSlot())); 

    return app.exec(); 
} 

main.qml :

import QtQuick 2.7 
import QtQuick.Controls 2.0 
import QtQuick.Layouts 1.0 

ApplicationWindow { 
    visible: true 
    width: 800 
    height: 460 

    Page1 { 
     id: page1 
     visible: true 
    } 
} 

Page1.qml :

import QtQuick 2.7 
import QtQuick.Window 2.2 

Item { 
    width: 800 
    height: 460 

    signal testSignal() 

    CustomButton { 
     id: cppSignalButton 
     x: 14 
     y: 55 
     buttonText: "Test CPP Signal" 
     onButtonClicked: { 
      testSignal(); 
     } 
    } 
} 

CustomButton.qml :

,363,210
import QtQuick 2.7 
import QtQuick.Window 2.2 


Rectangle { 
    id: root 
    width: 200 
    height: 50 
    color: "#000000" 
    border.color: "#FFFFFF" 

    property string buttonText 
    signal buttonClicked() 

    MouseArea { 
     id: mouseArea 
     anchors.fill: parent 

     onClicked:{ 
      root.buttonClicked(); 
     } 

    } 
     Text { 
      id: text1 
      x: 105 
      y: 31 
      color: "#ffffff" 
      text: buttonText 
      anchors.verticalCenter: parent.verticalCenter 
      anchors.horizontalCenter: parent.horizontalCenter 
      font.pixelSize: 20 
     } 
} 

및 MyClass.cpp를 : 당신은 다음과 같은 방법으로 QML에서 MyClass 객체 메소드를 호출 할 수 있습니다 Integrating QML and C++

:

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <iostream> 
#include <fstream> 
#include <QQuickItem> 
#include <QQuickView> 

class MyClass : public QObject 
{ 
    Q_OBJECT 
public: 
    MyClass(){ 

    }; 
public slots: 
    void cppSlot() { 
     std::ofstream textfile; 
     textfile.open("test.txt"); 
     textfile << "Signal worked" << std::endl; 
     textfile.close(); 
     qInfo("Called the C++ slot"); 
    } 
}; 

답변

1

먼저이 문서를 읽어야

// main.cpp

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <QQmlContext> 
#include "include/myclass.h" 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 
    QGuiApplication app(argc, argv); 

    QQmlApplicationEngine engine; 
    MyClass myClass; 
    engine.rootContext()->setContextProperty("myClass", &myClass); 
    engine.load(QUrl(QLatin1String("qrc:/main.qml"))); 



    return app.exec(); 
} 

//Page1.qml

import QtQuick 2.7 
import QtQuick.Window 2.2 

Item { 
    width: 800 
    height: 460 

    signal testSignal() 

    CustomButton { 
     id: cppSignalButton 
     x: 14 
     y: 55 
     buttonText: "Test CPP Signal" 
     onButtonClicked: { 
      myClass.cppSlot(); //now you can use the context property to invoke your slot 
     } 
    } 
} 
+0

감사합니다. 이것은 내가 원하는 것입니다. #include 도 필요하다는 것을 말씀 드리고 싶습니다. – numberCruncher