Understanding wxWidgets sizers

I am still used to sizers in wxWidgets and as such cannot get them to do what I want.

I want a large panel to contain a list of other panels / drawers, each of which then contains a set of text boxes

----------------------
| label    text box  |
| label2   text box2 |
----------------------
----------------------
| label    text box  |
| label2   text box2 |
----------------------
----------------------
| label    text box  |
| label2   text box2 |
----------------------

      

I also need to add (at the end) and remove (anywhere) these fields. If there is too much to fit into the containing panel, a vertical scroll bar is also required.

This is what I have tried so far, it works for the first window created with the pane, but the additional added items are just a small box in the upper left corner of the main pane, although the sizer code is the same for all blocks.

//itemsList is a container containg a list of *Item pointers to each box/panel/whatever the right name is
Items::Items(wxWindow *parent)
:wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN)
{
    //one sstarting item
    OnAdd(wxCommandEvent());
}

void Items::OnAdd(wxCommandEvent &event)
{
    unsigned id = itemsList .size();
    Item *item = new Item(this,id);
    itemsList .push_back(item);

    RebuildSizer();
}
void Items::RebuildSizer()
{
    this->SetSizer(0,true);
    wxBoxSizer *sizerV = new wxBoxSizer(wxVERTICAL);

    for(std::vector<Item*>::iterator it = itemsList .begin(); it != itemsList .end(); ++it)
        sizerV->Add(*it, 1, wxEXPAND | wxLEFT | wxRIGHT, 5);

    SetSizer(sizerV);
}
void Items::OnRemove      (wxCommandEvent &event, unsigned itemId)
{
    delete itemsList [itemId];
    itemsList .erase(items.begin()+itemId);
    for(std::vector<Item*>::iterator it = itemsList .begin()+itemId; it != itemsList .end(); ++it)
        (*it)->ChangeId(itemId++);

    RebuildSizer();
}

      

And what's the best way to lay out the contents of each box? I was thinking about using a 2 by 2 grid, but I'm not sure how to make the text boxes to expand as wide as possible while making the labels as large as possible (but also maintaining alignment between the 2 box texts)?

0


source to share


3 answers


"If there is too much to fit in the containing panel, a vertical scrollbar is also required."

You can take a look at wxScrolledWindow.

"additional added items are just a small box in the upper left corner of the main panel"



I'm not sure, but maybe calling wxSizer :: Layout () will help.

"And what's the best way to lay out the contents of each box?"

Check out this sizerdemo . If it is not necessary to keep the labels as small as possible, you could give the labels a fixed width and only let the text boxes grow. If you want to adapt the size when adding or removing new fields, you can implement an OnSize () event handler.

+1


source


May I suggest you post this question to one of the wxWidgets mailing list ? They will be able to help you very quickly.



0


source


May I also recommend wxForum , I found it very useful for wxWidgets questions in the past.

More specifically to scroll the wxScrolledWindow should help, use wxScrolledWindow-> SetSizer with your top level sizer (the one that contains your controls) to customize the scrolled area, also check the samples called scroll, scrollsub and vscroll included in wxWidgets (in the samples directory of your wxwidgets installation).

0


source







All Articles