1

I'm new to C++ and new to codelite and also new to wxCrafter. I'm trying to build some GUI apps, but I'm messed up about object passthrough in C++. I spent a few hours and I just understand a little bit of that. First, to pass variables between wxFrame/wxDialog, I should create a instance of that class.

in frameA.cpp

void frameA::buttonAClicked() {
    frameB * frameB1 = new frameB(NULL);
    frameB1->connect(this);
}

in frameB.cpp

void frameB::connect(frameA *upper) {
    //now I can access frameA via upper
}

But for a more complex case(e.g. 10 frames), values entered by user need to be shared between frames. I think it's better to make the frames/dialogs to be handle by a parent. Since all classes were triggered by main.cpp, so I think MainApp() will be good idea. So I tried to do this:

main.cpp:

class MainApp : public wxApp {
public:
frameA * frameA1;
frameB * frameB1
//frameC, frameD, frameE etc.
MainApp() {}
virtual ~MainApp() {}
virtual bool OnInit() {
    frameA1 = new frameA(NULL);
    frameB1 = new frameB(NULL);
    frameA1->connect(this);
    frameB1->connect(this);

    SetTopWindow(frameA);
    return GetTopWindow()->Show();
}
};

in both frameA.cpp and frameB.cpp:

frameA::connect(wxApp *par) {
    this->parent = par;
}

Now I'm able to access MainApp via parent, but the two member objects(one is itself) was not found. Am I missed something? I'm really new to C++. Is that any better way (or a formal way) to do?

Wilson Luniz
  • 459
  • 2
  • 7
  • 18

1 Answers1

1

There is convenient way to make kind of global data in wxWidgets application. Create file ApplicationData.h:

#pragma once    // replace with #ifndef ... if not supported by your compiler

class frameA;
// place here required forward declarations
// ...

struct ApplicationData
{
    frameA* frameA1;
    // any other data you need
};

Include this file to application class h-file:

#include "ApplicationData.h"

class MainApp: public wxApp
{
public:
    ApplicationData applicationData;  // or may it private with get/set functions
    ...
};

Finally, you can access applicationData from any place of wxWidgets application:

ApplicationData* pData = &wxGetApp().applicationData;
// Set/read global data members here:
// pData->...

See also: wxGetApp function definition in wxWidgets reference: http://docs.wxwidgets.org/2.6/wx_appinifunctions.html Note that you must add IMPLEMENT_APP and DECLARE_APP macros to make it working.

Alex F
  • 42,307
  • 41
  • 144
  • 212