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. QT Widget class inheriting other classes to use other methods

QT Widget class inheriting other classes to use other methods

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 548 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.
  • L Offline
    L Offline
    lukutis222
    wrote on 22 Jul 2022, 10:18 last edited by lukutis222
    #1

    Hello. I am new to c++. I have watched a couple of videos and tutorials but I would like to get good advice on what is the correct and best way to do certain things when it comes to class inheritance.

    For example. I have created a serial component:

    header file:

    #ifndef SERIAL_H
    #define SERIAL_H
    
    #include <QSerialPort>
    #include <QSerialPortInfo>
    
    class Serial
    {
    public:
    
        QSerialPort* serial;
    
        Serial();
        void Connect_to_device();
        void Serial_received();
        void Serial_write(QByteArray data);
        void Scan_serial_devices();
    };
    
    #endif // SERIAL_H
    
    

    cpp file:

    #include "serial.h"
    
    
    
    
    Serial::Serial()
    {
        serial = new QSerialPort;
    
    }
    
    void Serial::Connect_to_device(){
    
        serial->setPortName("COM3");
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
    
        if (serial->open(QIODevice::ReadWrite))
        {
            qDebug("connected sucesfully \n");
        }
        else
        {
            qDebug("connected failed \n");
        }
    
    
    }
    
    void Serial::Serial_received(){
        qDebug("serial received = %s",serial->readAll().toStdString().c_str());
    }
    
    
    void Serial::Serial_write(QByteArray data){
        qDebug("Trying to write data =%s \n",data.toStdString().c_str());
        serial->write(data);
    }
    
    
    
    void Serial::Scan_serial_devices(){
    
        QString description;
        QString manufacturer;
        QString serialNumber;
        const auto infos = QSerialPortInfo::availablePorts();
        for (const QSerialPortInfo &info : infos) {
            description = info.description();
            manufacturer = info.manufacturer();
            serialNumber = info.serialNumber();
            qDebug("description%s \n",description.toStdString().c_str());
            qDebug("manufacturer%s \n",manufacturer.toStdString().c_str());
            qDebug("serialNumber%s \n",serialNumber.toStdString().c_str());
        }
    
    }
    
    
    

    As you can see, I have implemented function Scan_serial_devices. This function scans all available serial devices which I will later want to display.

    I would like to call this function when button widget is clicked. In widget.cpp I have an event on pushbutton_2_clicked and I would like to call that function there, but since widget class does not know anything about serial class, It wont let me do it.

    void Widget::on_pushButton_2_clicked()
    {
        s.Scan_serial_devices(); // does not work because s is not known here
    }
    

    s is serial class object and is declared in main.cpp:

    #include "widget.h"
    
    #include <QApplication>
    #include "serial.h"
    
    int main(int argc, char *argv[])
    {
    
        QApplication a(argc, argv);
        Widget w;
        Serial s;
        w.show();
        return a.exec();
    }
    
    

    Can someone give me an idea what is the correct way to use other class functions within widget class?

    J 1 Reply Last reply 22 Jul 2022, 10:20
    0
    • L lukutis222
      22 Jul 2022, 10:18

      Hello. I am new to c++. I have watched a couple of videos and tutorials but I would like to get good advice on what is the correct and best way to do certain things when it comes to class inheritance.

      For example. I have created a serial component:

      header file:

      #ifndef SERIAL_H
      #define SERIAL_H
      
      #include <QSerialPort>
      #include <QSerialPortInfo>
      
      class Serial
      {
      public:
      
          QSerialPort* serial;
      
          Serial();
          void Connect_to_device();
          void Serial_received();
          void Serial_write(QByteArray data);
          void Scan_serial_devices();
      };
      
      #endif // SERIAL_H
      
      

      cpp file:

      #include "serial.h"
      
      
      
      
      Serial::Serial()
      {
          serial = new QSerialPort;
      
      }
      
      void Serial::Connect_to_device(){
      
          serial->setPortName("COM3");
          serial->setBaudRate(QSerialPort::Baud115200);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
      
          if (serial->open(QIODevice::ReadWrite))
          {
              qDebug("connected sucesfully \n");
          }
          else
          {
              qDebug("connected failed \n");
          }
      
      
      }
      
      void Serial::Serial_received(){
          qDebug("serial received = %s",serial->readAll().toStdString().c_str());
      }
      
      
      void Serial::Serial_write(QByteArray data){
          qDebug("Trying to write data =%s \n",data.toStdString().c_str());
          serial->write(data);
      }
      
      
      
      void Serial::Scan_serial_devices(){
      
          QString description;
          QString manufacturer;
          QString serialNumber;
          const auto infos = QSerialPortInfo::availablePorts();
          for (const QSerialPortInfo &info : infos) {
              description = info.description();
              manufacturer = info.manufacturer();
              serialNumber = info.serialNumber();
              qDebug("description%s \n",description.toStdString().c_str());
              qDebug("manufacturer%s \n",manufacturer.toStdString().c_str());
              qDebug("serialNumber%s \n",serialNumber.toStdString().c_str());
          }
      
      }
      
      
      

      As you can see, I have implemented function Scan_serial_devices. This function scans all available serial devices which I will later want to display.

      I would like to call this function when button widget is clicked. In widget.cpp I have an event on pushbutton_2_clicked and I would like to call that function there, but since widget class does not know anything about serial class, It wont let me do it.

      void Widget::on_pushButton_2_clicked()
      {
          s.Scan_serial_devices(); // does not work because s is not known here
      }
      

      s is serial class object and is declared in main.cpp:

      #include "widget.h"
      
      #include <QApplication>
      #include "serial.h"
      
      int main(int argc, char *argv[])
      {
      
          QApplication a(argc, argv);
          Widget w;
          Serial s;
          w.show();
          return a.exec();
      }
      
      

      Can someone give me an idea what is the correct way to use other class functions within widget class?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 22 Jul 2022, 10:20 last edited by jsulm
      #2

      @lukutis222 said in QT Widget class inheriting other classes to use other methods:

      does not work because s is not known here

      Simply define s as class member in your Widget class:

      #include "serial.h"
      
      class Widget...
      {
      private:
          Serial s;
      

      Or, if you need it also in other places, pass pointer to it to Widget instance:

      int main(int argc, char *argv[])
      {
      
          QApplication a(argc, argv);
          Serial s;
          Widget w(&s);
          w.show();
          return a.exec();
      }
      

      Of course you will need to change Widget constructor, so it can take a pointer to Serial and store this pointer in a class member in Widget.

      Better way in a Qt application would be to use signals and slots: define signals in Serial to distribute information and add slots in Widget where you use this information. Then connect these signals and slots. See https://doc.qt.io/qt-6/signalsandslots.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 25 Jul 2022, 07:58
      3
      • J jsulm
        22 Jul 2022, 10:20

        @lukutis222 said in QT Widget class inheriting other classes to use other methods:

        does not work because s is not known here

        Simply define s as class member in your Widget class:

        #include "serial.h"
        
        class Widget...
        {
        private:
            Serial s;
        

        Or, if you need it also in other places, pass pointer to it to Widget instance:

        int main(int argc, char *argv[])
        {
        
            QApplication a(argc, argv);
            Serial s;
            Widget w(&s);
            w.show();
            return a.exec();
        }
        

        Of course you will need to change Widget constructor, so it can take a pointer to Serial and store this pointer in a class member in Widget.

        Better way in a Qt application would be to use signals and slots: define signals in Serial to distribute information and add slots in Widget where you use this information. Then connect these signals and slots. See https://doc.qt.io/qt-6/signalsandslots.html

        L Offline
        L Offline
        lukutis222
        wrote on 25 Jul 2022, 07:58 last edited by
        #3

        @jsulm

        Hello. Thank you very much for your response. Sorry for taking some time to coming back to this. I would like to clarify the following:

        If I create an object in my main.cpp

        int main(int argc, char *argv[])
        {
        
            QApplication a(argc, argv);
            Widget w;
            Serial s;
        //Serial_list->addItems("test");
        
            w.show();
            return a.exec();
        }
        
        

        And then I define s as class member in my other widget class:

        #ifndef WIDGET_H
        #define WIDGET_H
        
        #include <QWidget>
        #include "serial.h"
        
        
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class Widget; }
        QT_END_NAMESPACE
        
        class Widget : public QWidget
        {
            Q_OBJECT
        
        public:
            Widget(QWidget *parent = nullptr);
            ~Widget();
        
        private slots:
            void on_Serial_list_activated(int index);
        
            void on_Serial_connect_clicked();
        
            void on_Scan_button_clicked();
        
        
        
            void on_Baudrate_select_activated(int index);
        
        private:
            Ui::Widget *ui;
            Serial s;
        };
        #endif // WIDGET_H
        
        

        Wouldnt that create a completely different instance of my Serial class object? How do I know for sure that it is referring to the same object instance since I can have multiple Serial class objects

        J JonBJ 2 Replies Last reply 25 Jul 2022, 08:16
        0
        • L lukutis222
          25 Jul 2022, 07:58

          @jsulm

          Hello. Thank you very much for your response. Sorry for taking some time to coming back to this. I would like to clarify the following:

          If I create an object in my main.cpp

          int main(int argc, char *argv[])
          {
          
              QApplication a(argc, argv);
              Widget w;
              Serial s;
          //Serial_list->addItems("test");
          
              w.show();
              return a.exec();
          }
          
          

          And then I define s as class member in my other widget class:

          #ifndef WIDGET_H
          #define WIDGET_H
          
          #include <QWidget>
          #include "serial.h"
          
          
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class Widget; }
          QT_END_NAMESPACE
          
          class Widget : public QWidget
          {
              Q_OBJECT
          
          public:
              Widget(QWidget *parent = nullptr);
              ~Widget();
          
          private slots:
              void on_Serial_list_activated(int index);
          
              void on_Serial_connect_clicked();
          
              void on_Scan_button_clicked();
          
          
          
              void on_Baudrate_select_activated(int index);
          
          private:
              Ui::Widget *ui;
              Serial s;
          };
          #endif // WIDGET_H
          
          

          Wouldnt that create a completely different instance of my Serial class object? How do I know for sure that it is referring to the same object instance since I can have multiple Serial class objects

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 25 Jul 2022, 08:16 last edited by jsulm
          #4

          @lukutis222 said in QT Widget class inheriting other classes to use other methods:

          Wouldnt that create a completely different instance of my Serial class object?

          Of course, but if only Widget needs Serial, then simply remove s from main...
          If you need Serial also in other places then take a look at my other suggestions.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • L lukutis222
            25 Jul 2022, 07:58

            @jsulm

            Hello. Thank you very much for your response. Sorry for taking some time to coming back to this. I would like to clarify the following:

            If I create an object in my main.cpp

            int main(int argc, char *argv[])
            {
            
                QApplication a(argc, argv);
                Widget w;
                Serial s;
            //Serial_list->addItems("test");
            
                w.show();
                return a.exec();
            }
            
            

            And then I define s as class member in my other widget class:

            #ifndef WIDGET_H
            #define WIDGET_H
            
            #include <QWidget>
            #include "serial.h"
            
            
            
            QT_BEGIN_NAMESPACE
            namespace Ui { class Widget; }
            QT_END_NAMESPACE
            
            class Widget : public QWidget
            {
                Q_OBJECT
            
            public:
                Widget(QWidget *parent = nullptr);
                ~Widget();
            
            private slots:
                void on_Serial_list_activated(int index);
            
                void on_Serial_connect_clicked();
            
                void on_Scan_button_clicked();
            
            
            
                void on_Baudrate_select_activated(int index);
            
            private:
                Ui::Widget *ui;
                Serial s;
            };
            #endif // WIDGET_H
            
            

            Wouldnt that create a completely different instance of my Serial class object? How do I know for sure that it is referring to the same object instance since I can have multiple Serial class objects

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 25 Jul 2022, 08:16 last edited by JonB
            #5

            @lukutis222
            In the code you show there is no relationship between the Serial s; in main() versus the one in the private section of Widget. Other than they are both of class Serial, they are quite distinct instances, and know nothing about each other. Does that answer your question?

            Earlier @jsulm was advising you to use one or the other of these, not both.

            1 Reply Last reply
            2
            • L Offline
              L Offline
              lukutis222
              wrote on 25 Jul 2022, 08:20 last edited by
              #6

              Yes now it is clear. Thank you very much for confirming

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lukutis222
                wrote on 26 Aug 2022, 08:32 last edited by lukutis222
                #7

                @jsulm

                Sorry for getting back to it after a long time, but I would like to understand how to correctly pass a class object pointer to another class constructor as you shown in the example:

                int main(int argc, char *argv[])
                {
                
                    QApplication a(argc, argv);
                    Serial s;
                    Widget w(&s);
                    w.show();
                    return a.exec();
                }
                

                So I have a Serial class and my serial.h looks like:

                #ifndef SERIAL_H
                #define SERIAL_H
                
                #include <QWidget>
                #include "QSerialPort"
                #include "QSerialPortInfo"
                #include "stdio.h"
                #include "stdint.h"
                
                
                
                class Serial : public QWidget
                {
                    Q_OBJECT
                public:
                    explicit Serial(QWidget *parent = nullptr);
                
                    QSerialPort serial_connection; // this keeps all information regarding the serial connection
                    QString available_devices[10];
                    uint8_t device_counter;
                
                
                    void Scan_serial_devices();
                    bool Serial_connect();
                    bool Serial_disconnect();
                
                    void write_data(QByteArray data);
                    bool is_data_available();
                    QByteArray read_data();
                
                
                    void Set_Baudrate(int32_t baudrate);
                    void Set_Portname(QString name);
                    void Set_Databits(QSerialPort::DataBits dataBits);
                    void Set_Stopbits(QSerialPort::StopBits stopBits);
                    void Set_Flowcontrol(QSerialPort::FlowControl flowControl);
                    void Set_Paritybits(QSerialPort::Parity parity);
                
                signals:
                
                
                private:
                    QWidget *Widget;
                };
                
                #endif // SERIAL_H
                

                I create a class instance in my main.cpp and try to pass it to Widget class constructor. =

                int main(int argc, char *argv[])
                {
                
                    QApplication a(argc, argv);
                    Serial s;
                    Widget w(&s);
                
                    w.show();
                    return a.exec();
                }
                

                I have modified my widget class constructor as following:

                Widget.cpp

                Widget::Widget(QWidget *parent,Serial* serial_instance)
                    : QWidget(parent)
                    , ui(new Ui::Widget)
                
                
                {
                    ui->setupUi(this);
                    fillPortsParameters();
                    //connect(&serial_obj->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                
                }
                
                

                Widget.h

                #ifndef WIDGET_H
                #define WIDGET_H
                
                #include <QWidget>
                #include "serial.h"
                #include "logging.h"
                #include <QTime>
                #include <QDate>
                #include <QDir>
                #include <QListView>
                #include <QScrollArea>
                
                QT_BEGIN_NAMESPACE
                namespace Ui { class Widget; }
                QT_END_NAMESPACE
                
                
                
                class Widget : public QWidget
                {
                    Q_OBJECT
                
                public:
                    Widget(QWidget *parent = nullptr,Serial* serial_instance);
                    ~Widget();
                
                
                
                
                
                
                
                void fillPortsParameters();
                
                
                private slots:
                
                    void on_Scan_button_clicked();
                    void on_Serial_connect_button_clicked();
                    void readData();
                    void on_write_box_returnPressed();
                    void on_Scenario_select_currentIndexChanged(int index);
                
                
                private:
                    Ui::Widget *ui;
                
                
                protected:
                
                
                
                };
                #endif // WIDGET_H
                
                

                I am not fully understanding why is it complaining:
                acf1b5e7-15f2-4e1e-842d-6d1d8c596a9d-image.png

                Am I not passing a class object to class constructor correctly?

                JonBJ 1 Reply Last reply 26 Aug 2022, 08:41
                0
                • L lukutis222
                  26 Aug 2022, 08:32

                  @jsulm

                  Sorry for getting back to it after a long time, but I would like to understand how to correctly pass a class object pointer to another class constructor as you shown in the example:

                  int main(int argc, char *argv[])
                  {
                  
                      QApplication a(argc, argv);
                      Serial s;
                      Widget w(&s);
                      w.show();
                      return a.exec();
                  }
                  

                  So I have a Serial class and my serial.h looks like:

                  #ifndef SERIAL_H
                  #define SERIAL_H
                  
                  #include <QWidget>
                  #include "QSerialPort"
                  #include "QSerialPortInfo"
                  #include "stdio.h"
                  #include "stdint.h"
                  
                  
                  
                  class Serial : public QWidget
                  {
                      Q_OBJECT
                  public:
                      explicit Serial(QWidget *parent = nullptr);
                  
                      QSerialPort serial_connection; // this keeps all information regarding the serial connection
                      QString available_devices[10];
                      uint8_t device_counter;
                  
                  
                      void Scan_serial_devices();
                      bool Serial_connect();
                      bool Serial_disconnect();
                  
                      void write_data(QByteArray data);
                      bool is_data_available();
                      QByteArray read_data();
                  
                  
                      void Set_Baudrate(int32_t baudrate);
                      void Set_Portname(QString name);
                      void Set_Databits(QSerialPort::DataBits dataBits);
                      void Set_Stopbits(QSerialPort::StopBits stopBits);
                      void Set_Flowcontrol(QSerialPort::FlowControl flowControl);
                      void Set_Paritybits(QSerialPort::Parity parity);
                  
                  signals:
                  
                  
                  private:
                      QWidget *Widget;
                  };
                  
                  #endif // SERIAL_H
                  

                  I create a class instance in my main.cpp and try to pass it to Widget class constructor. =

                  int main(int argc, char *argv[])
                  {
                  
                      QApplication a(argc, argv);
                      Serial s;
                      Widget w(&s);
                  
                      w.show();
                      return a.exec();
                  }
                  

                  I have modified my widget class constructor as following:

                  Widget.cpp

                  Widget::Widget(QWidget *parent,Serial* serial_instance)
                      : QWidget(parent)
                      , ui(new Ui::Widget)
                  
                  
                  {
                      ui->setupUi(this);
                      fillPortsParameters();
                      //connect(&serial_obj->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                  
                  }
                  
                  

                  Widget.h

                  #ifndef WIDGET_H
                  #define WIDGET_H
                  
                  #include <QWidget>
                  #include "serial.h"
                  #include "logging.h"
                  #include <QTime>
                  #include <QDate>
                  #include <QDir>
                  #include <QListView>
                  #include <QScrollArea>
                  
                  QT_BEGIN_NAMESPACE
                  namespace Ui { class Widget; }
                  QT_END_NAMESPACE
                  
                  
                  
                  class Widget : public QWidget
                  {
                      Q_OBJECT
                  
                  public:
                      Widget(QWidget *parent = nullptr,Serial* serial_instance);
                      ~Widget();
                  
                  
                  
                  
                  
                  
                  
                  void fillPortsParameters();
                  
                  
                  private slots:
                  
                      void on_Scan_button_clicked();
                      void on_Serial_connect_button_clicked();
                      void readData();
                      void on_write_box_returnPressed();
                      void on_Scenario_select_currentIndexChanged(int index);
                  
                  
                  private:
                      Ui::Widget *ui;
                  
                  
                  protected:
                  
                  
                  
                  };
                  #endif // WIDGET_H
                  
                  

                  I am not fully understanding why is it complaining:
                  acf1b5e7-15f2-4e1e-842d-6d1d8c596a9d-image.png

                  Am I not passing a class object to class constructor correctly?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 26 Aug 2022, 08:41 last edited by
                  #8

                  @lukutis222 said in QT Widget class inheriting other classes to use other methods:

                  how to correctly pass a class object pointer to another class constructor

                  That is what you are doing in your Widget w(&s) statement, passing a * Serial (class object pointer) to the Widget constructor (another class constructor).

                  Now look at your Widget(QWidget *parent = nullptr, Serial *serial_instance). This has a default for the first parameter and not for the second parameter, which is not allowed in C++. Optional parameters (those with a default) must come after mandatory ones (those without a default).

                  This is a separate issue from whether your Serial class should be a QWidget (why are you making it such, is it really any kind of widget?), and why it has a private: QWidget *Widget;, what is that supposed to be for?

                  L 1 Reply Last reply 26 Aug 2022, 08:54
                  1
                  • JonBJ JonB
                    26 Aug 2022, 08:41

                    @lukutis222 said in QT Widget class inheriting other classes to use other methods:

                    how to correctly pass a class object pointer to another class constructor

                    That is what you are doing in your Widget w(&s) statement, passing a * Serial (class object pointer) to the Widget constructor (another class constructor).

                    Now look at your Widget(QWidget *parent = nullptr, Serial *serial_instance). This has a default for the first parameter and not for the second parameter, which is not allowed in C++. Optional parameters (those with a default) must come after mandatory ones (those without a default).

                    This is a separate issue from whether your Serial class should be a QWidget (why are you making it such, is it really any kind of widget?), and why it has a private: QWidget *Widget;, what is that supposed to be for?

                    L Offline
                    L Offline
                    lukutis222
                    wrote on 26 Aug 2022, 08:54 last edited by lukutis222
                    #9

                    @JonB

                    You are totally right about My Serial class and it should not be QWidget. The reason why that happen is because I was generating this class automatically and accidentally select QWidget as base class. I have fixed my serial class :

                    serial.cpp

                    #include "serial.h"
                    
                    
                    Serial::Serial()
                    {
                    
                    }
                    
                    
                    void Serial::Scan_serial_devices(){
                    
                        QString description;
                        QString manufacturer;
                        QString serialNumber;
                        QString portName;
                        this->available_devices->clear();
                        this->device_counter = 0;
                    
                        const auto infos = QSerialPortInfo::availablePorts();
                        for (const QSerialPortInfo &info : infos) {
                            description = info.description();
                            manufacturer = info.manufacturer();
                            serialNumber = info.serialNumber();
                            portName = info.portName();
                            //qDebug("description = %s \n",description.toStdString().c_str());
                            //qDebug("manufacturer = %s \n",manufacturer.toStdString().c_str());
                            //qDebug("serialNumber = %s \n",serialNumber.toStdString().c_str());
                            //qDebug("Portname = %s \n",portName.toStdString().c_str());
                            available_devices[device_counter] = portName;
                            device_counter++;
                    
                        }
                    
                    }
                    
                    
                    bool Serial::Serial_connect(){
                    
                        if(serial_connection.open(QIODevice::ReadWrite)){
                            qDebug("Connection is succesfull \n");
                            return 1;
                        }
                        else
                        {
                            //error
                            qDebug() << serial_connection.errorString();
                        }
                        return 0;
                    
                    }
                    
                    
                    bool Serial::Serial_disconnect(){
                        serial_connection.close();
                        return 1;
                    }
                    
                    
                    
                    void Serial::Set_Baudrate(int32_t baudrate){
                        serial_connection.setBaudRate(baudrate);
                    }
                    
                    void Serial::Set_Portname(QString name){
                        serial_connection.setPortName(name);
                    }
                    
                    void Serial::Set_Databits(QSerialPort::DataBits dataBits){
                        serial_connection.setDataBits(dataBits);
                    }
                    
                    void Serial::Set_Stopbits(QSerialPort::StopBits stopBits){
                        serial_connection.setStopBits(stopBits);
                    }
                    
                    void Serial::Set_Paritybits(QSerialPort::Parity parity){
                        serial_connection.setParity(parity);
                    }
                    void Serial::Set_Flowcontrol(QSerialPort::FlowControl flowControl){
                        serial_connection.setFlowControl(flowControl);
                    }
                    
                    
                    
                    
                    void Serial::write_data(QByteArray data){
                        serial_connection.write(data);
                    }
                    
                    bool Serial::is_data_available(){
                         return (serial_connection.canReadLine());
                    }
                    
                    QByteArray Serial::read_data(){
                        QByteArray line = serial_connection.readLine();
                        return line;
                    }
                    
                    

                    serial.h

                    #ifndef SERIAL_H
                    #define SERIAL_H
                    
                    
                    #include "QSerialPort"
                    #include "QSerialPortInfo"
                    #include "stdio.h"
                    #include "stdint.h"
                    #include <qDebug>
                    
                    
                    
                    
                    class Serial
                    {
                    
                    public:
                        Serial();
                    
                        QSerialPort serial_connection; // this keeps all information regarding the serial connection
                        QString available_devices[10];
                        uint8_t device_counter;
                    
                    
                        void Scan_serial_devices();
                        bool Serial_connect();
                        bool Serial_disconnect();
                    
                        void write_data(QByteArray data);
                        bool is_data_available();
                        QByteArray read_data();
                    
                    
                        void Set_Baudrate(int32_t baudrate);
                        void Set_Portname(QString name);
                        void Set_Databits(QSerialPort::DataBits dataBits);
                        void Set_Stopbits(QSerialPort::StopBits stopBits);
                        void Set_Flowcontrol(QSerialPort::FlowControl flowControl);
                        void Set_Paritybits(QSerialPort::Parity parity);
                    
                    signals:
                    
                    
                    private:
                    
                    };
                    
                    #endif // SERIAL_H
                    
                    

                    Now going back to the main subject, when I try to pass a pointer to a Serial class object, I get the following error:

                    C:\Users\petrikas.lu\Desktop\WORK\QT\UTB\UTB_gui\main.cpp:13: error: no matching function for call to 'Widget::Widget(Serial*)'
                    ..\UTB_gui\main.cpp: In function 'int qMain(int, char**)':
                    ..\UTB_gui\main.cpp:13:16: error: no matching function for call to 'Widget::Widget(Serial*)'
                       13 |     Widget w(&s);
                          |         
                           ^
                    

                    In my widget.h I have modified the class constructor but I must be missing something else.

                    QT_BEGIN_NAMESPACE
                    namespace Ui { class Widget; }
                    QT_END_NAMESPACE
                    
                    
                    
                    class Widget : public QWidget
                    {
                        Q_OBJECT
                    
                    public:
                    
                        Widget(QWidget *parent = nullptr,Serial* serial_ptr = nullptr);
                        ~Widget();
                    
                    
                    

                    And in my widget.cpp I also added a parameter to a widget class constructor:

                    
                    
                    
                    
                    Widget::Widget(QWidget *parent, Serial* serial_ptr)
                        : QWidget(parent)
                        , ui(new Ui::Widget)
                    
                    
                    {
                    
                        ui->setupUi(this);
                        fillPortsParameters();
                        //connect(&serial_obj->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                    
                    }
                    
                    
                    
                    Widget::~Widget()
                    {
                        delete ui;
                    
                    }
                    
                    
                    

                    This might be confusing for me because for the widget class constructor it supposed to take 2 parameters:

                    
                    Widget::Widget(QWidget *parent, Serial* serial_ptr)
                    

                    but I only pass 1 parameter, that is not correct right?

                    JonBJ 1 Reply Last reply 26 Aug 2022, 09:00
                    0
                    • L lukutis222
                      26 Aug 2022, 08:54

                      @JonB

                      You are totally right about My Serial class and it should not be QWidget. The reason why that happen is because I was generating this class automatically and accidentally select QWidget as base class. I have fixed my serial class :

                      serial.cpp

                      #include "serial.h"
                      
                      
                      Serial::Serial()
                      {
                      
                      }
                      
                      
                      void Serial::Scan_serial_devices(){
                      
                          QString description;
                          QString manufacturer;
                          QString serialNumber;
                          QString portName;
                          this->available_devices->clear();
                          this->device_counter = 0;
                      
                          const auto infos = QSerialPortInfo::availablePorts();
                          for (const QSerialPortInfo &info : infos) {
                              description = info.description();
                              manufacturer = info.manufacturer();
                              serialNumber = info.serialNumber();
                              portName = info.portName();
                              //qDebug("description = %s \n",description.toStdString().c_str());
                              //qDebug("manufacturer = %s \n",manufacturer.toStdString().c_str());
                              //qDebug("serialNumber = %s \n",serialNumber.toStdString().c_str());
                              //qDebug("Portname = %s \n",portName.toStdString().c_str());
                              available_devices[device_counter] = portName;
                              device_counter++;
                      
                          }
                      
                      }
                      
                      
                      bool Serial::Serial_connect(){
                      
                          if(serial_connection.open(QIODevice::ReadWrite)){
                              qDebug("Connection is succesfull \n");
                              return 1;
                          }
                          else
                          {
                              //error
                              qDebug() << serial_connection.errorString();
                          }
                          return 0;
                      
                      }
                      
                      
                      bool Serial::Serial_disconnect(){
                          serial_connection.close();
                          return 1;
                      }
                      
                      
                      
                      void Serial::Set_Baudrate(int32_t baudrate){
                          serial_connection.setBaudRate(baudrate);
                      }
                      
                      void Serial::Set_Portname(QString name){
                          serial_connection.setPortName(name);
                      }
                      
                      void Serial::Set_Databits(QSerialPort::DataBits dataBits){
                          serial_connection.setDataBits(dataBits);
                      }
                      
                      void Serial::Set_Stopbits(QSerialPort::StopBits stopBits){
                          serial_connection.setStopBits(stopBits);
                      }
                      
                      void Serial::Set_Paritybits(QSerialPort::Parity parity){
                          serial_connection.setParity(parity);
                      }
                      void Serial::Set_Flowcontrol(QSerialPort::FlowControl flowControl){
                          serial_connection.setFlowControl(flowControl);
                      }
                      
                      
                      
                      
                      void Serial::write_data(QByteArray data){
                          serial_connection.write(data);
                      }
                      
                      bool Serial::is_data_available(){
                           return (serial_connection.canReadLine());
                      }
                      
                      QByteArray Serial::read_data(){
                          QByteArray line = serial_connection.readLine();
                          return line;
                      }
                      
                      

                      serial.h

                      #ifndef SERIAL_H
                      #define SERIAL_H
                      
                      
                      #include "QSerialPort"
                      #include "QSerialPortInfo"
                      #include "stdio.h"
                      #include "stdint.h"
                      #include <qDebug>
                      
                      
                      
                      
                      class Serial
                      {
                      
                      public:
                          Serial();
                      
                          QSerialPort serial_connection; // this keeps all information regarding the serial connection
                          QString available_devices[10];
                          uint8_t device_counter;
                      
                      
                          void Scan_serial_devices();
                          bool Serial_connect();
                          bool Serial_disconnect();
                      
                          void write_data(QByteArray data);
                          bool is_data_available();
                          QByteArray read_data();
                      
                      
                          void Set_Baudrate(int32_t baudrate);
                          void Set_Portname(QString name);
                          void Set_Databits(QSerialPort::DataBits dataBits);
                          void Set_Stopbits(QSerialPort::StopBits stopBits);
                          void Set_Flowcontrol(QSerialPort::FlowControl flowControl);
                          void Set_Paritybits(QSerialPort::Parity parity);
                      
                      signals:
                      
                      
                      private:
                      
                      };
                      
                      #endif // SERIAL_H
                      
                      

                      Now going back to the main subject, when I try to pass a pointer to a Serial class object, I get the following error:

                      C:\Users\petrikas.lu\Desktop\WORK\QT\UTB\UTB_gui\main.cpp:13: error: no matching function for call to 'Widget::Widget(Serial*)'
                      ..\UTB_gui\main.cpp: In function 'int qMain(int, char**)':
                      ..\UTB_gui\main.cpp:13:16: error: no matching function for call to 'Widget::Widget(Serial*)'
                         13 |     Widget w(&s);
                            |         
                             ^
                      

                      In my widget.h I have modified the class constructor but I must be missing something else.

                      QT_BEGIN_NAMESPACE
                      namespace Ui { class Widget; }
                      QT_END_NAMESPACE
                      
                      
                      
                      class Widget : public QWidget
                      {
                          Q_OBJECT
                      
                      public:
                      
                          Widget(QWidget *parent = nullptr,Serial* serial_ptr = nullptr);
                          ~Widget();
                      
                      
                      

                      And in my widget.cpp I also added a parameter to a widget class constructor:

                      
                      
                      
                      
                      Widget::Widget(QWidget *parent, Serial* serial_ptr)
                          : QWidget(parent)
                          , ui(new Ui::Widget)
                      
                      
                      {
                      
                          ui->setupUi(this);
                          fillPortsParameters();
                          //connect(&serial_obj->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                      
                      }
                      
                      
                      
                      Widget::~Widget()
                      {
                          delete ui;
                      
                      }
                      
                      
                      

                      This might be confusing for me because for the widget class constructor it supposed to take 2 parameters:

                      
                      Widget::Widget(QWidget *parent, Serial* serial_ptr)
                      

                      but I only pass 1 parameter, that is not correct right?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 26 Aug 2022, 09:00 last edited by JonB
                      #10

                      @lukutis222 said in QT Widget class inheriting other classes to use other methods:

                      no matching function for call to 'Widget::Widget(Serial*)'

                      Well that's right, isn't it, because your constructor is Widget(QWidget *parent = nullptr,Serial* serial_ptr = nullptr);, so how could that match your attempt to pass Widget w(&s); which has a Serial * as its first parameter?

                      Presumably you want its constructor to be Widget(Serial* serial_ptr, QWidget *parent = nullptr);.... It requires (i'm guessing that will be your intention for what you are writing) some Serial* for its first argument, and accepts an optional QWidget * as a parent for its second parameter (else uses nullptr for that value).

                      J 1 Reply Last reply 26 Aug 2022, 09:02
                      0
                      • JonBJ JonB
                        26 Aug 2022, 09:00

                        @lukutis222 said in QT Widget class inheriting other classes to use other methods:

                        no matching function for call to 'Widget::Widget(Serial*)'

                        Well that's right, isn't it, because your constructor is Widget(QWidget *parent = nullptr,Serial* serial_ptr = nullptr);, so how could that match your attempt to pass Widget w(&s); which has a Serial * as its first parameter?

                        Presumably you want its constructor to be Widget(Serial* serial_ptr, QWidget *parent = nullptr);.... It requires (i'm guessing that will be your intention for what you are writing) some Serial* for its first argument, and accepts an optional QWidget * as a parent for its second parameter (else uses nullptr for that value).

                        J Online
                        J Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 26 Aug 2022, 09:02 last edited by
                        #11

                        @JonB said in QT Widget class inheriting other classes to use other methods:

                        Presumably you want its constructor to be Widget(Serial* serial_ptr, QWidget *parent = nullptr);....

                        Or simply pass nullptr for the first parameter...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lukutis222
                          wrote on 26 Aug 2022, 09:11 last edited by
                          #12

                          Thank you both very much you are very kind @jsulm @JonB .

                          The last thing I did:

                          On my widget.h, I have declared a Serial variable that will be local for the Widget class but it will point to the global Serial class object.

                          QT_BEGIN_NAMESPACE
                          namespace Ui { class Widget; }
                          QT_END_NAMESPACE
                          
                          
                          
                          class Widget : public QWidget
                          {
                              Q_OBJECT
                          
                          public:
                          
                          
                              Serial* serial_local; // this will be "local" instance of the Serial object that will point to the global Serial object.
                          
                          
                              Widget(Serial* serial_ptr = nullptr, QWidget *parent = nullptr);
                              ~Widget();
                          
                          
                          

                          And in my widget class constructor, I assign it to the variable that I pass in my main.cpp:

                          Widget::Widget(Serial* serial_ptr,QWidget *parent)
                              : QWidget(parent)
                              , ui(new Ui::Widget)
                          
                          
                          {
                              serial_local = serial_ptr;
                              ui->setupUi(this);
                              fillPortsParameters();
                              serial_local->Test_print_message();
                              //connect(&serial_local->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                          
                          }
                          

                          Now my widget class can use all Serial methods via serial_local object. I believe that is correct now.

                          JonBJ 1 Reply Last reply 26 Aug 2022, 09:21
                          0
                          • L lukutis222
                            26 Aug 2022, 09:11

                            Thank you both very much you are very kind @jsulm @JonB .

                            The last thing I did:

                            On my widget.h, I have declared a Serial variable that will be local for the Widget class but it will point to the global Serial class object.

                            QT_BEGIN_NAMESPACE
                            namespace Ui { class Widget; }
                            QT_END_NAMESPACE
                            
                            
                            
                            class Widget : public QWidget
                            {
                                Q_OBJECT
                            
                            public:
                            
                            
                                Serial* serial_local; // this will be "local" instance of the Serial object that will point to the global Serial object.
                            
                            
                                Widget(Serial* serial_ptr = nullptr, QWidget *parent = nullptr);
                                ~Widget();
                            
                            
                            

                            And in my widget class constructor, I assign it to the variable that I pass in my main.cpp:

                            Widget::Widget(Serial* serial_ptr,QWidget *parent)
                                : QWidget(parent)
                                , ui(new Ui::Widget)
                            
                            
                            {
                                serial_local = serial_ptr;
                                ui->setupUi(this);
                                fillPortsParameters();
                                serial_local->Test_print_message();
                                //connect(&serial_local->serial_connection, &QSerialPort::readyRead, this, &Widget::readData);
                            
                            }
                            

                            Now my widget class can use all Serial methods via serial_local object. I believe that is correct now.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 26 Aug 2022, 09:21 last edited by JonB
                            #13

                            @lukutis222
                            You are getting there, but I would suggest you make an alteration.

                            As I wrote earlier

                            It requires (i'm guessing that will be your intention for what you are writing) some Serial* for its first argument

                            Note the word "requires". That is why I did not suggest/agree with @jsulm's

                            Or simply pass nullptr for the first parameter...

                            That "works" (i.e. compiles), but may not be the right solution. Sure enough, your code goes

                            serial_local = serial_ptr;
                            serial_local->Test_print_message();
                            

                            But you have allowed the serial_ptr parameter to default to nullptr. What will happen in your code if that is the case? Your whole intention is that this class must be given a Serial * to use, right (else it's a pointless class)?

                            Consequently, do not make it optional/default to nullptr. I said earlier the best signature is:

                            Widget(Serial* serial_ptr, QWidget *parent = nullptr)

                            [or even Serial &serial , depending]

                            Only make parameters optional/have a default if they really can be absent or have a sensible default, else make them mandatory.

                            L 1 Reply Last reply 26 Aug 2022, 09:57
                            0
                            • JonBJ JonB
                              26 Aug 2022, 09:21

                              @lukutis222
                              You are getting there, but I would suggest you make an alteration.

                              As I wrote earlier

                              It requires (i'm guessing that will be your intention for what you are writing) some Serial* for its first argument

                              Note the word "requires". That is why I did not suggest/agree with @jsulm's

                              Or simply pass nullptr for the first parameter...

                              That "works" (i.e. compiles), but may not be the right solution. Sure enough, your code goes

                              serial_local = serial_ptr;
                              serial_local->Test_print_message();
                              

                              But you have allowed the serial_ptr parameter to default to nullptr. What will happen in your code if that is the case? Your whole intention is that this class must be given a Serial * to use, right (else it's a pointless class)?

                              Consequently, do not make it optional/default to nullptr. I said earlier the best signature is:

                              Widget(Serial* serial_ptr, QWidget *parent = nullptr)

                              [or even Serial &serial , depending]

                              Only make parameters optional/have a default if they really can be absent or have a sensible default, else make them mandatory.

                              L Offline
                              L Offline
                              lukutis222
                              wrote on 26 Aug 2022, 09:57 last edited by
                              #14

                              @JonB That makes sense. Thank you.

                              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