ofxPanel clear and add

Hi all,

I have a gui using ofxPanel into which I add parameters at runtime. I’d like to be able to delete the parameters too. ofxPanel doesn’t have a remove method but it does have a clear method, so I am trying to use that.

So I add elements to a vector, when I do that I also add a ofParameterGroup to the gui, then when I delete an element from the vector, I clear the gui then loop through the elements and add the ofParameterGroups back to the gui.

My problem is that when adding back to the gui. the first element is missed for some reason.

Here is some example code. To test, press key 1 around 5 time to create some parameters in the gui. Then press key 2 to delete the first element and refresh the gui.

Any idea why this is happening?

ofApp.h

#include "ofMain.h"
#include "ofxGui.h"

class myParams {
public:
	ofParameter<int> p1{ "p1",0,0,100 };
	ofParameterGroup prm{ "params", p1 };
};

class ofApp : public ofBaseApp{

	public:
		void setup();
		void draw();

		void keyPressed(int key);

		ofxPanel gui;
		std::vector<myParams> params;
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	gui.setup("settings");
}

//--------------------------------------------------------------
void ofApp::draw(){
	gui.draw();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
	if (key == '1') {
		myParams p;
		gui.add(p.prm);
		params.push_back(p);
	}
	else if (key == '2') {
		//print out the values
		cout << "size: " << params.size() << endl;
		for (int i = 0; i < params.size(); i++) {
			cout << params[i].p1.get() << endl;
		}

		//now erase the first one
		params.erase(params.begin());

		//now regenerate the gui
		gui.clear();
		for (int i = 0; i < params.size(); i++) {
			//this does not work
			gui.add(params[i].prm);

			//if I add the ofParameter straigt to the gui it works
			//gui.add(params[i].p1); 
		}

		//print out the values
		cout << "size: " << params.size() << endl;
		for (int i = 0; i < params.size(); i++) {
			cout << params[i].p1.get() << endl;
		}
	}
}