Error using XRC files with wxwidgets

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
martynwheeler
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Feb 11, 2021 7:53 pm

Error using XRC files with wxwidgets

Post by martynwheeler »

hi,

I have been trying out codelite in my learning of wxWidgets and C++. I was reading about XRC files to store the gui front end, I like the idea of the this as it separates the code and the interface design. I tried to fire up a project on codelite with skeleton code for an XRC project - start a new project for Executable (wxWidgets + XRC Frame). The skeleton code compiles but when it is run it says "cannot load resources from '../gui.xrc'". The file is there and I have not done anything with the project. I am using 3.1.4 build of wxWidgets that I compiled on my machine. Here are the code snippets:

main.h

Code: Select all

#ifndef __MAIN_H__
#define __MAIN_H__

////////////////////////////////////////////////////////////////////////////////
// application class declaration 
////////////////////////////////////////////////////////////////////////////////

class MainApp : public wxApp
{
public:
    virtual bool OnInit();
};

// declare global static function wxGetApp()
DECLARE_APP(MainApp)

////////////////////////////////////////////////////////////////////////////////
// main application frame declaration 
////////////////////////////////////////////////////////////////////////////////

class MainFrame : public wxFrame
{
public:
    MainFrame(wxWindow *parent = NULL);
    ~MainFrame();
    
private:
    void OnCloseFrame(wxCloseEvent& event);
    void OnExitClick(wxCommandEvent& event);
};

#endif //__MAIN_H__
main.cpp

Code: Select all

#include <wx/app.h>
#include <wx/frame.h>
#include <wx/event.h>
#include <wx/xrc/xmlres.h>
#include <wx/image.h>

#include "main.h"

// initialize the application
IMPLEMENT_APP(MainApp);

////////////////////////////////////////////////////////////////////////////////
// application class implementation 
////////////////////////////////////////////////////////////////////////////////

bool MainApp::OnInit()
{
    if (!wxApp::OnInit())
        return false;

    wxInitAllImageHandlers();

    wxXmlResource::Get()->InitAllHandlers();

    if (!wxXmlResource::Get()->Load(wxT("../gui.xrc")))
        return false;

    SetTopWindow(new MainFrame(NULL));
    GetTopWindow()->Show();

    // true == enter the main loop
    return true;
}

////////////////////////////////////////////////////////////////////////////////
// main application frame implementation 
////////////////////////////////////////////////////////////////////////////////

MainFrame::MainFrame(wxWindow* parent)
{
    wxXmlResource::Get()->LoadFrame(this, parent, wxT("MainFrame"));

    Connect(wxEVT_CLOSE_WINDOW,
                wxCloseEventHandler(MainFrame::OnCloseFrame));

    Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
                wxCommandEventHandler(MainFrame::OnExitClick));
}

MainFrame::~MainFrame()
{
    Disconnect(wxEVT_CLOSE_WINDOW,
                wxCloseEventHandler(MainFrame::OnCloseFrame));

    Disconnect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
                wxCommandEventHandler(MainFrame::OnExitClick));
}

void MainFrame::OnCloseFrame(wxCloseEvent& event)
{
    Destroy();
}

void MainFrame::OnExitClick(wxCommandEvent& event)
{
    Destroy();
}
and gui.xrc

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<resource xmlns="http://www.wxwidgets.org/wxxrc">
  <object class="wxFrame" name="MainFrame">
    <title>wxMiniApp</title>
    <centered>1</centered>
    <style>wxDEFAULT_FRAME_STYLE|wxCLOSE_BOX|wxTAB_TRAVERSAL</style>
    <object class="wxMenuBar" name="m_menuBar">
      <style/>
      <object class="wxMenu" name="m_menuFile">
        <label>&amp;File</label>
        <style/>
        <object class="wxMenuItem" name="wxID_EXIT">
          <label/>
          <accel>
            <![CDATA[]]>
          </accel>
          <help>
            <![CDATA[]]>
          </help>
        </object>
      </object>
    </object>
    <object class="wxStatusBar" name="m_statusBar">
      <style/>
      <fields>1</fields>
    </object>
    <object class="wxBoxSizer">
      <orient>wxVERTICAL</orient>
      <object class="sizeritem" name="boxSizer12">
        <flag>wxALL|wxALIGN_CENTER</flag>
        <border>5</border>
        <object class="wxBoxSizer">
          <orient>wxVERTICAL</orient>
          <object class="sizeritem">
            <flag>wxALL|wxALIGN_CENTER</flag>
            <border>5</border>
            <object class="wxStaticText" name="m_staticText">
              <label>Testing 123</label>
              <style/>
            </object>
          </object>
        </object>
      </object>
    </object>
  </object>
</resource>
If anyone has an idea why it won't run I'd be very grateful.

Thanks,

MArtyn
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: Error using XRC files with wxwidgets

Post by DavidHart »

Hi,

It's a bug in the CodeLite project template (it worked when I last used it, years ago, but presumably it was later changed).
You'll find that it loads if you replace line 38 with
if (!wxXmlResource::Get()->Load("gui.xrc"))
instead of ../gui.xrc

Regards,

David

Edit: Now fixed.
Last edited by DavidHart on Sun Feb 14, 2021 12:01 pm, edited 1 time in total.
Reason: Update
Post Reply