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 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
    
    }
    
    
    mrjjM Offline
    mrjjM 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
    • mrjjM 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?

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

        mrjjM Offline
        mrjjM 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
        • mrjjM 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
          • mrjjM Offline
            mrjjM 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
            • mrjjM 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.

              Pablo J. RoginaP 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.

                Pablo J. RoginaP Offline
                Pablo J. RoginaP 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
                • Pablo J. RoginaP 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

                  21/28

                  1 Jun 2018, 09:59

                  • Login

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