Unable to get a toolbar to appear 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.
Post Reply
wx_n00b
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Sep 05, 2013 12:07 am

Unable to get a toolbar to appear

Post by wx_n00b » Sun Sep 08, 2013 3:46 pm

I'm going through this tutorial:
http://zetcode.com/gui/wxwidgets/menustoolbars/

This is my code:
toolbar.cpp:

Code: Select all

// toolbar.cpp:

#include "toolbar.h"

Toolbar::Toolbar(const wxString & title)
  : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{
  wxImage::AddHandler(new wxPNGHandler);

  wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG);

  wxToolBar * toolbar = CreateToolBar();
  toolbar -> AddTool(wxID_EXIT, NULL, wxT("Exit application"));
  toolbar -> Realize();

  Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED,
    wxCommandEventHandler(Toolbar::OnQuit));

  Centre();
}

void Toolbar::OnQuit(wxCommandEvent & WXUNUSED(event))
{
  Close(true);
}
toolbar.h:

Code: Select all

// toolbar.h:

#ifndef __TOOLBAR_H__
#define __TOOLBAR_H__

#include <wx/wx.h>

class Toolbar : public wxFrame
{
public:
  Toolbar(const wxString & title);

  void OnQuit(wxCommandEvent & event);
};

#endif
main_toolbar.cpp:

Code: Select all

// main_toolbar.cpp:

#include "main_toolbar.h"
#include "toolbar.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
  Toolbar * toolbar = new Toolbar(wxT("Toolbar"));
  toolbar -> Show(true);

  return true;
}
main_toolbar.h:

Code: Select all

// main_toolbar.h:

#ifndef __MAIN_TOOLBAR_H__
#define __MAIN_TOOLBAR_H__

#include <wx/wx.h>

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

#endif
This is how I compile:
$ g++ main_toolbar.cpp main_toolbar.h toolbar.cpp toolbar.h `wx-config --libs` `wx-config --cxxflags` -o main_toolbar

When I compile and run this code... the toolbar does not show up, at all. It's just an empty space. What am I doing wrong?

User avatar
doublemax
Moderator
Moderator
Posts: 14059
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Unable to get a toolbar to appear

Post by doublemax » Sun Sep 08, 2013 9:42 pm

Tutorial code:

Code: Select all

toolbar->AddTool(wxID_EXIT, exit, wxT("Exit application"));
Your code:

Code: Select all

toolbar -> AddTool(wxID_EXIT, NULL, wxT("Exit application"));
See the difference?

Are you using a debug build for testing? The assert messages it displays are really helpful most of the time.

Also, be aware that the tutorial code is pretty old, so if you use wx 2.9.5 for testing, it's possible that the code does not compile out of the box. So check for compiler warnings and errors.
Use the source, Luke!

wx_n00b
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Sep 05, 2013 12:07 am

Re: Unable to get a toolbar to appear

Post by wx_n00b » Mon Sep 09, 2013 5:49 pm

doublemax wrote:Tutorial code:

Code: Select all

toolbar->AddTool(wxID_EXIT, exit, wxT("Exit application"));
Your code:

Code: Select all

toolbar -> AddTool(wxID_EXIT, NULL, wxT("Exit application"));
See the difference?

Are you using a debug build for testing? The assert messages it displays are really helpful most of the time.

Also, be aware that the tutorial code is pretty old, so if you use wx 2.9.5 for testing, it's possible that the code does not compile out of the box. So check for compiler warnings and errors.
Nope, using 2.8.12.

And thanks for spotting that silly error of mine :) .

Post Reply