Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Arduino analog input to QT Gauge.
Forum Update on Monday, May 27th 2025

Arduino analog input to QT Gauge.

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 2 Posters 10.4k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #8

    What errors is it emitting ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • S Offline
      S Offline
      StevieGardiner
      wrote on last edited by
      #9

      C:\Users\Stephn\Documents\300zx\dialog.cpp:37: error: no matching function for call to 'Dialog::connect(QDialog*&, const char*, QQuickItem*&, const char*)'
      QObject::connect(Dialog, SIGNAL(oil_pressure_bar(QVariant)),Rectangle, SLOT(needleAngle(QVariant)));
      ^

      This is the error I'm getting. much appreciated.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #10

        You are trying to connect classes. You have to connect instances of these classes.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • S Offline
          S Offline
          StevieGardiner
          wrote on last edited by StevieGardiner
          #11

          And by classes you mean "dialog" when I should be connecting " updatedoilpress"?
          And instead of "Rectangle" I should be connecting Rotation"?

          Also would this QObject::Connect be the easiest and simplest way of connecting a C++ signal (analogread2, oil_press_bar) to a QML Slot (Neeedle, needleAngle)?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #12

            You'd have to search for the objects you are interest in in your engine and then connect them.

            By the way, why not make this dialog also with QtQuick ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              StevieGardiner
              wrote on last edited by
              #13

              Ok so id search for the instances, I can google how to do that.

              What are the benefits of using QtQuick, over my method?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #14

                That was just curiosity. Nothing wrong in mixing both. If you go with a QtQuick only GUI you should be able to drop the dependency on the QtWidgets module which makes your application lighter weight.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  StevieGardiner
                  wrote on last edited by
                  #15

                  Hi,

                  I took your advice and used the cpp for serial data collection and the qml for the GUI, but I'm having a problem with this error.

                  C:\Users\Stephn\Documents\nissan300zx\serialport.h:50: error: 'double SerialPort::oil_pressure_volt' conflicts with a previous declaration
                  double oil_pressure_volt;
                  ^

                  I know that the code is duplicating names, but id like to read that name in Q_Property and then send it to QML.

                  #ifndef SERIALPORT_H
                  #define SERIALPORT_H
                  
                  #include <QObject>
                  #include <QQuickItem>
                  #include <QSerialPort>
                  #include <QSerialPortInfo>
                  #include <QByteArray>
                  
                  class SerialPort : public QObject
                  {
                      Q_OBJECT
                      Q_PROPERTY(QString oil_pressure_volt READ oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed)
                  
                  public:
                      SerialPort(QObject *parent = 0);
                              SerialPort(QString);
                  
                  
                              QString oil_pressure_volt() const
                              {
                                  return m_oil_pressure_volt;
                              }
                  
                  public slots:
                      void analogRead2();
                      void updateOilPressure(QString);
                  
                  
                      void set_oil_pressure_volt(QString oil_pressure_volt)
                      {
                          if (m_oil_pressure_volt == oil_pressure_volt)
                              return;
                  
                          m_oil_pressure_volt = oil_pressure_volt;
                          emit oil_pressure_volt_Changed(oil_pressure_volt);
                      }
                  
                  signals:
                  
                      void oil_pressure_volt_Changed(QString oil_pressure_volt);
                  
                  private:
                      QSerialPort *arduino;
                      static const quint16 arduino_uno_vendor_id = 0x2341;
                      static const quint16 arduino_uno_product_id = 0x0001;
                      QByteArray serialData;
                      QString serialBuffer;
                      QString parsed_data;
                      double oil_pressure_volt;
                  
                      QString m_oil_pressure_volt;
                  };
                  
                  #endif // SERIALPORT_H
                  
                  

                  Could I rename the oil_pressure_volt in the CPP

                  void SerialPort::updateOilPressure(QString sensor_reading)
                  {
                      //oil_pressure_volt(double) = data;
                  
                  }
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    Why are you declaring a oil_pressure_volt property QString then the matching variable as double and still use QString everywhere ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      StevieGardiner
                      wrote on last edited by
                      #17

                      Sorry, that was me trying different ideas out.

                      #ifndef SERIALPORT_H
                      #define SERIALPORT_H
                      
                      #include <QObject>
                      #include <QQuickItem>
                      #include <QSerialPort>
                      #include <QSerialPortInfo>
                      #include <QByteArray>
                      
                      class SerialPort : public QObject
                      {
                          Q_OBJECT
                          Q_PROPERTY(double oil_pressure_volt READ oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed)
                      
                      public:
                          SerialPort(QObject *parent = 0);
                                  SerialPort(QString);
                      
                                  double oil_pressure_volt() const
                                  {
                                      return m_oil_pressure_volt;
                                  }
                      
                      public slots:
                          void analogRead2();
                          void updateOilPressure(QString);
                      
                          void set_oil_pressure_volt(double oil_pressure_volt)
                          {
                              if (m_oil_pressure_volt == oil_pressure_volt)
                                  return;
                      
                              m_oil_pressure_volt = oil_pressure_volt;
                              emit oil_pressure_volt_Changed(oil_pressure_volt);
                          }
                      
                      signals:
                      
                          void oil_pressure_volt_Changed(double oil_pressure_volt);
                      
                      private:
                          QSerialPort *arduino;
                          static const quint16 arduino_uno_vendor_id = 0x2341;
                          static const quint16 arduino_uno_product_id = 0x0001;
                          QByteArray serialData;
                          QString serialBuffer;
                          QString parsed_data;
                          double oil_pressure_volt;
                      
                          double m_oil_pressure_volt;
                      };
                      
                      #endif // SERIALPORT_H
                      
                      This is the error,
                      
                      C:\Users\Stephn\Documents\nissan300zx\serialport.h:48: error: 'double SerialPort::oil_pressure_volt' conflicts with a previous declaration
                           double oil_pressure_volt;
                                  ^
                      
                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        StevieGardiner
                        wrote on last edited by
                        #18

                        Hi, So after making a few suggested changes I cant now display a "0" on my qml text field, I think this zero is the first reading of the analogRead 2.

                        I changed to this in the main.

                        engine.rootContext()->setContextProperty("serialport", &serialport);
                        

                        and this in the header.

                        Q_PROPERTY( double oilPressureVoltage MEMBER m_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )
                        
                        

                        I think I need to add some code into a string,

                        #ifndef SERIALPORT_H
                        #define SERIALPORT_H
                        
                        #include <QObject>
                        #include <QQuickItem>
                        #include <QSerialPort>
                        #include <QSerialPortInfo>
                        #include <QByteArray>
                        
                        class SerialPort : public QObject
                        {
                            Q_OBJECT
                            Q_PROPERTY( double oilPressureVoltage MEMBER m_oil_pressure_volt WRITE set_oil_pressure_volt NOTIFY oil_pressure_volt_Changed )
                        
                        public:
                            SerialPort(QObject *parent = 0);
                                    SerialPort(QString);
                        
                        public slots:
                            void analogRead2();
                            void updateOilPressure(QString);
                        
                            void set_oil_pressure_volt(double oilPressureVoltage)
                            {
                                if (m_oilPressureVoltage == oilPressureVoltage)
                                    return;
                        
                                m_oilPressureVoltage = oilPressureVoltage;
                                emit oil_pressure_volt_Changed(oilPressureVoltage);
                            }
                        
                        signals:
                        
                            void oil_pressure_volt_Changed(double oilPressureVoltage);
                        
                        private:
                            QSerialPort *arduino;
                            static const quint16 arduino_uno_vendor_id = 0x2341;
                            static const quint16 arduino_uno_product_id = 0x0001;
                            QByteArray serialData;
                            QString serialBuffer;
                            QString parsed_data;
                            double oil_pressure_volt;
                            double m_oilPressureVoltage;
                        };
                        
                        #endif // SERIALPORT_H
                        
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #19

                          Can you how how you are using that class in QML ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            StevieGardiner
                            wrote on last edited by
                            #20

                            yes its...

                            import QtQuick 2.5
                            import QtQuick.Window 2.2
                            import QtQuick.Extras 1.4
                            import QtQuick.Controls 2.0
                            import SerialPortlib 1.0
                            
                            Window {
                                id: gauge
                                visible: true
                                width: 640
                                height: 480
                            
                                TextEdit {
                                    id: textEdit
                                    text: SerialPort.newValue
                                    verticalAlignment: Text.AlignVCenter
                                    horizontalAlignment: Text.AlignCenter
                                    }
                            }
                            
                            and...
                            
                            

                            int main(int argc, char *argv[])
                            {
                            QGuiApplication app(argc, argv);
                            SerialPort serialport;
                            qmlRegisterType<SerialPort>("SerialPortlib", 1, 0, "SerialPort");
                            QQmlApplicationEngine engine;
                            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

                            return app.exec();
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #21

                              What's that newValue ?

                              You seem to be trying to use a SerialPort object but you are currently accessing it's class (or Type in QML).

                              You either need to create a SerialPort object in QML or give an instance from it to the engine.

                              Did you already read the QML CPP integration chapter of Qt's documentation ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                StevieGardiner
                                wrote on last edited by
                                #22

                                I tried to create an object by doing;

                                import QtQuick 2.5
                                import QtQuick.Window 2.2
                                import QtQuick.Extras 1.4
                                import QtQuick.Controls 2.0
                                import SerialPortlib 1.0
                                
                                Window {
                                    visible: true
                                    width: 640
                                    height: 480
                                    id: gauge
                                
                                    SerialPort{
                                        OnOil_pressure_volt_Changed:(console.log(newValue));
                                    }
                                }
                                
                                
                                

                                with no joy, is this what your referring too?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #23

                                  Your QML slot name is wrong.

                                  AFAIK it should be onOil_pressure_volt_changed Note that Qt's naming style is camelCase, that might help you match your code with the documentation.

                                  You should maybe take a look at http://qmlbook.org

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  1
                                  • S Offline
                                    S Offline
                                    StevieGardiner
                                    wrote on last edited by
                                    #24

                                    Ahh thank you, that was an interesting read, ive achieved what I wanted.

                                    1 Reply Last reply
                                    1
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #25

                                      Great !

                                      In that case, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      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