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. New class --> signal and slots
QtWS25 Last Chance

New class --> signal and slots

Scheduled Pinned Locked Moved Solved General and Desktop
connectsignal & slot
7 Posts 5 Posters 2.0k 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.
  • T Offline
    T Offline
    TMJJ
    wrote on last edited by
    #1

    Dear all,

    This is probably a very stupid question, but I can't get my head around it.
    I have a class which is called Canbus. This class is called from within my Mainwindow.cpp

    In the Canbus class I initialize the canbus settings.

    void Canbus::Can_Init()
    {
        /*START CAN0*/
        QString cmd1 = "ip";
        QStringList opt1;
    
        /*ip link set can0 up type can bitrate 250000*/
        opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
        proc1.start(cmd1,opt1);
    
        QString cmd2 = "ifconfig";
        QStringList opt2;
    
        /*ifconfig can0 up*/
        opt2 <<"can0"<< "up";
        proc2.start(cmd2,opt2);
    
        QString cmd3 = "candump";
        QStringList opt3;
        opt3 << "can0";
        proc3.start(cmd3,opt3);
    
    }
    
    
    

    Now I have another function in this class which is called dataReady().
    I wish to do the following:

    connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady()));
    

    I used this before when I was working only into the mainwindow.cpp.
    If I want to connect those to in a class, how shouldI do this?

    The following file is my canbus.h file

    #ifndef CANBUS_H
    #define CANBUS_H
    
    #include <QtGui>
    #include <QProcess>
    #include <QObject>
    class Canbus
    {
         Q_OBJECT
    
    public:
        Canbus();
        ~Canbus();
    
    public slots:
        void dataReady();
    
    private:
        void Can_Init();
        int buttonPushed(void);
    
    
        QProcess proc1;
        QProcess proc2;
        QProcess proc3;
        QProcess proc4;
        QProcess proc5;
        QProcess proc6;
    };
    
    #endif // CANBUS_H
    

    This is my canbus.cpp file

    #include "canbus.h"
    
    
    void Canbus::dataReady()
    {
        while(proc3.bytesAvailable()){
    
            QString in = proc3.readLine();
            QString out;
    
            QString DataString[8];
            QString CANbus;
            QString IDstring[8];
            QString IDString;
            long IDdec[8];
            QString IDhex;
            unsigned long ID;
            QString dataBytes;
            int dataCAN[8];
            QString DataStringL[4];
            short dataCANL[4];
            QString printOut;
            QString regString;
            bool ok;
            int regNumb;
            QString DataHEX;
    
            QRegExp reg("  can([01])  ([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])   \\[([0-9]+)\\]  ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+)");
    
            if(reg.indexIn(in)>=0){
    
    
                regNumb = 11;
                for(int n=0;n<8;n++)
                {
                    regString = "%" + QString::number(regNumb);
                    DataString[n] = regString;
                    DataString[n] = DataString[n].arg(reg.cap(regNumb));
    
                    DataHEX = "0x" + DataString[n];
                    dataCAN[n] = DataHEX.toUInt(&ok, 16);
    
                    regNumb++;
    
                }
    
                if((dataCAN[0])==0x15)
                {
                    printOut = "GO";
    
    
    
                    for (int n=0;n<8;n++)
                    {
                        qDebug() << DataString[n];
    
                    }
                }
                else if ((dataCAN[0])==0x15 & (dataCAN[1])==0x16 & (dataCAN[2])==0x1E & (dataCAN[3])==0xCF & (dataCAN[4])==0x00 & (dataCAN[5])==0x09 & (dataCAN[6])==0x71 & (dataCAN[7])==0x00)
                {
    
    
    
                }
            }
    
    
        }
    }
    
    void Canbus::Can_Init()
    {
        /*START CAN0*/
        QString cmd1 = "ip";
        QStringList opt1;
    
        /*ip link set can0 up type can bitrate 250000*/
        opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
        proc1.start(cmd1,opt1);
    
        QString cmd2 = "ifconfig";
        QStringList opt2;
    
        /*ifconfig can0 up*/
        opt2 <<"can0"<< "up";
        proc2.start(cmd2,opt2);
    
        QString cmd3 = "candump";
        QStringList opt3;
        opt3 << "can0";
        proc3.start(cmd3,opt3);
    
    }
    
    

    So I have no idea on how to connect the signal and the slot in a class. I searched in the documentation of qt but I don't understand it completely.

    Thanks in advance,
    Kind regards,
    Toon Mertens

    Pablo J. RoginaP 1 Reply Last reply
    0
    • T TMJJ

      Dear all,

      This is probably a very stupid question, but I can't get my head around it.
      I have a class which is called Canbus. This class is called from within my Mainwindow.cpp

      In the Canbus class I initialize the canbus settings.

      void Canbus::Can_Init()
      {
          /*START CAN0*/
          QString cmd1 = "ip";
          QStringList opt1;
      
          /*ip link set can0 up type can bitrate 250000*/
          opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
          proc1.start(cmd1,opt1);
      
          QString cmd2 = "ifconfig";
          QStringList opt2;
      
          /*ifconfig can0 up*/
          opt2 <<"can0"<< "up";
          proc2.start(cmd2,opt2);
      
          QString cmd3 = "candump";
          QStringList opt3;
          opt3 << "can0";
          proc3.start(cmd3,opt3);
      
      }
      
      
      

      Now I have another function in this class which is called dataReady().
      I wish to do the following:

      connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady()));
      

      I used this before when I was working only into the mainwindow.cpp.
      If I want to connect those to in a class, how shouldI do this?

      The following file is my canbus.h file

      #ifndef CANBUS_H
      #define CANBUS_H
      
      #include <QtGui>
      #include <QProcess>
      #include <QObject>
      class Canbus
      {
           Q_OBJECT
      
      public:
          Canbus();
          ~Canbus();
      
      public slots:
          void dataReady();
      
      private:
          void Can_Init();
          int buttonPushed(void);
      
      
          QProcess proc1;
          QProcess proc2;
          QProcess proc3;
          QProcess proc4;
          QProcess proc5;
          QProcess proc6;
      };
      
      #endif // CANBUS_H
      

      This is my canbus.cpp file

      #include "canbus.h"
      
      
      void Canbus::dataReady()
      {
          while(proc3.bytesAvailable()){
      
              QString in = proc3.readLine();
              QString out;
      
              QString DataString[8];
              QString CANbus;
              QString IDstring[8];
              QString IDString;
              long IDdec[8];
              QString IDhex;
              unsigned long ID;
              QString dataBytes;
              int dataCAN[8];
              QString DataStringL[4];
              short dataCANL[4];
              QString printOut;
              QString regString;
              bool ok;
              int regNumb;
              QString DataHEX;
      
              QRegExp reg("  can([01])  ([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])   \\[([0-9]+)\\]  ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+)");
      
              if(reg.indexIn(in)>=0){
      
      
                  regNumb = 11;
                  for(int n=0;n<8;n++)
                  {
                      regString = "%" + QString::number(regNumb);
                      DataString[n] = regString;
                      DataString[n] = DataString[n].arg(reg.cap(regNumb));
      
                      DataHEX = "0x" + DataString[n];
                      dataCAN[n] = DataHEX.toUInt(&ok, 16);
      
                      regNumb++;
      
                  }
      
                  if((dataCAN[0])==0x15)
                  {
                      printOut = "GO";
      
      
      
                      for (int n=0;n<8;n++)
                      {
                          qDebug() << DataString[n];
      
                      }
                  }
                  else if ((dataCAN[0])==0x15 & (dataCAN[1])==0x16 & (dataCAN[2])==0x1E & (dataCAN[3])==0xCF & (dataCAN[4])==0x00 & (dataCAN[5])==0x09 & (dataCAN[6])==0x71 & (dataCAN[7])==0x00)
                  {
      
      
      
                  }
              }
      
      
          }
      }
      
      void Canbus::Can_Init()
      {
          /*START CAN0*/
          QString cmd1 = "ip";
          QStringList opt1;
      
          /*ip link set can0 up type can bitrate 250000*/
          opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
          proc1.start(cmd1,opt1);
      
          QString cmd2 = "ifconfig";
          QStringList opt2;
      
          /*ifconfig can0 up*/
          opt2 <<"can0"<< "up";
          proc2.start(cmd2,opt2);
      
          QString cmd3 = "candump";
          QStringList opt3;
          opt3 << "can0";
          proc3.start(cmd3,opt3);
      
      }
      
      

      So I have no idea on how to connect the signal and the slot in a class. I searched in the documentation of qt but I don't understand it completely.

      Thanks in advance,
      Kind regards,
      Toon Mertens

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

      @TMJJ did you take a look at the CAN Bus example already? I guess using the QCanBusDevice and QCanBusFrame classes will simplify your development.
      You can even look at this webinar from ICS

      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

      T 1 Reply Last reply
      2
      • Pablo J. RoginaP Pablo J. Rogina

        @TMJJ did you take a look at the CAN Bus example already? I guess using the QCanBusDevice and QCanBusFrame classes will simplify your development.
        You can even look at this webinar from ICS

        T Offline
        T Offline
        TMJJ
        wrote on last edited by
        #3

        @Pablo-J.-Rogina
        Thanks for the reply. Yes indeed that would be the best way. The issue is that I'm developping this for an embedded system which only support qt4.8.
        So I can't use it.

        That's why I'm setting this up.

        Kind regards

        JonBJ 1 Reply Last reply
        0
        • T TMJJ

          @Pablo-J.-Rogina
          Thanks for the reply. Yes indeed that would be the best way. The issue is that I'm developping this for an embedded system which only support qt4.8.
          So I can't use it.

          That's why I'm setting this up.

          Kind regards

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @TMJJ

          So I have no idea on how to connect the signal and the slot in a class

          Maybe I'm missing the point, but why can't you put your connect line into, say, Can_Init()?

          T 1 Reply Last reply
          0
          • JonBJ JonB

            @TMJJ

            So I have no idea on how to connect the signal and the slot in a class

            Maybe I'm missing the point, but why can't you put your connect line into, say, Can_Init()?

            T Offline
            T Offline
            TMJJ
            wrote on last edited by
            #5

            @JonB
            Yes that I tried as well. I did the following :

            void Canbus::Can_Init()
            {
                /*START CAN0*/
                QString cmd1 = "ip";
                QStringList opt1;
            
                /*ip link set can0 up type can bitrate 250000*/
                opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
                proc1.start(cmd1,opt1);
            
                QString cmd2 = "ifconfig";
                QStringList opt2;
            
                /*ifconfig can0 up*/
                opt2 <<"can0"<< "up";
                proc2.start(cmd2,opt2);
            
                QString cmd3 = "candump";
                QStringList opt3;
                opt3 << "can0";
                proc3.start(cmd3,opt3);
                
                QObject::connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady()));
            
            }
            

            So I added it to the bottom. Then I get the following error:

            /home/whooper/Documents/Tractor/Fendt_CAN_seeder/Fendt415_CAN_Seeder_V1_0/canbus.cpp:93: error: no matching function for call to 'QObject::connect(QProcess*, const char [13], Canbus* const, const char [13])'
            

            I have no clue what I'm doing

            aha_1980A 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Canbus must derive from QObject.
              And you should do your connect before QProcess::start(). And you should wait for process to finish until proceeding further.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • T TMJJ

                @JonB
                Yes that I tried as well. I did the following :

                void Canbus::Can_Init()
                {
                    /*START CAN0*/
                    QString cmd1 = "ip";
                    QStringList opt1;
                
                    /*ip link set can0 up type can bitrate 250000*/
                    opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000";
                    proc1.start(cmd1,opt1);
                
                    QString cmd2 = "ifconfig";
                    QStringList opt2;
                
                    /*ifconfig can0 up*/
                    opt2 <<"can0"<< "up";
                    proc2.start(cmd2,opt2);
                
                    QString cmd3 = "candump";
                    QStringList opt3;
                    opt3 << "can0";
                    proc3.start(cmd3,opt3);
                    
                    QObject::connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady()));
                
                }
                

                So I added it to the bottom. Then I get the following error:

                /home/whooper/Documents/Tractor/Fendt_CAN_seeder/Fendt415_CAN_Seeder_V1_0/canbus.cpp:93: error: no matching function for call to 'QObject::connect(QProcess*, const char [13], Canbus* const, const char [13])'
                

                I have no clue what I'm doing

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @TMJJ

                I hope you are not going to interpret the output of candump in your application to receive CAN frames!

                SocketCAN provides a userspace API which can be perfectly used from C/C++ programs.

                As SocketCAN is open source, there are lots of examples how to do this, like the SocketCAN plugin of QtSerialBus or the source code of candump.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                4

                • Login

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