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. public function to set lineedit in mainwindow
Forum Update on Monday, May 27th 2025

public function to set lineedit in mainwindow

Scheduled Pinned Locked Moved Solved General and Desktop
publiclineeditembedded linuxfunction
28 Posts 6 Posters 5.7k 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
    TMJJ001
    wrote on 30 May 2018, 18:31 last edited by
    #11

    Ooooh!!

    Very very good explaination. Now I know what I did wrong.
    Ok I will try to solve the issue the good way like you said ;).

    Thanks in advance,
    I will comeback later to give some feedback.

    1 Reply Last reply
    0
    • P Pablo J. Rogina
      30 May 2018, 13:33

      @TMJJ001 said in public function to set lineedit in mainwindow:

      qDebug()<<"send to ui";
      MainWindow mainw;
      mainw.tspVisualize(reply[0],reply[1],reply[2],reply[3],reply[4],reply[5],reply[6],reply[7],reply[8],reply[9],reply[10]);
      

      So first of all down below in tsp.cpp I call my "tspVisualize". Then I get the output only in my terminal via the qDebug().
      But when I call "void MainWindow::on_SAButton_clicked()" at the bottom of mainwindow.cpp which then calls "tspVisualize", the lineedits get set. SO the reason is probably that the function gets called from another class. Which is strange.

      It's not strange! :-) it's just simply logic!
      The object mainw you create in TSP::can_tsp() is NOT the same object you have already created in your application which holds the UI. That's why on_SAButton_clicked() does indeed shows the test/fake values.

      So now that the problem has been diagnosed, let's try to solve it. I see two ways here:

      1. Easier but not very good programming as you are tightly coupling TSP with MainWindow: use the parent object you have in TSP constructor to call tspVisualize from it. Something like this pseudo-code:
      TSP::tsp_can(...)
      {
          ...
          qDebug()<<"send to ui";
          parentWidget()->tspVisualize(reply[0],...);
      }
      

      and then in MainWindow:

      ...
      void MainWindow::on_searchButton_clicked()
      {
          TSP tsp_run(this);
          tsp_run.can_tsp(ui->comboBox->currentText());
          ui->Terminal_textEdit->appendPlainText("Start reading sensor");
      }
      ...
      
      1. A little more complex but more elegant and easier to maintain, aligned with Qt way of doing things:
      • Pack all your parameters into a struct (or class) so they're easier to handle. Even if you need to add a new value, you add it to the struct and all the interfaces (calls to methods remain the same, you hide the implementation!)
      • Add a signal to class TSP. You didn't show your tsp.h file but I assume you're using macro Q_OBJECT in class definition. If so then add something like this:
      ...
      signals:
            readingAvailable(struct valuesFromCan);
      ...
      

      In MainWindow class connect the TSP signal with the method where you'll be handling such values. You may need to move the TSP object as member of your class (and a pointer to), not created in a method so when signal is emitted the object is still alive.

      MainWindow::MainWindow(...)
      {
      ...
          tsp_run = new TSP(this);
          connect(tsp_run, TSP::readingsAvailable, this, MainWindow::tsp_visualize);
      ...
      }
      

      Spoiler alert: code may not work as is, you need to provide proper syntax. it's just to try to show the idea.

      T Offline
      T Offline
      TMJJ001
      wrote on 31 May 2018, 06:29 last edited by
      #12

      @Pablo-J.-Rogina
      Hi,

      I'm using the macro Q_OBJECT indeed.

      Now I get a bunch an error on the signal.

      SO what I did:

      tsp.h

      #ifndef TSP_H
      #define TSP_H
      
      #include <QObject>
      #include <QMessageBox>
      #include "mainwindow.h"
      #include <QDebug>
      
      class TSP : public QObject
      {
          Q_OBJECT
      public:
          explicit TSP(QObject *parent = 0);
          ~TSP();
      	
      	struct valuesFromCanSensor{		// Set structure to send data to mainwindow.cpp
              int can_id;
              int can_DT;
              int can_DN;
              int can_SN;
              int can_S;
              int can_HW;
              int can_SW;
              int can_PV;
              int can_OP;
              int can_M;
              int can_B;
          };
      
      signals:
          void readingAvailable(struct valuesFromCanSensor candatasend);			// Signal which sends the data 
      
      public slots:
          void can_tsp(QString cranT);
         
      
      private:
          struct valuesFromCanSensor candata;		// initialize a new structure to save data into and send later on
      
      };
      
      #endif // TSP_H
      

      tsp.cpp

      #include "tsp.h"
      #include "canbus_socket.h"
      #include <linux/can.h>
      #include <linux/can/raw.h>
      
      TSP::TSP(QObject *parent) : QObject(parent)
      {
      
      }
      
      TSP::~TSP()
      {
      
      }
      QString idc, d0c, d1c, d2c, d3c, d4c, d5c, d6c, d7c;
      int question=0;
      
      void TSP::can_tsp_received(QString id, QString d0, QString d1, QString d2, QString d3, QString d4, QString d5, QString d6, QString d7)
      {
          idc=id;   d0c=d0;   d1c=d1;     d2c=d2;     d3c=d3;     d4c=d4;     d5c=d5;     d6c=d6;     d7c=d7;
      }
      
      void TSP::can_tsp(QString cranT)
      {
      
          bool convStatus;
          Canbus_Socket canbus;
      
          int bdr;
          if(cranT=="CC2800-1")
          {
              bdr = 250000;
          }
      
          canbus.open_port("can0",0);                       // init canbus
      
          /***********************************************/
          /*Set the first message into struct and send it*/
          /***********************************************/
          struct can_frame ncandata;      /***************/
          ncandata.can_dlc=2;             /***************/
          ncandata.can_id= 0;             /***************/
          ncandata.data[0]= 01;           /***************/
          ncandata.data[1]=0;             /***************/
          canbus.send_port(&ncandata);    /***************/
          /***********************************************/
          /***********************************************/
      
          qDebug()<<"Start messages on canbus";
      
          int loop = 1;
          int *msgn;
          while(loop==1)
          {
              msgn = canbus.read_port();
              qDebug()<< "id = " << msgn[1];
             if(msgn[1]!=0)
             {
                  qDebug()<< "ID known: " << (msgn[1]-384);
                  loop=0;
                  canbus.close_port();
             }
          }
          qDebug()<< "out of ID loop";
          canbus.open_port("can0",(1408+msgn[1])-384);
          qDebug()<< "port closed and new filter set";
      
      
          while(question<10)
          {
              int fid[2];
              //qDebug()<< "Start switch with question: " + QString::number(question,10);
              switch(question)
              {
                  case(0):	fid[0] =0;          fid[1] =32;     break;	// CAN ID of sensor
                  case(1):    fid[0] =0;          fid[1] =16;     break;	// CAN device type
                  case(2):    fid[0] =8;          fid[1] =16;     break;	// CAN device name
                  case(3):    fid[0] =11;         fid[1] =101;	break;	// CAN serial number
                  case(4):    fid[0] =0;          fid[1] =101;	break;	// CAN status
                  case(5):	fid[0] =9;          fid[1] =16;     break;	// HW ver`on
                  case(6):	fid[0] =10;         fid[1] =16;     break;	// SW version
                  case(7):	fid[0] =7;          fid[1] =101;	break;	// Profile version
                  case(8):	fid[0] =0;          fid[1] =96;     break;	// Operating parameters
                  case(9):	fid[0] =1;          fid[1] =96;     break;	// Measuring .units/revolution
                  case(10):   fid[0] =1;          fid[1] =32;     break;	// Bittiming
              }
      
              /***********************************************/
              /*Set the second message into struct and send it*/
              /***********************************************//***************/
              ncandata.can_dlc=8;                    /***************/
              ncandata.can_id= 1536 + msgn[1]-384;   /***************/
              ncandata.data[0]= 64;                  /***************/
              ncandata.data[1]=fid[0];               /***************/
              ncandata.data[2]=fid[1];               /***************/
              ncandata.data[3]=0;                    /***************/
              ncandata.data[4]=0;                    /***************/
              ncandata.data[5]=0;                    /***************/
              ncandata.data[6]=0;                    /***************/
              ncandata.data[7]=0;                    /***************/
              canbus.send_port(&ncandata);           /***************/
              /***********************************************/
              /***********************************************/
      
              int *msgn2 = canbus.read_port();
              convStatus=false;
              QString number = QString::number(msgn2[4],16);
              QString number1 = QString::number(msgn2[3],16);
      
              QString setting0 = QString::number(msgn2[9],16);
              QString setting1 = QString::number(msgn2[8],16);
              QString setting2 = QString::number(msgn2[7],16);
              QString setting3 = QString::number(msgn2[6],16);
      
      
              if(number.length()<2)   number = "0" + number;                  // Make sure you have 2 numbers in your hex value
              if(number1.length()<2)   number1 = "0" + number1;
      
              if(setting0.length()<2)   setting0 = "0" + setting0;
              if(setting1.length()<2)   setting1 = "0" + setting1;
              if(setting2.length()<2)   setting2 = "0" + setting2;
              if(setting3.length()<2)   setting3 = "0" + setting3;
      
      
              int idr = (number+number1).toUInt(&convStatus,16);
              qDebug() << QString::number(idr);
      
              int frep = (setting0+setting1+setting2+setting3).toUInt(&convStatus,16);
              qDebug() << "frep = "<< QString::number(frep);
      
              switch(idr)
              {
                  case(8192): candata.can_id = frep;          question=1;     break;	// CANid
                  case(4096): candata.can_DT = frep;          question=2;     break;	// Device type
                  case(4104): candata.can_DN = frep;          question=3;     break;	// Device name
                  case(25867):candata.can_SN = frep;          question=4;     break;	// SN
                  case(25856):candata.can_S = frep;           question=5;     break;	// Status
                  case(4105): candata.can_HW = frep;          question=6;     break;	// HW version
                  case(4106): candata.can_SW = frep;          question=7;     break;	// SW version
                  case(25863):candata.can_PV = frep;          question=8;     break;	// Profile version
                  case(24576):candata.can_OP = frep;          question=9;     break;	// Operating parameters
                  case(24577):candata.can_M = frep;           question=10;    break;	// Measuring units/revolution
                  case(8193): candata.can_B = frep;           question=11;    break;	// bittiming
              }
      
          }
      
          emit(readingAvailable(candata));
      
      }
      

      So what I did in the mainwindow.h

      
      public slots:
          void tspVisualize(valuesFromCanSensor candata);
      
      
      

      mainwindow.cpp

      void MainWindow::on_searchButton_clicked()
      {
          TSP tsp_run(this);
          tsp_run.can_tsp(ui->comboBox->currentText());
          connect(tsp_run,TSP::readingAvailable,this,MainWindow::tspVisualize);
      }
      
      void MainWindow::tspVisualize(struct valuesFromCanSensor candata)
      {
          qDebug()<< "ID " << candata.can_id;
      }
      

      First of all, I don't know a lot of structures. I searched a few times for it, but I get always affraid of it ;). I though I understood it when I was using socket_can, where I send a structure to can_socket.cpp to send data on the bus. But now I don't know how to do it.

      When I try to run this I get following error:

      tsp.h:31: error: 'void TSP::readingAvailable(TSP::valuesFromCanSensor)' is protected
           void readingAvailable(struct valuesFromCanSensor candatasend);
                   ^
      
      mainwindow.cpp:486: error: within this context
      connect(tsp_run,TSP::readingAvailable,this,MainWindow::tspVisualize);
                                               ^
      
      
      mainwindow.cpp:486: error: invalid use of non-static member function 'void TSP::readingAvailable(TSP::valuesFromCanSensor)'
      
      mainwindow.cpp:-1: In member function 'void MainWindow::tspVisualize(valuesFromCanSensor*)':
      
      
      
      

      Any Idea what I;m doing wrong?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 31 May 2018, 21:46 last edited by
        #13

        You are missing the & before the functions in the connect statement.

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

        T 1 Reply Last reply 1 Jun 2018, 06:58
        0
        • S SGaist
          31 May 2018, 21:46

          You are missing the & before the functions in the connect statement.

          T Offline
          T Offline
          TMJJ001
          wrote on 1 Jun 2018, 06:58 last edited by
          #14

          @SGaist

          Hi,

          Thanks, I indeed forgot those. But this didn't solve the problem.
          I still get:

          void  TSP::readingAvailable(TSP::valuesFromCanSensor) is protected
          

          No Idea what this means.

          Kind regards

          M 1 Reply Last reply 1 Jun 2018, 07:05
          0
          • T TMJJ001
            1 Jun 2018, 06:58

            @SGaist

            Hi,

            Thanks, I indeed forgot those. But this didn't solve the problem.
            I still get:

            void  TSP::readingAvailable(TSP::valuesFromCanSensor) is protected
            

            No Idea what this means.

            Kind regards

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 1 Jun 2018, 07:05 last edited by
            #15

            @TMJJ001
            hi

            • void TSP::readingAvailable(TSP::valuesFromCanSensor) is protected

            means that in the TSP class its listed under protected.
            that means nobody from outside can call it.

            So are you sure that is the function you are meant to call ?
            who made the TSP class ?

            T 1 Reply Last reply 1 Jun 2018, 07:50
            0
            • M mrjj
              1 Jun 2018, 07:05

              @TMJJ001
              hi

              • void TSP::readingAvailable(TSP::valuesFromCanSensor) is protected

              means that in the TSP class its listed under protected.
              that means nobody from outside can call it.

              So are you sure that is the function you are meant to call ?
              who made the TSP class ?

              T Offline
              T Offline
              TMJJ001
              wrote on 1 Jun 2018, 07:50 last edited by TMJJ001 6 Jan 2018, 07:51
              #16

              @mrjj

              Thanks for the reply,

              I made the class myself.

              void readingAvailable(struct valuesFromCanSensor candatasend)
              

              this is the signal I emit in the tsp.cpp . In my tsp.h this function is under "signals:".
              (As in the above code.)

              This signal should call the function tspVisualize.

              So I don't get it why it is protected.

              M 1 Reply Last reply 1 Jun 2018, 08:25
              0
              • T TMJJ001
                1 Jun 2018, 07:50

                @mrjj

                Thanks for the reply,

                I made the class myself.

                void readingAvailable(struct valuesFromCanSensor candatasend)
                

                this is the signal I emit in the tsp.cpp . In my tsp.h this function is under "signals:".
                (As in the above code.)

                This signal should call the function tspVisualize.

                So I don't get it why it is protected.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 1 Jun 2018, 08:25 last edited by
                #17

                @TMJJ001

                its listed under protected in the .h file.

                T 1 Reply Last reply 1 Jun 2018, 08:34
                0
                • M mrjj
                  1 Jun 2018, 08:25

                  @TMJJ001

                  its listed under protected in the .h file.

                  T Offline
                  T Offline
                  TMJJ001
                  wrote on 1 Jun 2018, 08:34 last edited by
                  #18

                  @mrjj

                  Sorry but I don't see it. How is it under protected in the .h files?
                  It is under "signals:", how is it then under protected.
                  Looks like I'm missing something some thing big times!!

                  Kind regards.

                  M 1 Reply Last reply 1 Jun 2018, 08:45
                  0
                  • T TMJJ001
                    1 Jun 2018, 08:34

                    @mrjj

                    Sorry but I don't see it. How is it under protected in the .h files?
                    It is under "signals:", how is it then under protected.
                    Looks like I'm missing something some thing big times!!

                    Kind regards.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 1 Jun 2018, 08:45 last edited by mrjj 6 Jan 2018, 08:45
                    #19

                    @TMJJ001

                    or maybe i do, but you are not showing any code so im just guessing. :)
                    its normally what message means.

                    T 1 Reply Last reply 1 Jun 2018, 09:10
                    0
                    • M mrjj
                      1 Jun 2018, 08:45

                      @TMJJ001

                      or maybe i do, but you are not showing any code so im just guessing. :)
                      its normally what message means.

                      T Offline
                      T Offline
                      TMJJ001
                      wrote on 1 Jun 2018, 09:10 last edited by
                      #20

                      @mrjj

                      Ok sorry, I will post my code again:

                      So Lets start with the mainwindow.h

                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H
                      
                      #include <QMainWindow>
                      #include <tsp.h>
                      #include <QMessageBox>
                      
                      namespace Ui {
                      class MainWindow;
                      }
                      
                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit MainWindow(QWidget *parent = 0);
                          ~MainWindow();
                      
                      public slots:
                          void tspVisualize(struct valuesFromCanSensor candata); //This is the slot which get called after signal readingAvailable.
                      
                      private slots:
                      
                      private:
                          Ui::MainWindow *ui;
                      
                      };
                      
                      #endif // MAINWINDOW_H
                      

                      Next I will show the mainwindow.cpp file

                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include "canbus_socket.h"
                      #include "tsp.h"
                      
                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      }
                      
                      MainWindow::~MainWindow()
                      {
                          delete ui;
                      }
                      void MainWindow::on_searchButton_clicked()
                      {
                          TSP tsp_run(this);
                      	ui->Terminal_textEdit->appendPlainText("Start reading sensor");
                          tsp_run.can_tsp(ui->comboBox->currentText());							// Here I call the function can_tsp from the class TSP
                          connect(tsp_run,&TSP::readingAvailable,this,&MainWindow::tspVisualize);	// Here I connected the signal readingAvailable to the visualization.
                      }																			// readingAvailable will be called at the end of can_tsp and will pass a structure to tspVisualize
                      
                      
                      
                      void MainWindow::tspVisualize(struct valuesFromCanSensor candata)
                      {
                          qDebug()<< "ID " << candata.can_id;
                      	ui->DT_lineEdit->setText(QString::number(candata.can_id,16));
                      
                      	// ... and visualize all other parameters from the passed structure
                      
                      }
                      

                      next tsp.h

                      #ifndef TSP_H
                      #define TSP_H
                      
                      #include <QObject>
                      #include <QMessageBox>
                      #include "mainwindow.h"
                      #include <QDebug>
                      
                      class TSP : public QObject
                      {
                          Q_OBJECT
                      public:
                          explicit TSP(QObject *parent = 0);
                          ~TSP();
                      	
                      	struct valuesFromCanSensor{		// Set structure to send data to mainwindow.cpp
                              int can_id;
                              int can_DT;
                              int can_DN;
                              int can_SN;
                              int can_S;
                              int can_HW;
                              int can_SW;
                              int can_PV;
                              int can_OP;
                              int can_M;
                              int can_B;
                          };
                      
                      signals:
                          void readingAvailable(struct valuesFromCanSensor candatasend);			// Signal which sends the data 
                      
                      public slots:
                          void can_tsp(QString cranT);
                         
                      
                      private:
                          struct valuesFromCanSensor candata;		// initialize a new structure to save data into and send later on
                      
                      };
                      
                      #endif // TSP_H
                      

                      last tsp.cpp

                      
                      #include "tsp.h"
                      #include "canbus_socket.h"
                      #include <linux/can.h>
                      #include <linux/can/raw.h>
                      
                      TSP::TSP(QObject *parent) : QObject(parent)
                      {
                      
                      }
                      
                      TSP::~TSP()
                      {
                      
                      }
                      
                      int question=0;
                      
                      
                      void TSP::can_tsp(QString cranT)
                      {
                      
                          bool convStatus;
                          Canbus_Socket canbus;
                      
                          int bdr;
                          if(cranT=="CC2800-1")
                          {
                              bdr = 250000;
                          }
                      
                          canbus.open_port("can0",0);                       // init canbus
                      
                          /***********************************************/
                          /*Set the first message into struct and send it*/
                          /***********************************************/
                          struct can_frame ncandata;      /***************/
                          ncandata.can_dlc=2;             /***************/
                          ncandata.can_id= 0;             /***************/
                          ncandata.data[0]= 01;           /***************/
                          ncandata.data[1]=0;             /***************/
                          canbus.send_port(&ncandata);    /***************/
                          /***********************************************/
                          /***********************************************/
                      
                          qDebug()<<"Start messages on canbus";
                      
                          int loop = 1;
                          int *msgn;
                          while(loop==1)
                          {
                              msgn = canbus.read_port();
                              qDebug()<< "id = " << msgn[1];
                             if(msgn[1]!=0)
                             {
                                  qDebug()<< "ID known: " << (msgn[1]-384);
                                  loop=0;
                                  canbus.close_port();
                             }
                          }
                          qDebug()<< "out of ID loop";
                          canbus.open_port("can0",(1408+msgn[1])-384);
                          qDebug()<< "port closed and new filter set";
                      
                      
                          while(question<10)
                          {
                              int fid[2];
                              //qDebug()<< "Start switch with question: " + QString::number(question,10);
                              switch(question)
                              {
                                  case(0):	fid[0] =0;          fid[1] =32;     break;	// CAN ID of sensor
                                  case(1):    fid[0] =0;          fid[1] =16;     break;	// CAN device type
                                  case(2):    fid[0] =8;          fid[1] =16;     break;	// CAN device name
                                  case(3):    fid[0] =11;         fid[1] =101;	break;	// CAN serial number
                                  case(4):    fid[0] =0;          fid[1] =101;	break;	// CAN status
                                  case(5):	fid[0] =9;          fid[1] =16;     break;	// HW ver`on
                                  case(6):	fid[0] =10;         fid[1] =16;     break;	// SW version
                                  case(7):	fid[0] =7;          fid[1] =101;	break;	// Profile version
                                  case(8):	fid[0] =0;          fid[1] =96;     break;	// Operating parameters
                                  case(9):	fid[0] =1;          fid[1] =96;     break;	// Measuring .units/revolution
                                  case(10):   fid[0] =1;          fid[1] =32;     break;	// Bittiming
                              }
                      
                              /***********************************************/
                              /*Set the second message into struct and send it*/
                              /***********************************************//***************/
                              ncandata.can_dlc=8;                    /***************/
                              ncandata.can_id= 1536 + msgn[1]-384;   /***************/
                              ncandata.data[0]= 64;                  /***************/
                              ncandata.data[1]=fid[0];               /***************/
                              ncandata.data[2]=fid[1];               /***************/
                              ncandata.data[3]=0;                    /***************/
                              ncandata.data[4]=0;                    /***************/
                              ncandata.data[5]=0;                    /***************/
                              ncandata.data[6]=0;                    /***************/
                              ncandata.data[7]=0;                    /***************/
                              canbus.send_port(&ncandata);           /***************/
                              /***********************************************/
                              /***********************************************/
                      
                              int *msgn2 = canbus.read_port();
                              convStatus=false;
                              QString number = QString::number(msgn2[4],16);
                              QString number1 = QString::number(msgn2[3],16);
                      
                              QString setting0 = QString::number(msgn2[9],16);
                              QString setting1 = QString::number(msgn2[8],16);
                              QString setting2 = QString::number(msgn2[7],16);
                              QString setting3 = QString::number(msgn2[6],16);
                      
                      
                              if(number.length()<2)   number = "0" + number;                  // Make sure you have 2 numbers in your hex value
                              if(number1.length()<2)   number1 = "0" + number1;
                      
                              if(setting0.length()<2)   setting0 = "0" + setting0;
                              if(setting1.length()<2)   setting1 = "0" + setting1;
                              if(setting2.length()<2)   setting2 = "0" + setting2;
                              if(setting3.length()<2)   setting3 = "0" + setting3;
                      
                      
                              int idr = (number+number1).toUInt(&convStatus,16);
                              qDebug() << QString::number(idr);
                      
                              int frep = (setting0+setting1+setting2+setting3).toUInt(&convStatus,16);
                              qDebug() << "frep = "<< QString::number(frep);
                      
                              switch(idr)
                              {
                                  case(8192): candata.can_id = frep;          question=1;     break;	// CANid
                                  case(4096): candata.can_DT = frep;          question=2;     break;	// Device type
                                  case(4104): candata.can_DN = frep;          question=3;     break;	// Device name
                                  case(25867):candata.can_SN = frep;          question=4;     break;	// SN
                                  case(25856):candata.can_S = frep;           question=5;     break;	// Status
                                  case(4105): candata.can_HW = frep;          question=6;     break;	// HW version
                                  case(4106): candata.can_SW = frep;          question=7;     break;	// SW version
                                  case(25863):candata.can_PV = frep;          question=8;     break;	// Profile version
                                  case(24576):candata.can_OP = frep;          question=9;     break;	// Operating parameters
                                  case(24577):candata.can_M = frep;           question=10;    break;	// Measuring units/revolution
                                  case(8193): candata.can_B = frep;           question=11;    break;	// bittiming
                              }
                      
                          }
                      
                          emit(readingAvailable(candata));		// send the signal so the data can be visualized in the mainwindow
                      
                      }
                      
                      
                      M 1 Reply Last reply 1 Jun 2018, 09:59
                      0
                      • T TMJJ001
                        1 Jun 2018, 09:10

                        @mrjj

                        Ok sorry, I will post my code again:

                        So Lets start with the mainwindow.h

                        #ifndef MAINWINDOW_H
                        #define MAINWINDOW_H
                        
                        #include <QMainWindow>
                        #include <tsp.h>
                        #include <QMessageBox>
                        
                        namespace Ui {
                        class MainWindow;
                        }
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            explicit MainWindow(QWidget *parent = 0);
                            ~MainWindow();
                        
                        public slots:
                            void tspVisualize(struct valuesFromCanSensor candata); //This is the slot which get called after signal readingAvailable.
                        
                        private slots:
                        
                        private:
                            Ui::MainWindow *ui;
                        
                        };
                        
                        #endif // MAINWINDOW_H
                        

                        Next I will show the mainwindow.cpp file

                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        #include "canbus_socket.h"
                        #include "tsp.h"
                        
                        MainWindow::MainWindow(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        void MainWindow::on_searchButton_clicked()
                        {
                            TSP tsp_run(this);
                        	ui->Terminal_textEdit->appendPlainText("Start reading sensor");
                            tsp_run.can_tsp(ui->comboBox->currentText());							// Here I call the function can_tsp from the class TSP
                            connect(tsp_run,&TSP::readingAvailable,this,&MainWindow::tspVisualize);	// Here I connected the signal readingAvailable to the visualization.
                        }																			// readingAvailable will be called at the end of can_tsp and will pass a structure to tspVisualize
                        
                        
                        
                        void MainWindow::tspVisualize(struct valuesFromCanSensor candata)
                        {
                            qDebug()<< "ID " << candata.can_id;
                        	ui->DT_lineEdit->setText(QString::number(candata.can_id,16));
                        
                        	// ... and visualize all other parameters from the passed structure
                        
                        }
                        

                        next tsp.h

                        #ifndef TSP_H
                        #define TSP_H
                        
                        #include <QObject>
                        #include <QMessageBox>
                        #include "mainwindow.h"
                        #include <QDebug>
                        
                        class TSP : public QObject
                        {
                            Q_OBJECT
                        public:
                            explicit TSP(QObject *parent = 0);
                            ~TSP();
                        	
                        	struct valuesFromCanSensor{		// Set structure to send data to mainwindow.cpp
                                int can_id;
                                int can_DT;
                                int can_DN;
                                int can_SN;
                                int can_S;
                                int can_HW;
                                int can_SW;
                                int can_PV;
                                int can_OP;
                                int can_M;
                                int can_B;
                            };
                        
                        signals:
                            void readingAvailable(struct valuesFromCanSensor candatasend);			// Signal which sends the data 
                        
                        public slots:
                            void can_tsp(QString cranT);
                           
                        
                        private:
                            struct valuesFromCanSensor candata;		// initialize a new structure to save data into and send later on
                        
                        };
                        
                        #endif // TSP_H
                        

                        last tsp.cpp

                        
                        #include "tsp.h"
                        #include "canbus_socket.h"
                        #include <linux/can.h>
                        #include <linux/can/raw.h>
                        
                        TSP::TSP(QObject *parent) : QObject(parent)
                        {
                        
                        }
                        
                        TSP::~TSP()
                        {
                        
                        }
                        
                        int question=0;
                        
                        
                        void TSP::can_tsp(QString cranT)
                        {
                        
                            bool convStatus;
                            Canbus_Socket canbus;
                        
                            int bdr;
                            if(cranT=="CC2800-1")
                            {
                                bdr = 250000;
                            }
                        
                            canbus.open_port("can0",0);                       // init canbus
                        
                            /***********************************************/
                            /*Set the first message into struct and send it*/
                            /***********************************************/
                            struct can_frame ncandata;      /***************/
                            ncandata.can_dlc=2;             /***************/
                            ncandata.can_id= 0;             /***************/
                            ncandata.data[0]= 01;           /***************/
                            ncandata.data[1]=0;             /***************/
                            canbus.send_port(&ncandata);    /***************/
                            /***********************************************/
                            /***********************************************/
                        
                            qDebug()<<"Start messages on canbus";
                        
                            int loop = 1;
                            int *msgn;
                            while(loop==1)
                            {
                                msgn = canbus.read_port();
                                qDebug()<< "id = " << msgn[1];
                               if(msgn[1]!=0)
                               {
                                    qDebug()<< "ID known: " << (msgn[1]-384);
                                    loop=0;
                                    canbus.close_port();
                               }
                            }
                            qDebug()<< "out of ID loop";
                            canbus.open_port("can0",(1408+msgn[1])-384);
                            qDebug()<< "port closed and new filter set";
                        
                        
                            while(question<10)
                            {
                                int fid[2];
                                //qDebug()<< "Start switch with question: " + QString::number(question,10);
                                switch(question)
                                {
                                    case(0):	fid[0] =0;          fid[1] =32;     break;	// CAN ID of sensor
                                    case(1):    fid[0] =0;          fid[1] =16;     break;	// CAN device type
                                    case(2):    fid[0] =8;          fid[1] =16;     break;	// CAN device name
                                    case(3):    fid[0] =11;         fid[1] =101;	break;	// CAN serial number
                                    case(4):    fid[0] =0;          fid[1] =101;	break;	// CAN status
                                    case(5):	fid[0] =9;          fid[1] =16;     break;	// HW ver`on
                                    case(6):	fid[0] =10;         fid[1] =16;     break;	// SW version
                                    case(7):	fid[0] =7;          fid[1] =101;	break;	// Profile version
                                    case(8):	fid[0] =0;          fid[1] =96;     break;	// Operating parameters
                                    case(9):	fid[0] =1;          fid[1] =96;     break;	// Measuring .units/revolution
                                    case(10):   fid[0] =1;          fid[1] =32;     break;	// Bittiming
                                }
                        
                                /***********************************************/
                                /*Set the second message into struct and send it*/
                                /***********************************************//***************/
                                ncandata.can_dlc=8;                    /***************/
                                ncandata.can_id= 1536 + msgn[1]-384;   /***************/
                                ncandata.data[0]= 64;                  /***************/
                                ncandata.data[1]=fid[0];               /***************/
                                ncandata.data[2]=fid[1];               /***************/
                                ncandata.data[3]=0;                    /***************/
                                ncandata.data[4]=0;                    /***************/
                                ncandata.data[5]=0;                    /***************/
                                ncandata.data[6]=0;                    /***************/
                                ncandata.data[7]=0;                    /***************/
                                canbus.send_port(&ncandata);           /***************/
                                /***********************************************/
                                /***********************************************/
                        
                                int *msgn2 = canbus.read_port();
                                convStatus=false;
                                QString number = QString::number(msgn2[4],16);
                                QString number1 = QString::number(msgn2[3],16);
                        
                                QString setting0 = QString::number(msgn2[9],16);
                                QString setting1 = QString::number(msgn2[8],16);
                                QString setting2 = QString::number(msgn2[7],16);
                                QString setting3 = QString::number(msgn2[6],16);
                        
                        
                                if(number.length()<2)   number = "0" + number;                  // Make sure you have 2 numbers in your hex value
                                if(number1.length()<2)   number1 = "0" + number1;
                        
                                if(setting0.length()<2)   setting0 = "0" + setting0;
                                if(setting1.length()<2)   setting1 = "0" + setting1;
                                if(setting2.length()<2)   setting2 = "0" + setting2;
                                if(setting3.length()<2)   setting3 = "0" + setting3;
                        
                        
                                int idr = (number+number1).toUInt(&convStatus,16);
                                qDebug() << QString::number(idr);
                        
                                int frep = (setting0+setting1+setting2+setting3).toUInt(&convStatus,16);
                                qDebug() << "frep = "<< QString::number(frep);
                        
                                switch(idr)
                                {
                                    case(8192): candata.can_id = frep;          question=1;     break;	// CANid
                                    case(4096): candata.can_DT = frep;          question=2;     break;	// Device type
                                    case(4104): candata.can_DN = frep;          question=3;     break;	// Device name
                                    case(25867):candata.can_SN = frep;          question=4;     break;	// SN
                                    case(25856):candata.can_S = frep;           question=5;     break;	// Status
                                    case(4105): candata.can_HW = frep;          question=6;     break;	// HW version
                                    case(4106): candata.can_SW = frep;          question=7;     break;	// SW version
                                    case(25863):candata.can_PV = frep;          question=8;     break;	// Profile version
                                    case(24576):candata.can_OP = frep;          question=9;     break;	// Operating parameters
                                    case(24577):candata.can_M = frep;           question=10;    break;	// Measuring units/revolution
                                    case(8193): candata.can_B = frep;           question=11;    break;	// bittiming
                                }
                        
                            }
                        
                            emit(readingAvailable(candata));		// send the signal so the data can be visualized in the mainwindow
                        
                        }
                        
                        
                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 1 Jun 2018, 09:59 last edited by
                        #21

                        @TMJJ001

                        Hi
                        ok thats odd. looks ok from quick look.
                        must test in compiler to know for sure

                        anyway, i say

                        void MainWindow::on_searchButton_clicked()
                        {
                        TSP tsp_run(this);
                        ui->Terminal_textEdit->appendPlainText("Start reading sensor");
                        tsp_run.can_tsp(ui->comboBox->currentText()); // Here I call the function can_tsp from the class TSP
                        connect(tsp_run,&TSP::readingAvailable,this,&MainWindow::tspVisualize); // Here I connected the signal readingAvailable to the visualization.
                        }

                        TSP tsp_run(this); << wont this be DELETED long before it can ever say readingAvailable ?
                        its deleted as soon as on_searchButton_clicked ends since its local stack variable.

                        T 1 Reply Last reply 1 Jun 2018, 11:26
                        1
                        • M mrjj
                          1 Jun 2018, 09:59

                          @TMJJ001

                          Hi
                          ok thats odd. looks ok from quick look.
                          must test in compiler to know for sure

                          anyway, i say

                          void MainWindow::on_searchButton_clicked()
                          {
                          TSP tsp_run(this);
                          ui->Terminal_textEdit->appendPlainText("Start reading sensor");
                          tsp_run.can_tsp(ui->comboBox->currentText()); // Here I call the function can_tsp from the class TSP
                          connect(tsp_run,&TSP::readingAvailable,this,&MainWindow::tspVisualize); // Here I connected the signal readingAvailable to the visualization.
                          }

                          TSP tsp_run(this); << wont this be DELETED long before it can ever say readingAvailable ?
                          its deleted as soon as on_searchButton_clicked ends since its local stack variable.

                          T Offline
                          T Offline
                          TMJJ001
                          wrote on 1 Jun 2018, 11:26 last edited by
                          #22

                          @mrjj

                          Hi,

                          When I run this, it runs completely without issue.
                          I get the outputs in the terminal by "qDebug();", So I think this is fine no?

                          M 1 Reply Last reply 1 Jun 2018, 12:47
                          0
                          • T TMJJ001
                            1 Jun 2018, 11:26

                            @mrjj

                            Hi,

                            When I run this, it runs completely without issue.
                            I get the outputs in the terminal by "qDebug();", So I think this is fine no?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 1 Jun 2018, 12:47 last edited by
                            #23

                            @TMJJ001
                            Ok, sounds ok then. Just looked fishy.
                            does tsp_run.can_tsp send the readingAvailable signal ?

                            T 1 Reply Last reply 1 Jun 2018, 13:36
                            0
                            • M mrjj
                              1 Jun 2018, 12:47

                              @TMJJ001
                              Ok, sounds ok then. Just looked fishy.
                              does tsp_run.can_tsp send the readingAvailable signal ?

                              T Offline
                              T Offline
                              TMJJ001
                              wrote on 1 Jun 2018, 13:36 last edited by
                              #24

                              @mrjj

                              Hi,

                              When I send the signal within my class without I have no problems at all. When I try to connect in the mainwindow I get the previous error. Now I was playing a bit with the connect in the main function. When I did the following I get no error, but the SLOT also doesn't get called.

                              connect(&tsp_run,SIGNAL(readingAvailable(),this,SLOT(tspVisualize());
                              
                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 1 Jun 2018, 13:40 last edited by mrjj 6 Jan 2018, 13:41
                                #25

                                @TMJJ001
                                main function = main.cpp ?
                                when using old syntax, you should check return value to see if it works.

                                qDebug() << "works:" << connect(&tsp_run,SIGNAL(readingAvailable(),this,SLOT(tspVisualize());

                                T 1 Reply Last reply 1 Jun 2018, 13:51
                                0
                                • M mrjj
                                  1 Jun 2018, 13:40

                                  @TMJJ001
                                  main function = main.cpp ?
                                  when using old syntax, you should check return value to see if it works.

                                  qDebug() << "works:" << connect(&tsp_run,SIGNAL(readingAvailable(),this,SLOT(tspVisualize());

                                  T Offline
                                  T Offline
                                  TMJJ001
                                  wrote on 1 Jun 2018, 13:51 last edited by
                                  #26

                                  @mrjj

                                  Sorry in the mainwindow. NOT in the main!
                                  Ok, When I do this I get "works:true". So this works.

                                  But, In the function tspVisualize() I have written:

                                  qDebug()<<"send to mainwindow";
                                  

                                  This I don't see in the terminal. Damn what is happening.

                                  P 1 Reply Last reply 3 Jun 2018, 02:26
                                  0
                                  • T TMJJ001
                                    1 Jun 2018, 13:51

                                    @mrjj

                                    Sorry in the mainwindow. NOT in the main!
                                    Ok, When I do this I get "works:true". So this works.

                                    But, In the function tspVisualize() I have written:

                                    qDebug()<<"send to mainwindow";
                                    

                                    This I don't see in the terminal. Damn what is happening.

                                    P Offline
                                    P Offline
                                    Pablo J. Rogina
                                    wrote on 3 Jun 2018, 02:26 last edited by
                                    #27

                                    @TMJJ001 you may want to check this example

                                    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 4 Jun 2018, 10:00
                                    3
                                    • P Pablo J. Rogina
                                      3 Jun 2018, 02:26

                                      @TMJJ001 you may want to check this example

                                      T Offline
                                      T Offline
                                      TMJJ001
                                      wrote on 4 Jun 2018, 10:00 last edited by
                                      #28

                                      @Pablo-J.-Rogina
                                      Thanks for the time to make example code.
                                      It didn't work straight away, this because I use qt4.8 and you the newer one probably.
                                      So I changed your code to the older syntax to get it working.

                                      I would like to thank you and all others for there help!
                                      Again I learned a lot!

                                      Thanks for helping!

                                      1 Reply Last reply
                                      2

                                      20/28

                                      1 Jun 2018, 09:10

                                      • Login

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