Get Frame from Constructor? [Solved] Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Get Frame from Constructor? [Solved]

Post by Dafarkias »

Heya :)

Am new to c++ and wxWidgets. I'm having difficulty figuring out how to store a Frame that has been declared in a class constructor...

Here's the code:

Code: Select all

namespace Examples {
  class Frame : public wxFrame {
  public:
    Frame() : wxFrame(nullptr, wxID_ANY, "Frame and events") {}
    
    bool ProcessEvent (wxEvent &event) override {
      wxWindow* window = ((wxWindow*)event.GetEventObject());
      if (event.GetEventType() == wxEVT_ACTIVATE_APP) wxMessageOutputDebug().Output("wxEVT_ACTIVATE_APP");
      if (event.GetEventType() == wxEVT_CLOSE_WINDOW) wxMessageOutputDebug().Output("wxEVT_CLOSE_WINDOW");
      if (event.GetEventType() == wxEVT_CREATE) wxMessageOutputDebug().Output("wxEVT_CREATE");
      if (event.GetEventType() == wxEVT_DESTROY) wxMessageOutputDebug().Output("wxEVT_DESTROY");
      if (event.GetEventType() == wxEVT_COMMAND_ENTER) wxMessageOutputDebug().Output("wxEVT_COMMAND_ENTER");
      if (event.GetEventType() == wxEVT_MOVE) wxMessageOutputDebug().Printf("wxEVT_MOVE {x = %d, y= %d}", window->GetPosition().x, window->GetPosition().y);
      if (event.GetEventType() == wxEVT_NULL) wxMessageOutputDebug().Output("wxEVT_NULL");
      if (event.GetEventType() == wxEVT_TEXT) wxMessageOutputDebug().Output("wxEVT_TEXT");
      if (event.GetEventType() == wxEVT_SHOW) wxMessageOutputDebug().Printf("wxEVT_SHOW show = %d", window->IsShown());
      if (event.GetEventType() == wxEVT_SIZE) wxMessageOutputDebug().Printf("wxEVT_SIZE {width = %d, height= %d}", window->GetSize().GetWidth(), window->GetSize().GetHeight());

      return wxFrame::ProcessEvent(event);
    }
    
  private:
    wxPanel* panel = new wxPanel(this);
  };

  class Application : public wxApp {
    bool OnInit() override {
      (new Frame())->Show();
      return true;
    }
  };
}
If I want to access the Frame generated by the constructor, how do I do so?

I know I can access the base class window by using

Code: Select all

wxApp::GetTopWindow()
Any help is appreciated :)
Last edited by Dafarkias on Tue Apr 27, 2021 2:11 pm, edited 1 time in total.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Get Frame from Constructor?

Post by catalin »

where do you need to access it from? One example is to keep a pointer to it, as a member of Application class.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax »

It's unclear to me what you're trying to achieve. Where do you want to access the Frame pointer? Inside ProcessEvent(), you could just use "this", if that's what you mean.

However, it's very unusual to override wxWindow::ProcessEvent(), especially for a beginner. Is there a special reason for you to do that, or did you just not know how else to receive and process events?
Use the source, Luke!
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor?

Post by Dafarkias »

@ doublemax & catalin

Thanks for the replies!

I'm new to object/class programming so this stuff is very confusing. My limited coding experience comes from .lua scripting 8)

When I said I want to access it, maybe I should have said that I want to store the pointer? Whatever you would call this:

Code: Select all

wxFrame* frame = new wxFrame(NULL, wxID_ANY, "Test", wxDefaultPosition, wxSize(600, 400));
^^^that is what I want to do, after the class constructor creates the frame, of course.

@doublemax

I'm just pulling stuff from various tutorials and examples trying to learn/absorb how the gui works. All I'm trying to accomplish with that particular code is to create an EVT_SIZE event, and that snipped of code was the first I came across that worked for me
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax »

When I said I want to access it, maybe I should have said that I want to store the pointer?
Yes. you could add a member variable "wxFrame *m_frame;" to the Application class and store the pointer in there.

Code: Select all

m_frame = new wxFrame(NULL, wxID_ANY, "Test", wxDefaultPosition, wxSize(600, 400));
All I'm trying to accomplish with that particular code is to create an EVT_SIZE event, and that snipped of code was the first I came across that worked for me
Forget about the overriding ProcessEvent :)

Code: Select all

class Frame : public wxFrame {
  public:
    Frame() : wxFrame(nullptr, wxID_ANY, "Frame and events")
    {
      m_panel = new wxPanel(this);

      // other samples might use a static event table, both methods are ok
      Bind(wxEVT_SIZE, &Frame::Onsize, this);
    }
    
  private:
    void OnSize (wxSizeEvent &event)
    {
      wxLogDebug("onsize: %d %d", event.GetSize().x, event.GetSize().y );
      
      // call event.Skip() if you want event processing to continue
      // for size events this is important, because e.g. the 
      // wxSizer layout algorithm depends on it
      event.Skip();
    }
    
    wxPanel *m_panel;
  };
Use the source, Luke!
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor?

Post by Dafarkias »

