我正在使用书籍Programming Principles and Practice Using C++学习c ++。第16章介绍了通过接口库使用FLTK库的gui部分的技术。
本章中的一个练习是动画画面的动画,由一个类中实现的开始和停止按钮控制。为了计时,我发现使用FLTK Fl::add_timeout
和Fl::repeat_timeout
比进入无限循环更好,并使用Sleep(),阻止其他回调。
我没有成功使用Fl::add_timeout
和Fl::repeat_timeout
来实施有效的解决方案,但是使用带有开始和停止按钮的进度条找到了示例here:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_Button.H>
Fl_Progress* progBar;
void runcount(void*)
{
if (progBar->value() == progBar->maximum())
{
Fl::remove_timeout(runcount);
progBar->value(0);
}
else
{
Fl::repeat_timeout(1, runcount);
progBar->value(progBar->value() + 1);
}
}
void cb_startb(void)
{
Fl::add_timeout(1, runcount);
}
void cb_stopb(void)
{
Fl::remove_timeout(runcount);
}
int main (int argc, char *argv[])
{
Fl_Double_Window window(200,70,"ProgressBar Test");
progBar = new Fl_Progress(5, 10, window.w()-10, 20);
progBar->box(FL_SHADOW_BOX);
progBar->selection_color((Fl_Color)4);
progBar->minimum(0);
progBar->maximum(10);
Fl_Button* start_button = new Fl_Button(10, 40, 80, 20, "START");
start_button->box(FL_SHADOW_BOX);
start_button->callback((Fl_Callback*)cb_startb,(void*)"start");
Fl_Button* stop_button = new Fl_Button(110, 40, 80, 20, "STOP");
stop_button->box(FL_SHADOW_BOX);
stop_button->callback((Fl_Callback*)cb_stopb,(void*)"stop");
window.end();
window.show(argc, argv);
return Fl::run();
}
此示例编译并正常工作。
然后我尝试将进度条示例放在一个类中,这就是我被卡住的地方。
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_Button.H>
#include <string>
class ProgressBar : public Fl_Double_Window {
public:
ProgressBar(int w, int h, const std::string label)
: Fl_Double_Window{ w,h,label.c_str() }
{
progBar = new Fl_Progress(5, 10, 10, 20);
progBar->box(FL_SHADOW_BOX);
progBar->selection_color((Fl_Color)4);
progBar->minimum(0); // set range: 0-10
progBar->maximum(10);
start_button = new Fl_Button(10, 40, 80, 20, "START");
start_button->box(FL_SHADOW_BOX);
start_button->callback((Fl_Callback*)cb_startb, (void*)"start"); //compile error: 'type-cast':cannot convert
//from 'overloaded-function'..
stop_button = new Fl_Button(110, 40, 80, 20, "STOP");
stop_button->box(FL_SHADOW_BOX);
stop_button->callback(static_cast<Fl_Callback*>(cb_stopb), (void*)"stop");//(Fl_Callback*)cb_stopb
//compile error: 'type-cast':cannot convert from 'overloaded-function'..
}
~ProgressBar()
{
delete progBar;
delete start_button;
delete stop_button;
}
private:
void runcount(void*)
{
if (progBar->value() == progBar->maximum())
// max reached, stop timer and reset pregress bar to 0
{
Fl::remove_timeout(runcount); // non-standard syntax, use & to create a pointer to member
progBar->value(0);
}
else
// timer running, recursive calling this function - increase progress bar by 1.
{
Fl::repeat_timeout(0.1, runcount); ///compile error: non-standard syntax, use & to create a pointer to member
progBar->value(progBar->value() + 1);
}
}
void cb_startb(void)
{
Fl::add_timeout(1, runcount);///compile error: non-standard syntax, use & to create a pointer to member
}
void cb_stopb(void)
{
Fl::remove_timeout(runcount);///compile error: non-standard syntax, use & to create a pointer to member
}
Fl_Button* start_button;
Fl_Button* stop_button;
Fl_Progress* progBar;
};
int main()
{
ProgressBar* progBar = new ProgressBar{200, 700,"Progress bar" };
progBar->end();
progBar->show();
return Fl::run();
delete progBar;
}
我无法找到如何实现回调函数。我收到了评论中写的编译错误。
如果我将runcount()
函数设为静态,则对runcount()
的4次调用的编译错误消失,但是对于我来说这个函数是静态的没有意义。我在progBar调用上遇到了新的错误。
如何实现此类,以使用启动和停止功能?
我可能缺少一些关于回调函数如何工作以及指针使用的知识,这就是为什么我要尝试解决这个问题。
答案 0 :(得分:1)
回调具有签名
void xxx(Fl_Widget*, void*)
ProgressBar回调具有签名
void ProgressBar::xxx(void*)
解决此问题的一个简单解决方案是创建一个静态函数,该函数又调用成员函数。以cb_startb为例
// Where you are getting the compilation error
start_button->callback(_cb_startb, this);
...
// Create a static version of your function
static void _cb_startb(Fl_Widget*, void* self)
{
reinterpret_cast<ProgressBar*>(self)->cb_startb();
}
// This is the member function
void cb_startb()
{
// do the same thing for runcount
Fl::add_timeout(1, _runcount, this);
}
如果将此模型应用于runcount,cb_startb和cb_stopb,它应该消除大部分编译错误。无论您将runcount用作参数,只需传入静态版本,使用 this 作为 void * 参数。
次要注意:将构造函数中的标签更改为const std :: string&amp; 。