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 » Mon Apr 26, 2021 8:05 pm

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: 1599
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Get Frame from Constructor?

Post by catalin » Mon Apr 26, 2021 8:43 pm

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: 16018
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax » Mon Apr 26, 2021 9:10 pm

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 » Mon Apr 26, 2021 9:31 pm

@ 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: 16018
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax » Mon Apr 26, 2021 9:59 pm

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 » Mon Apr 26, 2021 10:56 pm

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 » Tue Apr 27, 2021 2:20 am

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: 16018
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get Frame from Constructor?

Post by doublemax » Tue Apr 27, 2021 5:14 am

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: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor?

Post by ONEEYEMAN » Tue Apr 27, 2021 12:03 pm

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 » Tue Apr 27, 2021 2:08 pm

@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
Experienced Solver
Experienced Solver
Posts: 97
Joined: Wed Jul 29, 2020 6:06 pm

Re: Get Frame from Constructor? [Solved]

Post by doublemax@work » Tue Apr 27, 2021 2:38 pm

Stay with wxWidgets, learning by doing is the best ;)

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor? [Solved]

Post by ONEEYEMAN » Tue Apr 27, 2021 3:35 pm

@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 » Tue Apr 27, 2021 4:11 pm

@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: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get Frame from Constructor? [Solved]

Post by ONEEYEMAN » Tue Apr 27, 2021 4:28 pm

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 » Wed Apr 28, 2021 1:51 pm

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