[re-SOLVED] Getting Action Class Names
Esemwy
Posts: 578
How does one retrieve the action.className() as one would do in DAZScript? Basically the equivalent of:
var am = MainWindow.getActionMgr();var na = am.getNumActions();for (var i = 0; i < na; i++) { var action = am.getAction(i); var name = action.className(); var text = action.description; var icon = action.iconFile; print('%1,"%2" -- %3'.arg(name).arg(text).arg(icon))}
action->objectName is all the way up in QObject. It seems to be returning the right thing. Using action->metaObject()->className() does a better job, so long as it cast to a QString. The method actually returns char *, which JSON->addMember() doesn't appreciate.
void ActionServer::enumerateDAZActions(DzJsonWriter *json){ DzMainWindow *mw = dzApp->getInterface(); DzActionMgr *am = mw->getActionMgr(); int nActions = am->getNumActions(); for (int i = 0; i < nActions; i++) { json->startObject(); DzAction *ac = am->getAction(i); QString path = ac->getIconFile(); if ((!path.isEmpty()) && (QFileInfo(path).exists())) { json->addMember("icon", path); } json->addMember("name", QString(ac->metaObject->className())); json->addMember("text", ac->text()); json->addMember("custom", false); json->finishObject(); }}
Post edited by Esemwy on
Comments
DzMainWindow* mw = dzApp->getInterface();
DzActionMgr *mgr = mw->getActionMgr();
DzAction* action;
int numberOfActions = mgr->getNumActions();
for (int i = 0; i < numberOfActions; i++)
{
action = mgr->getAction(i);
dzApp->log(QString(" '%1' _ '%2' _ '%3' ").arg(action->className()).arg(action->text()).arg(action->getIconFile()));
}
Action->className() was the first thing I tried, but the code wouldn't compile. Maybe I was missing an include?
I checked VC++ says 'class " DzAction" has no member "className"'.
For reference, this is what I'm trying to write:
This is the result
1>actionserver.cpp(179): error C2039: 'className': is not a member of 'DzAction'
1> D:\DAZStudio4.5_SDK\include\dzaction.h(39): note: see declaration of 'DzAction' (TaskId:17)
1>20:00:21.951 1>
1>actionserver.cpp(179): error C2661: 'DzJsonWriter::addMember': no overloaded function takes 1 arguments
1> moc_actionserver.cpp (TaskId:17)
1> The command exited with code 2. (TaskId:17)
1> Done executing task "CL" -- FAILED. (TaskId:17)
Yes, DzAction class doesn't have the className() method. But it inherits QAction, and that inherits the QObject class.
"qobject.h" file is in the QtCore, and there is "qmetaobject.h" file in the QtCore too. The QMetaObject class contains meta-information about Qt objects. The className() function belongs to the QMetaObject class. A single QMetaObject instance is created for each QObject subclass that is used in an application, and this instance stores all the meta-information for the QObject subclass. This object is available as QObject::metaObject().
There is no DzAction.className() function. You need to use the inherits("...") function, it is not a dynamic solution.
Rob says you can use ac->metaObject()->className().
Greetings, Richard!
Yes, action->metaObject()->className() working too. I think the Meta-Object Compiler (moc) that produces a moc.cpp files containing the meta-object code for the QT classes is already creating the required information. Therefore, all you need to do is create a class containing the Q_OBJECT macro and put the code I gave above into it. Bind this code to some qpushbutton or some dialog and it will give you a complete listing of the DAZ Studio actions in the Log file (I got 835 strings !!!). You can also use the "A First Plugin" example for your experiments if you don't want to create your own class.
i thought I tried the ->metaObject() method, and I know my class has the Q_OBJECT macro in it. I'll give it another look and see if I can get a better list. As it stands, using ->objectName() leaves a lot of actions named DzAction.
Thanks! that got me exactly what I needed after I realized it was a char* rather than a QString.