So please correct if/when I'm wrong, but in the original code I provided, the constructor defines/creates the Frame? My issue is that when I initiate the constructor from a different class, I can't seem to figure out how to store the pointer--the constructor doesn't seem to return this information.

Interestingly, I tried your suggested code for the event binding, and it threw up some errors. The errors look similar to those I've seen when trying to bind events previously:

Code: Select all

E0304	no instance of overloaded function "Examples::Frame::Bind" matches the argument list	wxTexter2	C:\Visual Projects\wxTexter2\Application.cpp	160	
E0135	class "Examples::Frame" has no member "Onsize"	wxTexter2	C:\Visual Projects\wxTexter2\Application.cpp	160	
C2039	'Onsize': is not a member of 'Examples::Frame'	wxTexter2	C:\Visual Projects\wxTexter2\Application.cpp	160	
C2065	'Onsize': undeclared identifier	wxTexter2	C:\Visual Projects\wxTexter2\Application.cpp	160	
[update]

I'm not sure what I was doing incorrectly, but I tried your binding code and this time it worked :) Probably an inattention to detail.
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor?

Post by Dafarkias »

I'm still struggling with my issue from the OP, however. I haven't been able to figure out how to access the Frame pointer from the application class.

I tried using the formula you provided with declaring a panel (but instead substituting a Frame) but did not have any luck.

Sorry if I'm not being clear, or being a bother. It is not my intent.

Thanks for all the help!
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax »

Is this what you're looking for?

Code: Select all

class Application : public wxApp {
  bool OnInit() override {
    m_frame = new Frame();
    m_frame->Show();
    return true;
  }
  
public:
  Frame *m_frame;
};
I'm not sure what I was doing incorrectly, but I tried your binding code and this time it worked :) Probably an inattention to detail.
There was a typo in the initial code i posted (Onsize vs OnSize), which i edited shorty after.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor?

Post by ONEEYEMAN »

Hi,
Maybe you should start with wxLua and work with the main library when you become more acquainted with C++?

Thank you.
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor?

Post by Dafarkias »

@doublemax

That worked, thanks a million! I appreciate the help :)

@ONEEYEMAN

Possibly? Originally I was interested in wxLua--it's actually how I discovered wx. But I've been looking for an excuse to learn any/more c++ and wxLua doesn't seem to be as well documented or have as much community support?

Idk. I could easily be wrong, those are just my assumptions based on minimal research.
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Get Frame from Constructor? [Solved]

Post by doublemax@work »

Stay with wxWidgets, learning by doing is the best ;)
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor? [Solved]

Post by ONEEYEMAN »

@doublemax,
He needs to learn C++ first . ;-)
@OP:
wxWidgets is an advanced library. It will be very hard to learn and understand wxWidgets and C++ at one shot.
I suggest to try and sign up for some C++ classes in local college and then when you have a grip on the language come back to wxWidgets.

It will be easier and more appropriate.

In the meantime - maybe something like wxPython (python is also a scripting language) will suffice?

Thank you.
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor? [Solved]

Post by Dafarkias »

@ONEEYEMAN

Thanks for the advice! I can tell you mean well.

Unfortunately the university I am attending does not offer any c++ classes. I just checked. I would definitely have considered taking a c++ class to fulfill elective credit, if that were an option. My university has very little to offer in so much as programming classes, sadly.

What you say is very logical, but I will continue my current project with c++ and wxWidgets. I can be very patient and persistent when I want to be, and have personally found that "small bites" is the best method in terms of taking on complex and more subtle projects.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor? [Solved]

Post by ONEEYEMAN »

Hi,
I wish you all the best.
C++ itself is not a simple language. It takes time and patience to master and it will be very hard to do it on your own.
Please consider the possibility of going to another school to learn it. I can assure you - it will be much easier in the long run. And it comes from experience.

In the meantime I suggest to take a look at the sample code for wxWidgets. You can find it by downloading the sources and then looking at the wxWidgets/sample directory.
A lot of stuff can be found (aqnd solved) juist by running the samples and then check the code to see how it works.

But I still want to encourage you to get a proper education of the language. Things will have much more sense then.

Thank you.

P.S.: Out of curiosity - you are offered JAVASUCKS, right?
Dafarkias
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Apr 26, 2021 7:57 pm

Re: Get Frame from Constructor? [Solved]

Post by Dafarkias »

Thank you! Much appreciated!

I have downloaded a fair amount of sample code and examples, which will be helpful no doubt.

Unfortunately my University's coding classes (c++ or java) have many prerequisites, and I do not have many electives left. This may not sound like an issue to you, but it is to me. I'm going to school for law, and cannot simply derail my current degree path for this project.

I have found an interesting c# wxWidgets wrapper called ObjectListView, in which I was able yesterday to design my entire project UI with its graphical design components. This just leaves me with coding the back end which should be relatively simple (I hope).

The only downside is that the documentation for it is relatively sparse, and I'm not sure how active its community support is. I think once I wrap my head around events (just like doublemax explained to me here) I should be on home stretch.

Is there subforum on this site where ObjectListView would be considered an appropriate topic?
Post Reply