Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Signal and Slot between qml and c++

$
0
0
Hello, I want to execute a method in c++ by clicking a button in a qml interface by sending a signal in the qml file and receiving it in the c++ file. But when I cklick the button the program doesn’t execute the method. I don’t know where the error is. Maybe someone of you will find the error or could give me a hint? Thank you for all answers! Here is my code so far: main.qml import QtQuick 2.0       Rectangle {     id: main     width: 500     height: 300         //create signal     signal trigger_signal (string msg)       //only for testing the signal. When I click the button the text "signal sent successful..." is in the console. So the signal must be sent successful       onTrigger_signal: console.log("signal sent successful with number " + msg)           Rectangle {         id: trigger1         x: 27         y: 22         width: 80         height: 80         color: "#ffffff"         border.width: 5         border.color: "#000000"           Text {             id: trigger1_text             x: 19             y: 33             text: qsTr("Trigger1")             font.pixelSize: 12         }           MouseArea {             id: trigger1_mousearea             anchors.fill: parent             hoverEnabled: true             onEntered: parent.border.color = "green"             onExited: parent.border.color = "black"             onClicked: {                 console.log("Trigger1 clicked")                   //send signal...                 main.trigger_signal ("1")             }         }     } } main.cpp #include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QQuickItem> #include <QObject> #include <QString> #include <QStringList> #include <QFile> #include <QProcess> #include <iostream> #include <QDebug> #include <string>   using namespace std;     class Dialog : public QObject {     Q_OBJECT     public slots:     //create slot...     void trigger_slot(const QString &msg)     {         cout << "Trigger received" << endl;         qDebug() << "Trigger received with number " << msg;     }   };       int main(int argc, char *argv[]) {     QGuiApplication app(argc, argv);       QtQuick2ApplicationViewer viewer;     viewer.setMainQmlFile(QStringLiteral("qml/Qml_Simpack_Trigger/main.qml"));     viewer.showExpanded();         //connect signal and slot     QQuickView     view(QUrl::fromLocalFile("qml/Qml_Simpack_Trigger/main.qml"));     QQuickItem *item = view.rootObject();         Dialog dialog;     QObject::connect(item, SIGNAL(trigger_signal(QString)), &dialog, SLOT(trigger_slot(QString)));         return app.exec(); }

Viewing all articles
Browse latest Browse all 13965

Trending Articles