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.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Thu Nov 10, 2022 9:09 pm
Hello all,
be patient... I am trying to learn...
I would like to use an image as a button. Reading the documentation i did the following:
Code: Select all
wxBitmap bitmap;
bitmap.LoadFile("calc.png", wxBITMAP_TYPE_PNG);
wxButton* calculator = new wxButton(this, i, "", wxDefaultPosition, wxSize(20,-1));
calculator->SetBitmap(bitmap);
costSizer->Add(calculator);
but this is not working: no compile error, the windows is loaded but the image is not shown
What am I doing wrong?
thanks for your help!
ONEEYEMAN
Part Of The Furniture
Posts: 7481 Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine
Post
by ONEEYEMAN » Thu Nov 10, 2022 10:26 pm
Hi,
Are you sure the file you are loading exist?
Is loading sicceeded?
Did you call wxInitAllImageHandler() in the startup of the application?
Thank you.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 5:06 am
Hi, thanks for you answer. The file exists cose if I use BitMapButton it works.
I also confirm that wxInitAllImageHandler() have been added.
Last edited by enricovittorini on Fri Nov 11, 2022 6:02 am, edited 1 time in total.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 5:34 am
I guess I found the issue: in my button definition there was no text cose i just wanted the image
Code: Select all
wxButton* calcultatorBtn = new wxButton(this, i, "" );
as I added a text, it started working.
Code: Select all
wxButton* calcultatorBtn = new wxButton(this, i, " txt" );
Now I need to figure out how to style without text. I tried:
Code: Select all
wxButton* calcultatorBtn = new wxButton(this, i, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxBU_NOTEXT);
but if there is no text, it does not work!
any clue?
ONEEYEMAN
Part Of The Furniture
Posts: 7481 Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine
Post
by ONEEYEMAN » Fri Nov 11, 2022 5:54 am
Hi,
If there is no text - use wxBitmapButton.
Thank you.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 6:05 am
I was thinking that by using the following style I was going to get a button with an image without a text. Did I misunderstood?
https://docs.wxwidgets.org/3.2/classwx_button.html
wxBU_NOTEXT:
Disables the display of the text label in the button even if it has one or its id is one of the standard stock ids with an associated label: without using this style a button which is only supposed to show a bitmap but uses a standard id would display a label too.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 6:24 am
Damn... think I found the issue.
the button as "i" as ID: this is because it is in a for loop and I need to set a known ID. The "i" starts at 0 and it seems wxWidgets does not like this.
If I change the value of the ID from "i" to "i+1" it works.
I think this is due to
https://docs.wxwidgets.org/latest/overv ... owids.html
Finally, you can just define your own IDs. Typically this is done by using a C++ enum to automatically ensure their uniqueness. If you do this, please note that your custom IDs must be positive to avoid clashes with the automatically assigned IDs discussed above and should not have values 0 or 1 , that can result in surprising behaviour under some platforms
ONEEYEMAN
Part Of The Furniture
Posts: 7481 Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine
Post
by ONEEYEMAN » Fri Nov 11, 2022 1:25 pm
Hi,
Why do you need an id at all? Why not use wxID_ANY?
Thank you.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 5:00 pm
because I create a vector of objects with a for loop. In each raw I have a button.
Code: Select all
- unit price ; quantuty ; btn; total
- unit price ; quantity ; btn; total
- unit price ; quantity ; btn; total
- unit price ; quantity ; btn; total
...
...
every loop iteration, i put the "unit price", the "quantity" and the toal in three vectors of objects.
Code: Select all
vector_price.pushback(unit_price);
vector_quantity.pushback(unit_price);
vector_total.pushback(unit_price);
When i click the button that is at line 2, i know i have to get:
- vector_price.at(2)
- vector_quantity.at(2)
with these value i do a math (price * quantity * and put the result in total.at(2).
I need to know the ID of the button to understand which raw it belongs to. In this example the button of raw 2 would have ID=2
ONEEYEMAN
Part Of The Furniture
Posts: 7481 Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine
Post
by ONEEYEMAN » Fri Nov 11, 2022 6:12 pm
Hi,
You don't have to.
Make wxButton-derived class where you set the row to some internal variable.
When you click the button - you will know the row and will be able to get the values from that row of text controls.
This will be simpler and eliminate the need of keeping the id assignments.
Thank you.
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Fri Nov 11, 2022 9:00 pm
Seems a good approach.
As I am learning... Would like to get into the details of your proposal.
Let say I have a
Code: Select all
for (int i = 0; i< product_size; i++) {
Code to build wxTextCtl and Buttons here
};
Event handler for the button here
If I understand correctly, I should create a class
Code: Select all
RawButton : Button
{
public:
int raw= NULL;
}
What I do not understand is how to her the value of the raw variable in the event handles.
Would you help?
Thanks
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Sat Nov 12, 2022 6:21 am
I am trying.....
Defined the following class RawButton:
Code: Select all
class RawButton : public wxButton
{
public:
int rawBtn = NULL;
RawButton(int rawnumber, wxWindow* parent, wxWindowID id, wxString wxEmptyString, wxPoint wxDefaultPosition, wxSize wxDefaultSize, long style)
: wxButton(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize,style) {
rawBtn = rawnumber;
};
};
in my paneli created the following button where "i" is the rawnumber:
Code: Select all
m_calcultatorBtn = new RawButton(i, this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxBORDER_NONE | wxBU_NOTEXT);
m_calcultatorBtn->SetBitmap(*bitmap);
costSizer->Add(m_calcultatorBtn);
and the following event handler:
Code: Select all
m_calcultatorBtn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {OpenCalculator(&evt); });
Now, i do not know how to get the raw number in the OpenCalculator().
doublemax
Moderator
Posts: 19164 Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2
Post
by doublemax » Sat Nov 12, 2022 9:41 am
Code: Select all
m_calcultatorBtn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
RawButton *button = dynamic_cast<RawButton *>( evt.GetEventObject() );
if( button ) {
int a = button->rawBtn;
OpenCalculator(&evt);
}
});
However, personally i think subclassing wxButton for this purpose is overkill. Using the ID is fine. If you want to make it a little more robust, use a hashmap to connect button IDs to data-row IDs.
Also, if you use hard-coded IDs, make sure they start at wxID_HIGHEST + 1
Use the source, Luke!
enricovittorini
Knows some wx things
Posts: 34 Joined: Mon Nov 07, 2022 2:54 pm
Post
by enricovittorini » Mon Nov 14, 2022 8:20 pm
Thanks a lot