How to enter text in ofxGui?

I’d like to enter text into a field (like ofxLabel or ofParamter) from an ofxGui and retrieve that text when the user enters something (via an event listener perhaps) and do something with it. How can I do this?

1 Like

I believe you can use the hierarchy of an ofParameter to retrieve its name :
ofApp.h :

ofxPanel gui;
ofParameter < bool > ofParExample;

ofApp.cpp :


void ofApp::setup(){

    gui.add(ofParExample.set("nameIWantToRetrieve", false));
    ofAddListener(groupParam.parameterChangedE(), this, &ofApp::nameExtractionParam);
}

void ofApp::nameExtractionParam(ofAbstractParameter& parameter){

		vector<string> hierarchy = parameter.getGroupHierarchyNames();
		auto escaped = parameter.getEscapedName();
		auto stringName = parameter.toString();
                // cut the hierarchy name here
                // and use it for something else

} 

Additionally, hierarchy parsing is discussed in other posts on the forum, here for example.

I hope it helps,

++

Thank you.

Where does groupParam come from in your example? I’m not using a groupParam.

I’m creating a text input field (ofParameter textfield), adding it to an ofxGuiGroup.

For other parameter types, I simply do
slider.addListener(this, &ofApp::onsliderChange);

and that works, but strangely it doesn’t work if i change the type to string.

I get

Error:(505, 3) no matching function for call to 'ofAddListener'

I had forgotten to add the type in the listener function (oops!)

I fixed it by adding it into the listener function like this:

void ofApp::onTextChange(string & text)

Ah yes of course it was ofParExample not groupParam .

Glad you found it anyways.

++

Hi, if you only want to listen to the changes in a particular ofParameter, rather that on an ofParameterGroup, register to its listener. For such you can use either a lambda function or class method.

//Assuming this happens in ofApp.cpp, inside ofApp::setup() probably.

ofParameter<string> strParam;

strParam.set("Param name", "default value");

// listen via class method
strParam.addListener(this, &ofApp::strParamChanged);
void ofApp::strParamChanged(string& ){// remember to declare this in ofApp.h
  //do what ever you want here
}

// listen via lambda function. You dont need to declare this function anywhere else :D
strParam.addListener([&](string& ){
  //do what ever you want here
});
3 Likes