Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt QML signal and slot connection
Forum Updated to NodeBB v4.3 + New Features

Qt QML signal and slot connection

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 8 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JerwinprabuJ Offline
    JerwinprabuJ Offline
    Jerwinprabu
    wrote on last edited by A Former User
    #1

    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 !!!

    raven-worxR 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • JerwinprabuJ Jerwinprabu

        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 !!!

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • JerwinprabuJ Offline
          JerwinprabuJ Offline
          Jerwinprabu
          wrote on last edited by
          #4

          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 !!!

          raven-worxR 1 Reply Last reply
          0
          • JerwinprabuJ Jerwinprabu

            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 !!!

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • JerwinprabuJ Offline
              JerwinprabuJ Offline
              Jerwinprabu
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • JerwinprabuJ Jerwinprabu

                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.

                M Offline
                M Offline
                medyakovvit
                wrote on last edited by
                #7

                @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?

                JerwinprabuJ 1 Reply Last reply
                0
                • M medyakovvit

                  @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?

                  JerwinprabuJ Offline
                  JerwinprabuJ Offline
                  Jerwinprabu
                  wrote on last edited by
                  #8

                  @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" }
                  
                           }
                         }
                       }
                    }
                  
                  1 Reply Last reply
                  0
                  • BharathiB Offline
                    BharathiB Offline
                    Bharathi
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      srikanth06
                      wrote on last edited by
                      #10

                      Hpow did you solve this?

                      Pablo J. RoginaP 1 Reply Last reply
                      0
                      • S srikanth06

                        Hpow did you solve this?

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #11

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

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          srikanth2006
                          wrote on last edited by
                          #12

                          yes i did but I cant seem to get the solutions

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved