c++ - Qt: when can I access dynamic properties from qtcreator? -


i have couple of widgets in qtcreator promoted same class. however, i'd them have subtle differences between two, i'd pass differences in ui file promoted class can use distinguish itself. dynamic properties seem way go in ui editor i've assigned dynamic property each promoted widget. in code tried accessing property, noticed seems available post construction (probably because qt calling setproperty() after object created.

mywidget::mywidget(qwidget* parent) : qglwidget(parent) {     this->property("someproperty").tostring(); // returns blank }  void mywidget::initializegl() {     this->property("someproperty").tostring(); // returns string set in ui file } 

so question how people use these properties constructor-type stuff? in initializegl, seems odd since these properties might not related initializing opengl. imagine connect property changed signal , there. common way handle this?

if generated code setupui() .ui file this:

mywidget *w = new mywidget; w->setproperty(...); 

then constructor accessing meta property not yet exist.

you can reimplement qobject::event() capture qdynamicpropertychangeevents, letting act once property initialized.

bool mywidget::event(qevent *ev) {     if (ev->type() == qevent::dynamicpropertychange) {         if (qdynamicpropertychangeevent *propev = static_cast<qdynamicpropertychangeevent *>(ev)) {             if (propev->propertyname() == "someproperty")                 ...         }     } } 

bear in mind code called every time there dynamic property change.

a better approach may create function perform necessary initilization on widget after setupui() etc. called , dynamic property created.

void setupmywidget(mywidget *w) {     qstring s = w->property("someproperty").tostring();     ... } 

typically, dynamic properties assigned default value in constructor available , non-null later.

setproperty("someproperty", defaultvalue); 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -