0

I am doing an Application on Qt for my Internship and I have a problem.

The purpose of my Application is that I have a card connected by USB to my computer that sends CAN message.

I made an application in C++ on visual studio that received those CAN messages and printed them on the console with the help of a callback.

What I want to do know is to do the same thing but with a GUI application on Qt using the functions that I previously made (I didn't used classes in my visual studio project).

Iadded all my .cpp and .h files from my visual studio project into my QT project

So here is mainwindow.h file where I didn't changed anything. I just added a textLabel on my form.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public:
Ui::MainWindow *ui;    
};

#endif // MAINWINDOW_H

The main.cpp is very basic.

MainWindow.cpp :

Here is the constructor when I open the communication

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //Function created in Library.
    open_com(...)
}

open_com opens the communication and call the callback each time a message is received

Definition of the callback

void callback (message id )
{
     Here I want to call my TextLabel from my application
}

And my problem in this application is I don't know how to access my application from a function that is not part of the MainWindow class.

If someone already had this kind of problem and has the solution I would be very gratefull !

Thank you.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Manu53
  • 91
  • 1
  • 1
  • 6
  • You could have more information about your library, for example, how you define the callback – eyllanesc Apr 21 '17 at 07:19
  • What I would do is wrap your CAN communications stuff in a class and when you receive a callback you emit a qt signal – Marco Apr 21 '17 at 07:25
  • I toughted about it but i think it will be complicate since my code wasn't made to be in classes. That's why I want to know if there is another possible solution. – Manu53 Apr 21 '17 at 07:28
  • You have to have a reference to an object in order to access it. Set a static pointer to the desired object in the source your callback is implemented, initialize it after the object has been created, then you can use it from inside the callback. – dtech Apr 21 '17 at 07:29
  • For example you could create the callback as a friend class member function. – eyllanesc Apr 21 '17 at 07:29
  • As you set the callback, it is similar to `open_com(another arguments, callback)`? – eyllanesc Apr 21 '17 at 07:38
  • Yes it is very close to that. The thing is it work when i use a button to print data of my callback but i want this to be automatic instead of having a button to call my callback. – Manu53 Apr 21 '17 at 07:46
  • You can place the callback function in the MainWindow class, as a member function. There are a couple of ways to doing this, which are detailed in the answers to [this question](http://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples) – TheDarkKnight Apr 21 '17 at 08:34
  • Possible duplicate of [Use UI elements from static function](http://stackoverflow.com/questions/43290753/use-ui-elements-from-static-function) – m7913d Apr 22 '17 at 13:22

0 Answers0