c++fltk

FLTK 1.4: Fl_Hold_Browser How to change the background color of a given row


In the Fl_Browser_H file, the following function is not declared as virtual void item_draw(void* item, int X, int Y, int W, int H) const FL_OVERRIDE; so I can't override it in my derived class to do custom drawing. What am I missing? How should I do custom drawing? Thanks!

In a short example, I create my derived class and override the item_draw function. When compiling the example, I get the message:

void CustomHoldBrowser::item_draw(void*, int, int, int, int)’ marked ‘override’, but does not override

SAMPLE CODE:

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Hold_Browser.H>
#include <iostream>

// Custom Fl_Hold_Browser with item_draw functionality
class CustomHoldBrowser : public Fl_Hold_Browser 
{
 public:
    CustomHoldBrowser(int X, int Y, int W, int H, const char* L = nullptr)
        : Fl_Hold_Browser(X, Y, W, H, L) {}

    // Overriding the item_draw method
    void item_draw(void* item, int X, int Y, int W, int H) override 
    {
     const char* text = static_cast<const char*>(item);
     std::cout << "item_draw was called" << std::endl;
    }
};

int main(int argc, char** argv) 
{
    Fl_Double_Window* win = new Fl_Double_Window(400, 300, "Overriding item_draw");
    CustomHoldBrowser* browser = new CustomHoldBrowser(10, 10, 380, 280);
    win->end();
    win->show(argc, argv);
    return Fl::run();
}

Solution

  • const qualification is part of the function signature, you are missing the const qualification on your item_draw member function.

    void item_draw(void* item, int X, int Y, int W, int H) const override
    

    The direct parent doesn't declare the function virtual but its garndparent does. all child classes have inherit an implicit virtual, it doesn't need to be specified. using override helps catch bugs like the one you have, but it is optional too.