如何向wxwidgets中的类中创建的按钮添加命令
How to add commands to buttons created from within a class in wxwidgets?
我搜索了很长一段时间,考虑了不同的选择,现在我完全被难住了。我创建了一个简单的类,它创建了16个按钮,并在构造函数中为它们分配了id。我想每个按钮都有一个事件触发时点击。
头文件中的类:
class step16
{
///signals and buttons
private:
wxButton* sequencer [16];
long* ids = new long[16];
public:
step16(wxFrame* frame);
~step16();
};
源文件中函数的声明:
///constructor for 16 step sample sequencer class
step16::step16(wxFrame* frame)
{
///clear all signals on initialization and create buttons
for(int i = 0; i < 16; i++){
ids [i] = wxNewId();
sequencer[i] = new wxButton(frame,ids[i],wxString::Format(_("")),
wxPoint(i*30 , 0,wxSize(30,20) );
}
}
///destructor for the 16 step sequencer class
step16::~step16(){delete[]signals;}
我知道如何在wxWidgets中添加单击事件到按钮的唯一方法是在主wxFrame的初始化部分使用Connect()方法,但是在程序的该部分连接它们不会带来期望的结果。主要是因为我需要在step16类的每个实例中使用一组新的16个具有唯一id和事件的按钮。如何为每个按钮添加唯一点击事件?
您可以使用Bind来绑定从wxEventHandler派生的任何类中的处理程序(即几乎任何标准wxWidgets类,包括wxFrame)。
将按钮的ID传递给Bind()
调用,以便您的事件处理程序知道哪个按钮已被按下。
例如,您的step16
构造函数可能如下所示:
///constructor for 16 step sample sequencer class
step16::step16(wxFrame* frame)
{
///clear all signals on initialization and create buttons
for(int i = 0; i < 16; i++)
{
ids [i] = wxNewId();
sequencer[i] = new wxButton(frame,ids[i],wxString::Format(_("")),
wxPoint(i*30,0), wxSize(30,20));
/// Add it to something so I can test this works!
frame->GetSizer()->Add(sequencer[i]);
/// Bind the clicked event for this button to a handler
/// in the Main Frame.
sequencer[i]->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
&MainFrame::OnPress,
(MainFrame*)frame);
}
}
在这个例子中,我在MainFrame
类中创建了事件处理程序,一个指向step16
的实例的指针传递给了ctor。
您可以使用event.GetId()
来区分按钮按下,这将是由行设置的值:
ids [i] = wxNewId();
MainFrame::OnPress
方法可以像这样:
void MainFrame::OnPress(wxCommandEvent& event)
{
long firstID = *theStep16->GetIDs();
switch(event.GetId() - firstID)
{
case 0:
std::cout << "First button" << std::endl;
break;
case 1:
std::cout << "Second button" << std::endl;
break;
default:
std::cout << "One of the other buttons with ID "
<< event.GetId() << std::endl;
}
}
相关文章:
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 按下时将图像添加到按钮
- 将自定义样式添加到 QTableview 中添加的按钮
- 隐藏、显示、添加或删除功能区中的按钮?
- 添加按钮以通知运行函数的通知
- 添加按钮时出错,即使存在这样的插槽,也不存在
- 如何将按钮添加到节点的属性编辑器面板,该面板调用该节点内的方法?
- 如何使用 GTK 将图像添加到按钮
- 从代码添加时无法单击Qt按钮
- 我动态添加一个按钮,如何在Qt的另一个空白中使用此按钮
- 将自定义按钮标题添加到消息框
- 如何添加C++/WinRT按钮单击处理程序
- 如何使用C++代码并将其添加到 C# gui 按钮
- 如何使用GTKMM添加对按钮的响应
- 是否通过其他按钮将单选按钮添加到QListWidget或从QListWidget中删除单选按钮?问题
- 当我尝试使用按钮添加QTabWidget时,Appcrash
- 如何将按钮添加到任务栏后使窗口看起来对称
- WinApi:如何为屏幕阅读器的所有者绘制的按钮添加替代文本
- 如何向wxwidgets中的类中创建的按钮添加命令
- 如何将按钮添加到正在运行的Windows GUI程序中