在调整wxListView的大小时,我很难调整wxFrame的大小。我设法调整了ListView的父面板的大小,同一面板中的wxButton也会根据需要调整窗口的大小。
BluetoothConnectFrame::BluetoothConnectFrame(const wxString& title, const wxPoint& pos, const wxSize& size, Frame* parent)
: wxFrame(NULL, wxID_ANY, title, pos, size),
bleConnector(std::make_unique<BluetoothConnector>()),
mainPanel(new wxPanel(this, wxID_ANY, wxPoint(0,0), wxSize(size.x, size.y / 4 * 3), wxTAB_TRAVERSAL, "Main Panel")),
sizer (new wxBoxSizer(wxVERTICAL))
{
bledevListView = std::make_unique<wxListView>(new wxListView(mainPanel, ID_Bluetooth,
wxPoint(size.GetWidth() - size.GetWidth() + 20, size.GetHeight() - size.GetHeight() + 20),
wxSize(size.GetWidth() - 50, size.GetHeight() / 2)));
bledevListView->AppendColumn("Address");
bledevListView->SetColumnWidth(0, getBLEListViewSize().x/ 2);
bledevListView->AppendColumn("Name");
bledevListView->SetColumnWidth(1, getBLEListViewSize().x / 2);
stopDiscButton = new wxButton(mainPanel, wxID_ANY, "Stop discovery", wxPoint(0,0), STOPDISCSIZE, wxBU_LEFT, wxDefaultValidator, "Stop disc");
sizer->Add(bledevListView.get(), 1 ,wxEXPAND, 1);
sizer->Add(stopDiscButton );
mainPanel->SetSizer(sizer);
}wxSizeEvent函数
void BluetoothConnectFrame::OnSize(wxSizeEvent & e) {
size = e.GetSize();
mainPanel->SetSize(getMainPanelSize());
sizer->Layout();
}在OnSize事件中打印出bledevListView大小将打印正确的值。但是,UI不会更新ListView以匹配这些值。我尝试在bledevListView上使用SetSize(),Update(),Refresh(),也尝试在不使用wxSizer的情况下调整wxListView的大小,但都不起作用。有什么建议吗?
发布于 2020-06-15 18:38:12
正如其他人在评论中指出的那样,您可以在wxEVT_SIZE处理程序中显式地布局自己,或者(这是独占或)使用sizers。要执行后一种操作,首先要完全删除帧的OnSize()处理程序。您可能仍然希望拥有一个用于列表视图本身的wxEVT_SIZE处理程序,它可以根据您的需要调整列的大小。
您显示的代码中的第二个问题甚至更糟糕:您将wxListView的所有权交给了unique_ptr<>。除非您稍后对其调用release(),否则这是非常错误的:所有的图形用户界面元素都归wxWidgets所有,并将被它删除。对于程序中的所有wxWindow-derived对象,您需要使用原始指针,或者,如果您愿意,也可以使用不具有所有权的智能指针类型(observer_ptr<>) (但这也适用于sizers,基本上也适用于您“提供”给框架以由其管理的任何内容)。
https://stackoverflow.com/questions/62371491
复制相似问题