How to transfer char * from C to CPP?

Small background: I have a C code library that is part of a larger system (all C). For this part of the C library, you must create a graphical interface that allows users to play with parameters. For the graphical interface, I chose QT, as cross-platform support is required. I use Eclipse and MinGW as an IDE and compiler (but I think the question is more language than specific to the compiler?).

With QT, I created a widget that contains a pointer to a structure implemented in C that contains pointers to several functions that execute the logic of the C library.

//QTWidget.cpp
extern "C" {
#include "c-src/CLogic.h"
//extern char* textHelper;
}

QTWidget::QTWidget(QWidget *parent)
{
    //rtw is a struct that contains a function pointer to a member of QTWidget
    this->rtw.displayText = &QTWidget::displayText;
    this->clogic = CLogic_getInstance(&rtw);
}

//Public SLOT, connected to a button clicked SIGNAL
void QTWidget::buttonClicked()
{
    this->clogic->buttonClicked();
}

void QTWidget::displayText(char *text, int position)
{
    //I've tried creating a QString from the char*, but this does not work at all.
    //ui.textItem->setText(textHelper);
    ui.textItem->setText(text);
}

GUI, QTWidget:: buttonClicked(), C - . : CLogic QTWidget RefToWidget, .

//CLogic.c
static CLogic instance;

void CLogic_buttonClicked()
{
    //I've tried several variants here, such as making a global 
    //char* textHelper = "Hello World"; 
    //that is referenced by using the "extern" keyword in the CPP file above.
    instance.rtw->displayText("Hello World", 1);
}
CLogic* CLogic_getInstance(RefToWidget *rtw)
{
    instance.rtw = rtw;
    instance.buttonClicked = &CLogic_buttonClicked();
}

, ( , QT slotClicked(), CLogic_buttonClicked(), QTWidget:: displayText (), , . char * 0x1 , , int ().

C CPP?

EDIT @Luccas Matteis:

#ifdef __cplusplus
#include "QTWidget.h"

extern "C" {
#endif

struct RefToWidget{
#ifdef __cplusplus
    void (QTWidget::*displayLine)(char* text, int lineNumber);
#else
    void (*displayLine)(char* text, int lineNumber);
#endif
};

typedef struct RefToWidget RefToWidget;
#ifdef __cplusplus
}
#endif

, , , "" ( , , ... ...;))

+3
3

char * C ++, ++ C. , CLogic.c C? , CLogic.cpp , ++, C ?

++ GUI / C, Model-View-Controller. ++ - , C - ( ). , , View Controller, .

, . , CLogic.c? , CLogic, - :

void QTWidget::buttonClicked()
{
    char *display_text = this->clogic->get_button_click_text();
    ui.textItem->setText(display_text);
}
+4

, , -. -, , , 'this' - , . , , , .

"", , - "instance.rtw- > displayText (instance.rtw," Hello World ", 1)", , ..

: : "", , - , . , , .

+1

, ++ C. ,

0

Source: https://habr.com/ru/post/1778929/


All Articles