I’m trying to get a colour selector to show some swatches, at least I think that’s what they are called? A palette of set colours, like the one that appears in the colour selector in the introjucer. Anyhow, I’m having some problems. My class looks like this:
Surely I’m missing something here. Or perhaps it’s not as easy as I thought. I don’t have to go messing with colour selector look and feel methods do I?
Thanks Valley. I tried that but still no joy. Perhaps colour swatches is the wrong term? I’m hoping to show a palette of selected colours below the circular gradient selector widget. Here’s a screenshot of what I have:
[attachment=1]Untitled.png[/attachment]
And this is what I would like to have:
[attachment=0]Untitled1.png[/attachment]
I don’t see anything to tell the colour selector to show swatches?
Ok another thing wrong I see in your original code is that your implementation of getNumSwatches() etc are hiding not overriding their parent class implementations.
class MyColourSelector : public ColourSelector
{
public:
MyColourSelector();
virtual ~MyColourSelector(); // Note virtual !!!
// -----------------------------------------------------------------------
virtual int getNumSwatches() const; // note virtual
virtual Colour getSwatchColour(const int index) const; // note virtual
virtual void setSwatchColour(const int index, const Colour& newColour) const; // note virtual
private:
...
};
Google method hiding vs overriding for the details.
Ok, I need to learn about hiding versus overriding, method hiding is a new to me even though I’ve probably being using it without knowing for a while. However, in Jule’s introjucer code he does this:
class ColourSelectorWithSwatches : public ColourSelector
{
public:
ColourSelectorWithSwatches() {}
int getNumSwatches() const;
Colour getSwatchColour (int index) const;
void setSwatchColour (int index, const Colour& newColour) const;
};
And it definitely works there? I’ve looked over his code and I can’t see what I’m missing. I’m sure it’s something stupid I’ve overlooked!?
OK. Can someone chime in here, because both things Valley said are wrong, IMO.
First, you don’t have to specify the constructor as you show, C++ will run the default constructor for you.
Second, just adding a ‘virtual’ doesn’t mean that you are hiding the function. It’s just redundant. The signatures look the same, so they should be overriding. Most compilers will warn if you’re hiding, BTW.
Put your debugger at getNumSwatches() and see what happens. I wonder if your creation of standard colors is too late - it was 0 when you constructed the selector, so the view just doesn’t contain them.
Try a constant number of swatches, maybe - after all, you don’t want an infinite set of swatches anyway.
The above was knocked together in under 10 mins, so its bad practices all around, but it works, and it does show a colour selector with 16 colour swatches below the colour field. Maybe make a quick test project from it and test it?
Thanks everyone. Turns out there was a good reason the debugger wasn’t hitting those functions!Because I wasn’t including the const qualifier at the end of the function declaration thus I wasn’t actually overriding the functions. Serious embarrassment here…