我在调整wxFrame的大小时,很难调整我的wxListView的大小,我设法调整了ListView的父面板的大小,而同一面板中的wxButton也随着调整窗口的大小而移动。我成功地调整了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的大小,但是没有任何效果。有什么建议吗?
正如其他人在评论中所指出的,你可以自己明确地在 wxEVT_SIZE
处理程序或(这是唯一的或)使用sizer。要做后者,首先要删除你的 OnSize()
的处理程序。你可能仍然希望有一个 wxEVT_SIZE
处理程序,它可以按照你的要求调整其列的大小。
你展示的代码中的第二个问题更糟糕:你把列表视图的所有权交给了 wxListView
到 unique_ptr<>
. 除非你叫 release()
这是很错误的:所有的GUI元素都是由wxWidgets拥有的,并且会被它删除。你需要使用原始指针,或者,如果你喜欢,使用非所有权的智能指针类型 (observer_ptr<>
)对所有 wxWindow
-在你的程序中派生的对象(但这也适用于sizer,基本上,任何你 "给 "框架管理的东西)。