Using rectangles between the paint and resize functions

I’m making rectangles in the plugin editors paint function and want to use them in the resize function so I can put sliders in them. ie. slider.setBounds(rect.getX(), rect.getY(), rect.getWidth() * 0.5, rect.getHeight());
I thought I should create the rectangles in .h’s private section so they’re accessible from both but I can’t do this whether or not I define a default size and position. Defining them like Rectangle<int> rect; isn’t allowed because I haven’t set a rectangle size and Rectangle<int> rect(0, 0, 100, 100) isn’t ok either for some reason

That’s strange, it should work like that. Can you maybe post the exact error message? I assume you had some other mistake, that seemed to point to the rectangle code.

You can use like you did:

Rectangle<int> rect;

Since C++11 you can also initialise the rectangle in the header:

Rectangle<int> rect = {0, 0, 100, 80};

HTH, good luck

Not sure what happened, when I try it now, it works. No idea why.
edit… it works fine in PluginEditor, but not my custom class… I’m able to make a rectangle in the paint class but not in .h’s private section. On running, it hits
void Graphics::drawRect (Rectangle<float> r, const float lineThickness) const
jassert (r.getWidth() >= 0.0f && r.getHeight() >= 0.0f);

So I’m guessing I cant define a new rectangle without also setting it’s x y width height, but if i define it like Rectangle<int> rect = {0, 0, 100, 100}; I still get this assert break.

A rectangle with the default constructor has width = -1 and height = -1. So that’s what triggers the assert.
A common mistake when using this (at least happend to me several times) is to calculate a size for the rect in resized(), but not calling setSize() e.g. in the pluginEditor’s constructor.
But I just tried this in a new GuiApplication, adding in MainComponent.h:

    Rectangle<int> rect = {10, 10, 50, 60};

and in MainContentComponent.cpp - paint:

void MainContentComponent::paint (Graphics& g)
    // (Our component is opaque, so we must completely fill the background with a solid colour)
    g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));

    g.setFont (Font (16.0f));
    g.setColour (Colours::white);
    g.drawText ("Hello World!", getLocalBounds(), Justification::centred, true);

    g.drawRect (rect);

And it worked here…

oh my,
so in paint() I was setting my rectangle something like:
rect.setBounds(0, 0, iWidth, iHeight * 0.1);
but I was defining iWidth = 40; etc. in the destructor and not the constructor…
Thank you so much for your help! Sorry for wasting your time… :flushed:

Actually, no - a default one is just { 0, 0, 0, 0 } !

Ah, thanks for correcting, my bad…
One time I didn’t double check, and BAM :wink:

I wonder, why the assert fired for the OP, but he might have set then a negative width or height unintentionally…