Yogi Wannabe
Yogi Wannabe

Reputation: 181

WxWidgets SetContainingWindow exception

I am fairly new to WxWidgets. I wrote an app that was working fine in WxWidgets 3.1.5 but gives the following exceptions in 3.2.1. I looked at the sample code that comes with 3.2.1 and I don't see any issue with my code.

../src/common/sizer.cpp(887): assert "CheckExpectedParentIs(w, m_containingWindow)" failed in SetContainingWindow(): Windows managed by the sizer associated with the given window must have this window as parent, otherwise they will not be repositioned correctly.

Please use the window wxNotebook@0x7f9a5d9547d0 ("notebook") with which this sizer is associated, as the parent when creating the window wxStaticText@0x7f9a5d957e40 ("SMART log for /dev/nvme0n1") managed by it.
Collecting stack trace information, please wait...../src/common/sizer.cpp(887): assert "CheckExpectedParentIs(w, m_containingWindow)" failed in SetContainingWindow(): Windows managed by the sizer associated with the given window must have this window as parent, otherwise they will not be repositioned correctly.

Please use the window wxNotebook@0x7f9a5d9547d0 ("notebook") with which this sizer is associated, as the parent when creating the window wxButton@0x7f9a5d9564e0 ("Copy to clilpboard") managed by it.
Collecting stack trace information, please wait...../src/common/sizer.cpp(887): assert "CheckExpectedParentIs(w, m_containingWindow)" failed in SetContainingWindow(): Windows managed by the sizer associated with the given window must have this window as parent, otherwise they will not be repositioned correctly.

Please use the window wxNotebook@0x7f9a5d9547d0 ("notebook") with which this sizer is associated, as the parent when creating the window wxGrid@0x7f9a5c892a00 ("grid") managed by it. 

Here is my source

int
DlgDisplayLogs::insertPage(wxString caption, wxString hostname, wxArrayString strArray)
{
    wxPanel* panel = new wxPanel(notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize);

    wxButton* btnClipboard = new wxButton(panel, wxID_ANY, "Copy to clilpboard");
    wxStaticText* label = new wxStaticText(panel, wxID_ANY, caption, wxDefaultPosition, wxDefaultSize, wxST_NO_AUTORESIZE);

    wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
    hbox->Add(label, 1, wxEXPAND | wxRIGHT | wxBOTTOM, 10);
    hbox->Add(btnClipboard, 0, wxRIGHT | wxBOTTOM, 10);

    wxGrid* grid;
    int numEntries = strArray.GetCount();

    grid = new wxGrid(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize);
    if (!grid) {
        return -1;
    }

    grid->CreateGrid(numEntries / 2, 2);
    grid->EnableEditing(false);
    grid->DisableCellEditControl();
    grid->DisableDragGridSize();
    grid->SetSelectionMode(wxGrid::wxGridSelectionModes::wxGridSelectRows);
    grid->SetColLabelValue(0, "Description");
    grid->SetColLabelValue(1, "Value");
    grid->SetColMinimalWidth(0, 250);
    grid->SetColMinimalWidth(1, 250);
    grid->SetColSize(0, 250);
    grid->SetColSize(1, 450);
    grid->SetCellHighlightPenWidth(0);
    grid->SetCellHighlightROPenWidth(0);

    wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);

    // TODO hbox should be aligned to the right using wxALIGN_RIGHT
    vbox->Add(hbox, 0, wxLEFT | wxRIGHT | wxBOTTOM, 5);
    vbox->Add(grid, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5);
    notebook->SetSizerAndFit(vbox);

    notebook->AddPage(panel, hostname, true);
    notebook->Layout();
    //notebook->Refresh();

    for (int row = 0, i = 0; i < numEntries; row++, i += 2) {
        grid->SetCellValue(row, 0, strArray[i]);
        grid->SetCellValue(row, 1, strArray[i + 1]);
    }

    return 0;
}

All help greatly appreciated.

Upvotes: 0

Views: 896

Answers (1)

catalin
catalin

Reputation: 1987

notebook->SetSizerAndFit(vbox)

Replace that with

panel->SetSizer(vbox)

The explanation there is quite extensive. Child windows with a particular parent, can be added to a sizer that must be set to the same parent.

In your code you used panel as parent, but set the sizer to notebook.

Upvotes: 1

Related Questions