public function to set lineedit in mainwindow



  • Hi all,

    I'm having a strange issue. I'm trying to pass variables from a class to the mainwindow. (this works)
    I can show them in the terminal with qDebug().
    although I can't visualize them in there lineedit. When I try run the function from a other pushbutton and give some random values with it, then it works. (Am I getting crazy?). The application runs on embedded linux.

    mainwindow.h

    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void tspVisualize(int id, int DT, int DN, int SN, int S, int HW, int SW, int PV, int OP, int M, int B);
    
    

    mainwindow.cpp

    
    void MainWindow::on_searchButton_clicked()				// run the seperated class
    {
        TSP tsp_run;
        tsp_run.can_tsp(ui->comboBox->currentText());
    }
    
    void MainWindow::tspVisualize(int id, int DT, int DN, int SN, int S, int HW, int SW, int PV, int OP, int M, int B)
    {
        qDebug()<< "ID " << id;
        qDebug()<< "DT " << DT;
        qDebug()<< "DN " << DN;
        qDebug()<< "SN " << SN;
        qDebug()<< "S "  << S;
        qDebug()<< "HW " << HW;
        qDebug()<< "SW " << SW;
        qDebug()<< "PV " << PV;
        qDebug()<< "OP " << OP;
        qDebug()<< "M "  << M;
        qDebug()<< "B "  << B;
    
        ui->Address_LCD->display(id);
        ui->DT_lineEdit->setText(QString::number(DT,16));
        ui->DN_lineEdit->setText(QString::number(DN,16));
        ui->SN_lineEdit->setText(QString::number(SN,16));
        ui->S_lineEdit->setText(QString::number(S,16));
        ui->HW_lineEdit->setText(QString::number(HW,16));
        ui->SW_lineEdit->setText(QString::number(SW,16));
        ui->P_lineEdit->setText(QString::number(PV,16));
        ui->OP_lineEdit->setText(QString::number(OP,16));
        ui->M_lineEdit->setText(QString::number(M,16));
        ui->BT_lineEdit->setText(QString::number(B,16));
    }
    

    tsp.cpp

    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 the output of my terminal is :

    send to ui 
    ID  18 
    DT  197014 
    DN  12 
    SN  634925 
    S  4 
    HW  7 
    SW  38 
    PV  50529024 
    OP  4 
    M  4096 
    B  38132
    

    /EDIT 1\

    I will put the complete code of mainwindow.h , mainwindow.cpp and tsp.cpp down here.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <tsp.h>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void tspVisualize(int id, int DT, int DN, int SN, int S, int HW, int SW, int PV, int OP, int M, int B);
    
    
    private slots:
        void on_remoteButton_clicked();
        void on_homeButton_clicked();
        void on_liveButton_clicked();
        void on_programButton_clicked();
        void on_systemButton_clicked();
        void on_cranesettingsButton_clicked();
        void on_upButton_clicked();
        void on_searchButton_clicked();
        void on_SAButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "canbus_socket.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QWidget::setWindowTitle("SCOT -- Sarens CanOpen Terminal");
        QWidget::showFullScreen();
    
        // Set logo
        QPixmap lgpix(":/new/rsc/img/Sarens.png");
        QPixmap scaled_lgpix = lgpix.scaled(400,100);
        ui->sarlogo_label->setPixmap(scaled_lgpix);
    
        ui->statusBar->setVisible(false);
    
        // Set data transfer
        QPixmap dtpix(":/new/rsc/img/send data.png");
        ui->connectlink_label->setPixmap(dtpix);
    
        // Home button
        QPixmap hpix(":/new/rsc/img/home.png");
        ui->homeButton->setIcon(QIcon(hpix));
        ui->homeButton->setIconSize(QSize(50, 50));
    
        // System button
        QPixmap spix(":/new/rsc/img/program.png");
        ui->systemButton->setIcon(QIcon(spix));
        ui->systemButton->setIconSize(QSize(50, 50));
        ui->systemButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    
        // set crane image
        QPixmap cipix(":/new/rsc/img/Terex_CC2800_transp.png");
        ui->craneimg_label->setPixmap(cipix);
    
    
        // TSP button
        QPixmap tsppix(":/new/rsc/img/TSP_ICON.png");
        ui->programButton->setIcon(QIcon(tsppix));
        ui->programButton->setIconSize(QSize(50, 50));
        ui->programButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    
    
        // ID up
        QPixmap uppix(":/new/rsc/img/arrow_up.png");
        ui->upButton->setIcon(QIcon(uppix));
    
        // ID down
        QPixmap downpix(":/new/rsc/img/arrow_down.png");
        ui->downButton->setIcon(QIcon(downpix));
    
        // Crane settingsbutton
        QPixmap cranespix(":/new/rsc/img/cranesettings.png");
        ui->cranesettingsButton->setIcon(QIcon(cranespix));
        ui->cranesettingsButton->setIconSize(QSize(50, 50));
        ui->cranesettingsButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
        // ShutdownButton
        QPixmap sdpix(":/new/rsc/img/shutdown.png");
        ui->shutdownButton->setIcon(QIcon(sdpix.scaled(50,50)));
        ui->shutdownButton->setIconSize(QSize(50, 50));
    
        // Home image
        QPixmap htpix(":/new/rsc/img/home_pg.png");
        ui->symbol_label->setPixmap(htpix);
    
        ui->textlabel->setStyleSheet("color: white;font-size:30px;");
        ui->textlabel->setText("SCOT  -- ");
        ui->program_label->setStyleSheet("color: white;font-size:20px;");
        ui->program_label->setText(" home");
    
        // set remote button
        QPixmap rpix = QPixmap(":/new/rsc/img/remote.png");
        ui->remoteButton->setIcon(QIcon(rpix));
        ui->remoteButton->setIconSize(QSize(200,200));
    
        // set live button
        QPixmap lpix = QPixmap(":/new/rsc/img/local.png");
        ui->liveButton->setIcon(QIcon(lpix));
        ui->liveButton->setIconSize(QSize(200,200));
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_remoteButton_clicked()
    {
        ui->stackedWidget->setCurrentIndex(1);
    
        // remote
        QPixmap htpix(":/new/rsc/img/remote_top.png");
        ui->symbol_label->setPixmap(htpix);
    
        ui->program_label->setStyleSheet("color: white;font-size:20px;");
        ui->program_label->setText(" remote");
    }
    
    void MainWindow::on_homeButton_clicked()
    {
        ui->stackedWidget->setCurrentIndex(0);
    
        // Home image
        QPixmap htpix(":/new/rsc/img/home_pg.png");
        ui->symbol_label->setPixmap(htpix);
    
        ui->textlabel->setStyleSheet("color: white;font-size:30px;");
        ui->textlabel->setText("SCOT  -- ");
        ui->program_label->setStyleSheet("color: white;font-size:20px;");
        ui->program_label->setText(" home");
    
        ui->stackedWidget_2->setCurrentIndex(0);
        ui->programButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(82,204,0), stop: 1 rgb(0,204,29));"
                                         "border-style:solid;"
                                         "border-color:grey;"
                                         "border-color:grey;"
                                         "border-width:1px;"
                                         "border-radius:10px;");
        ui->systemButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
        ui->cranesettingsButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    
    }
    
    void MainWindow::on_liveButton_clicked()
    {
        ui->stackedWidget->setCurrentIndex(2);
        // live
        QPixmap htpix(":/new/rsc/img/local_top.png");
        ui->symbol_label->setPixmap(htpix);
    
        ui->program_label->setStyleSheet("color: white;font-size:20px;");
        ui->program_label->setText(" system");
    
    }
    
    void MainWindow::on_programButton_clicked()
    {
        ui->stackedWidget_2->setCurrentIndex(1);
    
        ui->programButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(82,204,0), stop: 1 rgb(0,204,29));"
                                         "border-style:solid;"
                                         "border-color:grey;"
                                         "border-color:grey;"
                                         "border-width:1px;"
                                         "border-radius:10px;");
        ui->systemButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
        ui->cranesettingsButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    }
    
    void MainWindow::on_systemButton_clicked()
    {
        ui->stackedWidget_2->setCurrentIndex(2);
        ui->systemButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(82,204,0), stop: 1 rgb(0,204,29));"
                                         "border-style:solid;"
                                         "border-color:grey;"
                                         "border-color:grey;"
                                         "border-width:1px;"
                                         "border-radius:10px;");
        ui->programButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
        ui->cranesettingsButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    }
    
    void MainWindow::on_cranesettingsButton_clicked()
    {
        ui->stackedWidget_2->setCurrentIndex(0);
        ui->cranesettingsButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(82,204,0), stop: 1 rgb(0,204,29));"
                                         "border-style:solid;"
                                         "border-color:grey;"
                                         "border-color:grey;"
                                         "border-width:1px;"
                                         "border-radius:10px;");
        ui->systemButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
        ui->programButton->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(166,166,166), stop: 1 rgb(115,115,115));"
                                        "border-style:solid;"
                                        "border-color:grey;"
                                        "border-color:grey;"
                                        "border-width:1px;"
                                        "border-radius:10px;");
    }
    
    void MainWindow::on_upButton_clicked()
    {
    
    }
    
    
    
    void MainWindow::on_searchButton_clicked()
    {
        TSP tsp_run;
        tsp_run.can_tsp(ui->comboBox->currentText());
        ui->Terminal_textEdit->appendPlainText("Start reading sensor");
    
    }
    
    
    
    void MainWindow::tspVisualize(int id, int DT, int DN, int SN, int S, int HW, int SW, int PV, int OP, int M, int B)
    {
        qDebug()<< "ID " << id;
        qDebug()<< "DT " << DT;
        qDebug()<< "DN " << DN;
        qDebug()<< "SN " << SN;
        qDebug()<< "S "  << S;
        qDebug()<< "HW " << HW;
        qDebug()<< "SW " << SW;
        qDebug()<< "PV " << PV;
        qDebug()<< "OP " << OP;
        qDebug()<< "M "  << M;
        qDebug()<< "B "  << B;
    
        ui->Terminal_textEdit->appendPlainText("Write settings to TSP");
    
        ui->Address_LCD->display(id);
        this->ui->DT_lineEdit->setText("hallo");
        ui->DN_lineEdit->setText(QString::number(DN,16));
        ui->SN_lineEdit->setText(QString::number(SN,16));
        ui->S_lineEdit->setText(QString::number(S,16));
        ui->HW_lineEdit->setText(QString::number(HW,16));
        ui->SW_lineEdit->setText(QString::number(SW,16));
        ui->P_lineEdit->setText(QString::number(PV,16));
        ui->OP_lineEdit->setText(QString::number(OP,16));
        ui->M_lineEdit->setText(QString::number(M,16));
        ui->BT_lineEdit->setText(QString::number(B,16));
        ui->BT_lineEdit->setText(QString::number(B,16));
    
    }
    
    
    void MainWindow::on_SAButton_clicked()
    {
        tspVisualize(1,2,3,4,5,6,7,8,9,0,1);
    }
    
    

    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";
        int reply[11];
        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):reply[0]= frep;      question=1;     break;	// CANid
                case(4096):reply[1]= frep;      question=2;     break;	// Device type
                case(4104):reply[2]= frep;      question=3;     break;	// Device name
                case(25867):reply[3]= frep; 	question=4;     break;	// SN
                case(25856):reply[4]= frep; 	question=5;     break;	// Status
                case(4105):reply[5]= frep;      question=6;     break;	// HW version
                case(4106):reply[6]= frep;      question=7;     break;	// SW version
                case(25863):reply[7]= frep; 	question=8;     break;	// Profile version
                case(24576):reply[8]= frep; 	question=9;     break;	// Operating parameters
                case(24577):reply[9]= frep; 	question=10;    break;	// Measuring units/revolution
                case(8193):reply[10]= frep; 	question=11;    break;	// bittiming
            }
        }
        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.

    If you have other comments on my please tell me! I'm not an experienced programmer so constructive critisisme is allways welcom!

    It must be something very very stupid but I don't see it.

    Kind regards,
    TMO



  • @TMJJ001
    After setting all the texts, you do allow for re-paint somewhere later in your code?
    Plus, this tspVisualize runs in the same thread as the UI?


  • Qt Champions 2017

    Can you show us where and how r u displaying in the lineEdit ?



  • @TMJJ001 where this string "send to ui" is printed?
    I suspect it's in your TSP class (maybe can_tsp method?)
    If so, do you have there the same qDebug() entries as you are showing in MainWindow::tspVisualize()?
    If so, it looks like the output to console happens in your TSP class, and data never reaches MainWindow:: tspVisualize()
    If so, it looks like you are missing (or at least not showing us the code) connecting when TSP::can_tsp() finished and have the values to MainWindow::tspVisualize() for proper display in your UI



  • @JonB
    I edited my question. full code of mainwindow.h, mainwindow.cpp and tsp.cpp.



  • @dheerendra
    I edited my question. full code of mainwindow.h, mainwindow.cpp and tsp.cpp.


  • Lifetime Qt Champion

    Hi,

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

    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]);
    }

    You are creating a local MainWindow object that is destroyed directly at the end of the method. In any case, you are not updating what you think you are updating.

    One way is to add a signal to your TSP class and emit it at the end of the function. Then in your MainWindow, make tspVisualize a slot and connect that signal to it.

    As an additional point, the way you make TSP run will likely block the event loop, you might want to reconsider its design.



  • @Pablo-J.-Rogina
    I edited my question. full code of mainwindow.h, mainwindow.cpp and tsp.cpp.



  • @SGaist said in public function to set lineedit in mainwindow:

    point

    Thanks for the reply!
    I will try to do it with a slot.

    What do you mean with your last sentence? I don't get it what you mean.

    Thanks in advance,
    Kind regards,
    Toon Mertens



  • @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.



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



  • @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?


  • Lifetime Qt Champion

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



  • @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


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

    @TMJJ001

    its listed under protected in the .h file.



  • @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.


  • Lifetime Qt Champion

    @TMJJ001

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



  • @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
    
    }
    
    

  • Lifetime Qt Champion

    @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.



  • @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?


  • Lifetime Qt Champion

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



  • @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());
    

  • Lifetime Qt Champion

    @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());



  • @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.



  • @TMJJ001 you may want to check this example



  • @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!