实现进度条 class

Implementing a progress bar class

我正在使用这本书 Programming Principles and Practice Using C++ 学习 c++。第 16 章介绍了通过接口库使用 FLTK 库的 gui 部分的技术。

本章的练习之一是动画化图片的移动,由 class 中实现的开始和停止按钮控制。对于计时,我发现使用 FLTK Fl::add_timeoutFl::repeat_timeout 是比进入无限循环并使用 Sleep() 阻塞其他回调更好的解决方案。

我没有成功使用 Fl::add_timeoutFl::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(); 
}

这个例子可以编译并且工作正常。

然后我尝试将进度条示例放在一个 class 中,这就是我卡住的地方。

#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 调用中收到新错误。

如何实现此 class,以使用启动和停止功能?

我可能缺少一些关于回调函数如何工作和指针使用的知识,这就是我尝试解决这个问题的原因。

回调有签名

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);
}

如果将此模型应用于运行计数,cb_startb 和 cb_stopb,它应该会消除大部分编译错误。无论你在哪里使用 runco​​unt 作为参数,传入静态版本,使用 this 作为 void* 参数。

小提示:将构造函数中的标签更改为 const std::string&。