Total newbie needs some guidance to get started Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Total newbie needs some guidance to get started

Post by Nico »

Hello,

I have installed Code Blocks and wxWidgets and i got both working. That is I can make drag and drop a frame with some buttons and it will compile and run. Unfortunately I don't want to make a frame with some buttons that don't do anything, so I have been looking for tutorials and manuals and I found quite a number. Only problem is these are to specific for me.

I would like to make a frame and draw in it with wxClientDC. Now I found a manual with some sample codes, which are great, but when I try to compile those I get an error that says using "this" is a "invalid use of 'this' in non-member function", but every example I find uses "this".
So I don't think the problem is in the reference to "this", but in the basic setup.

Is there anyone that wants to take the time to explain me what I'm doing wrong? And preferably how to prevent this in the future. I'm sure it is not very difficult once you know it, but it took me both days of the weekend without result :-(

Code: Select all

// Code from Main.h
#ifndef MAIN_H
#define MAIN_H
#include <wx/app.h>

class Main : public wxApp
{
    public:
        virtual bool OnInit();
};
#endif // MAIN_H



//Code from Main.cpp
#include "Main.h"
#include "Gui.h"
#include <wx/image.h>

IMPLEMENT_APP(Main);    // start

bool Main::OnInit()
{
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	Gui Frame(0);
    	SetTopWindow(&Frame);
    	wxsOK = false;
    }
    return wxsOK;
}

// code from Gui.h
#ifndef GUI_H
#define GUI_H
#include <wx/panel.h>
#ifndef WX_PRECOMP
	#include <wx/frame.h>
#endif

class Gui: public wxFrame
{
	public:
		Gui(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);
		virtual ~Gui();
		void OnPaint(wxPaintEvent& event);
        wxPanel* Panel1;

	protected:
        static const long ID_PANEL1;

	private:
		DECLARE_EVENT_TABLE()
};
#endif


// code from Gui.cpp
#include "wx_pch.h"
#include "Gui.h"
#include "wx/dcclient.h"

#ifndef WX_PRECOMP
	#include <wx/intl.h>
	#include <wx/string.h>
#endif

BEGIN_EVENT_TABLE(Gui,wxFrame)
    EVT_PAINT(Gui::OnPaint)
END_EVENT_TABLE()

Gui::Gui(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	Create(parent, id, _("My frame title"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER|wxMAXIMIZE_BOX|wxMINIMIZE_BOX|wxFULL_REPAINT_ON_RESIZE, _T("id"));
	SetClientSize(wxSize(1440,900));
	Move(wxDefaultPosition);
	Center();
	// Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(0,0), wxSize(368,360), wxTAB_TRAVERSAL, _T("ID_PANEL1")); // an attempt, I thought maybe i needed a panel to refer to
}

void OnPaint(wxPaintEvent &event)
{
    wxPaintDC dc (this);
    dc.SetPen(*wxBLACK_PEN);
    dc.SetBrush(*wxRED_BRUSH);

    wxSize sz = GetClienSize();
    wxCoord w = 100, h=50;

    int x=wxMax(0,(sz.x-w)/2);
    int y=wxMax(0,(sz.y-h)/2);

    wxRect rectToDraw(x,y,w,h);
    dc.Clear();
    dc.DrawRectangle(rectToDraw);
}
Errors generated:
In function 'void OnPaint(wxPaint(wxPaintEvent&)':
error: invalid use of 'this' in non-member function
error: 'GetClientSize' was not declared in this scope
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre »

replace

Code: Select all

void OnPaint(wxPaintEvent &event)
by

Code: Select all

void Gui::OnPaint(wxPaintEvent &event)
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Post by Nico »

Thank you very much, that helps a lot. Coming from Java I have a hard time with the format. Should there always be a "class::" in front of a function or is this in specific situations?

Also Is there an equaly stupid reason for the "GetClientSize()" to cause an error?

Kind regards, Nico
briceandre
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 672
Joined: Tue Aug 31, 2010 6:22 am
Location: Belgium

Post by briceandre »

if you write your code directly in class definition, you don't need it :

Code: Select all

class Gui: public wxFrame
{
        public:
                
                void OnPaint(wxPaintEvent& event)
                {
                    /* your code */
                }
};
If you declare your class somewhere, and that you implement functions at an other place, the compiler shall know that your are implementing the member of the class. Thus, you shall specify class name :

Code: Select all

class Gui: public wxFrame
{
        public:
                
                void OnPaint(wxPaintEvent& event);
};

void Gui::OnPaint(wxPaintEvent& event)
{
   /* your code */
}
If you do not specify the class, you are defining a new global method, whose name is OnPaint. As this function is not a member of a class, it cannot use a this pointer (which one would it be ?), and it cannot find the member function wxFrame::GetClientSize (or, at least, not with the syntax you provided)
Nico
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sun Nov 07, 2010 5:19 pm

Post by Nico »

Thank you very much for your help and patience. I now understand it a bit better.

kind regards, Nico Nijman
Post Reply