Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3489750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:25:57+00:00 2026-05-18T11:25:57+00:00

A little background: I’ve got a library of C code that is part of

  • 0

A little background:
I’ve got a library of C code that is part of larger system (all C). For this particular part of the C library a GUI has to be created which allows users to play around with the options. For the GUI I’ve chosen QT, since cross-platform support is desired.
I’m using Eclipse and MinGW as IDE and compiler (but I think the question is more language than compiler specific?).

With QT I’ve created a widget that holds a pointer to a struct implemented in C that contains pointers to several functions that perform 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's 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);
}

When the user presses a button in the GUI, the method QTWidget::buttonClicked() is called, which tells the C library to do something. Note the the CLogic struct has a reference to the the QTWidget in the form of a struct RefToWidget which holds a function pointer.

//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();
}

When debugging this program, I find that all the function calls are executed as intended (when I press a button, the QT slot buttonClicked() is called, the CLogic_buttonClicked() is called, which calls the QTWidget::displayText() as planned, but in this last call the parameters are invalid. The char* text points to 0x1 and claims to be pointing to memory out of bounds, while the int position looks like some random number (uninitialized).

How do I pass this data from C to 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

As said above the function calls behave as expected, but the data is not passed “correctly” (even though that when I look at it, the code seems a bit… weird… ;))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:25:58+00:00Added an answer on May 18, 2026 at 11:25 am

    Your problem is not passing the char * from C to C++ but calling a C++ function from C. I presume that CLogic.c is compiled as a C library? If not, can you rename it to CLogic.cpp or use a compiler switch to force it to be compiled as C++ even though it has a C file extension?

    If you want a C++ GUI on a C library/system, you need to use proper Model-View-Controller logic. Here the C++ code is the View and the Controller while the C code is the Model (as best as I can tell from your description). You need to make it so you set and get data from the model but the model never calls the View or Controller as you are trying to do.

    Think about what you are REALLY trying to do. If you just want to display a static string when the button is pressed, why go to the bother of calling into CLogic.c? If you want to display a string that depends upon the state of the CLogic instance then instead do something like:

    void QTWidget::buttonClicked()
    {
        char *display_text = this->clogic->get_button_click_text();
        ui.textItem->setText(display_text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First a little background: I have an Event model that has various event_type s.
I've got a little problem as seen below: The cell has a background color,
For a little background, I work for a firm that develops web-based enterprise social
A little background on this error: The customer getting this error message in their
little background: currently putting together a website that is selling products, many of which
A little background information: I have a table called table_a , which has 12
First of all a little background of my problem, i'm trying to identify how
First a little background. I got a warning in NetBeans told me not to
I'd like to start with a little background, in case anyone has any better
A little background I'm working on an .net application that's uses plugins heavily, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.