Multiple signals handling in one slot
Unsolved
QML and Qt Quick
-
Hi, dear Qt community! I am new to Qt development, few days ago started to learn this library.
I want to handle the pushButtons onClicked signals from QML front-end in C++ back-end., am i using the right approach for this task? or i should use another more proper way? Please give any advice..here is my simple code (Qt Quick project):
backend.h
#ifndef BACKEND_H #define BACKEND_H #include <QObject> #include <QMessageBox> class BackEnd : public QObject { Q_OBJECT public: explicit BackEnd(QObject *parent = nullptr); signals: public slots: void qml_Events_Handler(QString Id); }; #endif // BACKEND_H
backend.cpp
#include "backend.h" BackEnd::BackEnd(QObject *parent) : QObject(parent) { } void BackEnd::qml_Events_Handler(QString Control_ID) { if(Control_ID=="button") { QMessageBox msgBox; msgBox.setText("Hello QML."); msgBox.exec(); } if(Control_ID=="button1") { QMessageBox::warning(nullptr,tr("Тест"),"This is a text",QMessageBox::Ok); } Control_ID.clear(); }
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "backend.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); QQmlApplicationEngine engine; BackEnd backend; engine.rootContext()->setContextProperty("backend", &backend); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
main.qml
import QtQuick 2.14 import QtQuick.Controls 2.12 import QtQuick.Window 2.14 Window { id: mainWindow visible: true width: 360 height: 360 flags: Qt.FramelessWindowHint | Qt.Window color: "transparent" Rectangle { color: "#191919" anchors.fill: parent anchors.margins: 10 radius: 30 anchors.leftMargin: 10 anchors.bottomMargin: 242 opacity: 1 clip: true anchors.rightMargin: 10 anchors.topMargin: 20 MouseArea { anchors.fill: parent; property variant clickPos: "1,1" opacity: 1 onPressed: { clickPos = Qt.point(mouse.x,mouse.y) } onPositionChanged: { var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y) var new_x = mainWindow.x + delta.x var new_y = mainWindow.y + delta.y if (new_y <= 0) mainWindow.visibility = Window.Maximized else { if (mainWindow.visibility === Window.Maximized) mainWindow.visibility = Window.Windowed mainWindow.x = new_x mainWindow.y = new_y } } Button { id: button x: 174 y: 29 text: qsTr("Click Me") highlighted: true onClicked: backend.qml_Events_Handler("button") } } } Rectangle { color: "#191919" anchors.fill: parent anchors.margins: 10 radius: 30 rotation: 45 anchors.leftMargin: -15 anchors.bottomMargin: 126 opacity: 1 clip: true anchors.rightMargin: 35 anchors.topMargin: 136 MouseArea { anchors.fill: parent; property variant clickPos: "1,1" opacity: 1 onPressed: { clickPos = Qt.point(mouse.x,mouse.y) } onPositionChanged: { var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y) var new_x = mainWindow.x + delta.x var new_y = mainWindow.y + delta.y if (new_y <= 0) mainWindow.visibility = Window.Maximized else { if (mainWindow.visibility === Window.Maximized) mainWindow.visibility = Window.Windowed mainWindow.x = new_x mainWindow.y = new_y } } Button { id: button1 x: 149 y: 28 text: qsTr("Close") highlighted: true clip: false onClicked: { backend.qml_Events_Handler("button1") mainWindow.close() } } } } }