PDA

View Full Version : Using "data" when exposing QAction to QJSEngine



TorAn
30th March 2016, 14:24
I exposed QAction (name "act"), QMessageBox (name "msg") and option class (name "options") instances to QJSEngine. My script is shown below:

function(a)
{
msg.text = options.textdata;
msg.exec();
act.data = options.textdata;
act.trigger();
}

When executing the script I see message box with the correct text and "act" is triggered. Debugger hits the slot for the action, but the code act->data().toString() produces null.

My questions are:
0. Am I correctly using act->data().toString() or I should first convert it to JSValue? Code sample on how to do it will be greatly appreciated.
1. If the way I am retrieving "data" for act in my c++ handler is correct then why using "text" works for QMessageBox, but using "data" for QAction does not
2. How to add properties to the standard Qt classes (button etc) without wrapping it in custom classes and exposing Q_PROPERTY?

Thanks.

Lesiok
30th March 2016, 14:35
2. How to add properties to the standard Qt classes (button etc) without wrapping it in custom classes and exposing Q_PROPERTY?

Thanks.

Read about dynamic properties.

anda_skoa
30th March 2016, 14:44
0. Am I correctly using act->data().toString() or I should first convert it to JSValue? Code sample on how to do it will be greatly appreciated.

That should be ok. data() returns a QVariant, if the value inside it is a QString or has known conversion to QString, then toString() shou



1. If the way I am retrieving "data" for act in my c++ handler is correct then why using "text" works for QMessageBox, but using "data" for QAction does not

QMessageBox has a property called "text", QAction does not have a property called "data".
So assigning to "data" might have created a dynamic property with that name.
You could try accessing that through QObject::property() on the action object.



2. How to add properties to the standard Qt classes (button etc) without wrapping it in custom classes and exposing Q_PROPERTY?

You don't have to wrap classes, simply deriving allows you to add Q_PROPERTY.
QJSEngine might also be able to access dynamic properties, see above.

Cheers,
_

TorAn
30th March 2016, 15:32
anda_skoa,Lesiok - thank you guys for the quick reply!

I am aware of dynamic properties, as a matter of fact that's where I started. I came back to it after your comments and try to repeat what I did in the first place. Did not work :). Here is what I do:

// initialization

QAction* actp = actionSupport::addAction(params); // returns instance of the QAction
actp->setProperty("testprop", "ttt"); // creation of a dynamic property
QJSValue act = myEngine.newQObject(actp);
myEngine.globalObject().setProperty("act", act);

// script

act.testprop = "aaa";
act.trigger();

// QAction slot:

QAction* act = reinterpret_cast<QAction*>(QObject::sender());
QString s = act->text(); //correct action text
s = act->property("testprop").toString(); // returns "ttt", not "aaa" value that I set up in the script

anda_skoa
30th March 2016, 17:50
Ah, too bad.

Well, you can easily make actionSupport::addAction() return an instance of your own QAction derived class.

Btw, no need for reinterpret_cast, QObject::sender() doesn't return void*.

Just static_cast if you are sure it is a QAction or qobject_cast/dynamic_cast if you need runtime checking.

Cheers,
_