How to assign a Bitmap to a menu item

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
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

How to assign a Bitmap to a menu item

Post by Tony0945 »

I keep getting a segfault at the assignment:

Code: Select all

//static const wxBitmap defaultmap(_T("/usr/local/src/wxWidgets/samples/webview/wxlogo.xpm"));
const wxBitmap defaultmap(_T("/usr/share/pixmaps/mybird-bin-icon.png"));
static bool didit=false;
//wxBitmap("/usr/local/src/wxWidgets/samples/webview/wxlogo.xpm")
void MenuButton::InitializeProgramMenu(void)
{
wxMenuItem *item;
...
if (!didit) 
   item->SetBitmap(defaultmap);  //takes parameter      const wxBitmap &    bitmap,
didit=true;
...
SetBitmap segfaults every time. The didit stuff is because there is a loop and I wanted to isolate the problem by appending a bitmap only once. wxGTK 3.0.4
I also tried incorporating wxlog.xpm into the file instead of referencing the file name.

Is there an example somewhere of attaching a bitmap from a file? Even if the file is read into memory. Linux only.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to assign a Bitmap to a menu item

Post by doublemax »

The "menu" sample shows how to do it. There is not much that can go wrong here. If it's really the SetBitmap() call that crashes, either the wxMenuItem pointer is invalid or the bitmap. Before using the bitmap, use wxBitmap::IsOk() to check if it was loaded successfully.

Code: Select all

const wxBitmap defaultmap(_T("/usr/share/pixmaps/mybird-bin-icon.png"));
What's the context of this line? If it's a global variable, it won't work, because the image handlers are not yet initialized when this variable is created. You must call ::wxInitAllImageHandlers() before you can load any bitmap.
Use the source, Luke!
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Re: How to assign a Bitmap to a menu item

Post by Tony0945 »

doublemax wrote: Wed Jun 23, 2021 5:32 am You must call ::wxInitAllImageHandlers()
This solved the crash. But the icons are still not appearing on the button. Maybe a button was the wrong metaphor.
I'm trying to create a panel with a toolbar and a button that launches applications like the "Applications" button om Mate-panel and gnome-panel
but without all the bells and whistles like dbus messages. It's working fine now except the menu has no icons, only text. Maybe a toolbar was not the right widget.


I'm searching for the appropriate sample.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to assign a Bitmap to a menu item

Post by doublemax »

You're talking about menus, buttons, and toolbars which are all different and need different approaches to set a bitmap.

What exactly do you want to do?
Use the source, Luke!
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Re: How to assign a Bitmap to a menu item

Post by Tony0945 »

I want to create a panel with a menu that has icons next to it. Like the Quit menu in this example: https://zetcode.com/gui/wxwidgets/menustoolbars/
Perhaps the menu need not be on a button. Only the title is visible until the button is clicked. Perhaps that is standard for a wxmenu. I've been searching the web for an example but can't find one there. Mate, Gnome, XFCE have such panels
The menus look like the Firefox "History" button/menu but mine don't show the icon but should.

The "Quicklinks" button on this forum is another example.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to assign a Bitmap to a menu item

Post by ONEEYEMAN »

Hi,
Check ownerdraw sample for details.

Thank you.
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Re: How to assign a Bitmap to a menu item

Post by Tony0945 »

I checked the sample. I #include'd "wxlogo.xpm" and Appended wxlogo_xpm per the sample. It compiled fine and ran fine, but no logo icon.
Is it the size of the icon? Most of what i want to use is png Don't they resize? Maybe i should try a huge panel.
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Re: How to assign a Bitmap to a menu item

Post by Tony0945 »

It looks like wxBitmapComboBox may give me what I desire.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to assign a Bitmap to a menu item

Post by ONEEYEMAN »

Hi,
Tony0945 wrote: Fri Jun 25, 2021 1:00 am I checked the sample. I #include'd "wxlogo.xpm" and Appended wxlogo_xpm per the sample. It compiled fine and ran fine, but no logo icon.
Is it the size of the icon? Most of what i want to use is png Don't they resize? Maybe i should try a huge panel.
You mean you modified the sample code, you changed on of the bitmaps in a sample to display your bitmap and it still doesn't show?
Did you place the bitmap in the same directory as all other ones?
Was it successfully loaded?

Thank you.
Post Reply