2017-02-25 57 views
6

我正在学习C++使用书Programming Principles and Practice Using C++。第16章介绍了通过接口库使用FLTK库的gui部分的技巧。实现一个进度条类

本章中的练习之一是通过在课堂中实施的开始和停止按钮控制图片的移动动画。对于时机,我发现使用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(); 
} 

这个例子编译并运行良好。

然后我试着把进度条的例子放在一个类中,这就是我卡住的地方。

#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()函数为静态,4个调用runcount()上的编译错误消失,但对我来说使这个函数静态是没有意义的。我在progBar调用中遇到新错误。

如何实现这个类,使用启动和停止功能?

我可能错过了一些关于回调函数如何工作和使用指针的知识,这就是为什么我要解决这个问题。

回答

1

回调有签名

void xxx(Fl_Widget*, void*) 

的进度回调有签名

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

如果应用此模型runco​​unt,cb_startb和cb_stopb,它应该摆脱大部分的编译错误。无论你使用runco​​unt作为参数,使用这个作为void *参数传入静态版本。

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