首页 > IT职业 > IT职场 > boost的signal和solt机制使用入门
signal-slot是一个非常方便的接口机制,在Qt和Gtk中广泛使用。boost也实现了一个signal-slot机制。
void printCoordinates(long x, long y)
{
std::cout
}
void button_click_test()
{
Button button;
button.addOnClick(&printCoordinates);
std::cout
button.press(200,300);
button.press(20,30);
button.press(19,3);
}
button.addOnClick可以直接接收任何能够被signal.connect接受的参数。
class TextView : public View
{
public:
TextView(Document& doc) : View(doc) { }
virtual void refresh(bool bExtended) const {
std::cout
}
};
class HexView : public View
{
public:
HexView(Document& doc) : View(doc) { }
virtual void refresh(bool bExtended) const {
std::cout
const std::string& s = m_doc.getText();
for(std::string::const_iterator it = s.begin();
it != s.end(); ++it)
std::cout (*it);
std::cout
}
};
使用方法:
void document_view_test()
{
Document doc;
TextView v1(doc);
HexView v2(doc);
std::cout
doc.append("Hello world!n");
doc.append("Good!n");
doc.append("Happy!n");
}
该代码运行后,可以看到如下的结果
================= document view test ===============
TextView:Hello world!
HexView: 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 a
TextView:Hello world!
Good!
HexView: 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 a 47 6f 6f 64 21 a
TextView:Hello world!
Good!
Happy!
HexView: 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 a 47 6f 6f 64 21 a 48 61 70 70 79 21 a
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4684/viewspace-2801116/ ,如需转载,请注明出处,否则将追究法律责任。