public function to set lineedit in mainwindow
-
@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:
- 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"); } ...
- 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.
-
@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?
-
You are missing the
&
before the functions in the connect statement. -
@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 ? -
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.
-
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 }
-
Hi
ok thats odd. looks ok from quick look.
must test in compiler to know for sureanyway, 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. -
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());
-
-
@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!