Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt QML signal and slot connection



  • Hi,

    I am a Roboticist, I am using Qt c++ (Embedded) for my project work. Already I have developed code for my Robot in normal UI. Using that UI I have connected signals and slots.

    Now I would like to create a UI like car, bike dashboard, I have decided to create a new dashboard UI, I have created dashboard also (Dashboard gauge style (circular), button (stop, start, forward, Reverse, Mode 1 and Mode 2 button, Emergency button) ).

    I need to connect signal and slot, everything with existing code, is it possible ? that existing normal UI having same button, I am not familiar in Qt QML, so for further step I need guidance !!!


  • Qt Champions 2017

    welcome to the forum. Question is not very clear on what you would like to achieve ? Are you telling that you have written the UI in QML and would like to have signal/slot communication from Qt ? Is that the question ? If yes, it is possible. If this is not your question please give more details.


  • Moderators

    @Jerwinprabu said:

    I need to connect signal and slot, everything with existing code, is it possible ? that existing normal UI having same button, I am not familiar in QT QML, so for further step I need guidance !!!

    This should get you covered with everything you need.



  • Ya, exactly. Thanks for your support. I need to connect signal and slot, everything with existing code, is it possible ? that existing normal UI having same button, I am not familiar in QT QML, so for further step I need guidance !!!


  • Moderators

    @Jerwinprabu
    You should start reading about QML to get an overview.
    Basically you can create a QObject subclass which just forwards calls from QML to your existing C++ code and vice versa.



  • For example, Dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    #include <QDialog>
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
        bool startThreadflag;
        bool stopThreadflag;
    
    private:
        Ui::Dialog *ui;
    
    public slots:
    
        void startThread();
        void stopThread();
    
    private slots:
    
        void on_Start_2_clicked();
        void on_Stop_2_clicked();
        void on_Close_clicked();
        void on_Left_clicked();
        void on_Right_clicked();
    };
    
    #endif // DIALOG_H
    

    Dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QDebug>
    #include <iostream>     
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <stdint.h>
    
    using namespace std;
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    
        startThreadflag = false;
        stopThreadflag = true;
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void  Dialog:: startThread(){
    
        if(!startThreadflag){
    
        this->ui->Start_2->setEnabled(false);
        this->ui-> Stop_2->setEnabled(true);
    
        startThreadflag = true;
        stopThreadflag = false;
    
        }
    }
    
    void Dialog::on_Start_2_clicked()
    {
        startThread();
    }
    
    void Dialog:: stopThread(){
    
        if(!stopThreadflag){
                cout << "Closing threads"<< endl;
    
            this->ui->Start_2->setEnabled(true);
            this->ui->Stop_2->setEnabled(false);
    
            stopThreadflag= true;
            startThreadflag = false;
        }
    }
    
    void Dialog::on_Stop_2_clicked()
    {
        stopThread();
    }
    
    void Dialog::on_Close_clicked()
    {
        cout<<"close_clicked"<<endl;
    
        this->close();
    }
    
    void Dialog::on_Left_clicked()
    {
        cout<<"Left Clicked"<<endl;
    }
    void Dialog::on_Right_clicked()
    {
        cout<<"Right Clicked"<<endl;
    }
    

    main.cpp

    #include "dialog.h"
    #include <QApplication>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        return a.exec();
    }
    

    That existing normal UI and qml having same button. I need to connect signal and slot in qt qml.



  • @Jerwinprabu it's not Qt Quick/Qml application it's Qt Widgets app. Do you want to rewrite your gui from Qt Widgets to Qt Quick/Qml?



  • @medyakovvit ya exactly. First I have developed my code in Qt Widgets. For dashboard design I want to rewrite gui from Qt Widgets to Qt Quick/Qml. I have designed button and everything (Except signals and slot) . For example

    Button {
           id:sender
            x:220
            y:295
            text: "Start"
            signal buttonClicked(string text)
            onClicked: {
               sender.buttonClicked.connect(receiver.changeText);
               sender.buttonClicked(" Started !!!")
                       }
              style: ButtonStyle {
               background: Rectangle {
                           implicitWidth: 100
                           implicitHeight: 40
                           border.width: control.activeFocus ? 1 : 2
                           border.color: "green"
                           radius: 10
    gradient: Gradient {
       GradientStop { position: 5 ; color: control.pressed ? "green" : "#eee" }
       GradientStop { position: 6 ; color: control.pressed ? "green" : "#ccc" }
    
             }
           }
        }
     }
    
    Button {
          id: receiver
           x:240
           y:-260
           text: "Welcome !!! Waiting for your Response !!!"
           function changeText(text) {
          receiver.text = text;
                 }
           style: ButtonStyle {
                background: Rectangle {
                 implicitWidth: 550
                 implicitHeight: 50
                 border.width: control.activeFocus ? 1 : 2
                 border.color: "black"
                 radius: 10
    gradient: Gradient {
      GradientStop { position: 5 ; color: control.pressed ? "#eee" : "white" }
       GradientStop { position: 6 ; color: control.pressed ? "#ccc" : "white" }
    
             }
           }
         }
      }
    


  • You are trying to pass the String as argument from qml after clicking(signal) the button and pass it to qtwidgets(slots)???



  • Hpow did you solve this?



  • @srikanth06 have you checked the documentation for QML + C++ integration?



  • yes i did but I cant seem to get the solutions


Log in to reply