[Interest] Send Data to JavaScript
Roland Hughes
roland at logikalsolutions.com
Mon Apr 12 17:40:04 CEST 2021
On 4/12/21 8:56 AM, John Schneiderman wrote:
> I'm attempting to send data to the JavaScript side of the QWebEngine
> code. This data is being emitted via a signal.
Here's a link to an old tutorial.
https://myprogrammingnotes.com/communication-c-javascript-qt-webengine.html
Your little Demo was missing the signal declaration. I suspect the real
problem is you aren't passing by reference.
=====
class WebClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString someattribute MEMBER m_someattribute NOTIFY someattributeChanged)
public slots:
void jscallme()
{
QMessageBox::information(NULL,"jscallme","I'm called by js!");
}
signals:
void someattributeChanged(QString & attr);
private:
QString m_someattribute;
};
=====
I humbly suggest you follow the slow and painful path here. (Knowing
full well you are probably going to try just fixing the signal <Grin>)
1.) Get just a QString to work
2.) Get just a QStringList to work
3.) Get a QMap<int, QString > to work
4.) Get a QMap<int, QStringList> to work
5.) Get your QMap<QString, QStringList> to work
I suspect when moving from step 3 to 4 you may need to add a
Q_DECLARE_METATYPE for your QMap definition. You didn't say what version
of Qt you are using. I seem to remember running into that a while back.
I cannot share any of the code that I did because that was bought and
paid for by a client. During at least one rev of Qt, when you got to a
container of containers things got a bit dicey.
Keep in mind that you don't need a signal. As Qt users we knee-jerk to
use of signal/slot but you don't really need it. Cutting and pasting
from that link.
view->page()->runJavaScript("jsfun();",[this](const QVariant &v) { qDebug()<<v.toString();});
You can invoke a JavaScript function you write for your browser page and have it pull data.
Or, (I've never tried this) you can brute force a JavaScript function
into whatever page is loaded.
view->page()->runJavaScript("function getelement(){return $('#elementid').val();} getelement();",[this](const QVariant &v) { qDebug()<<v.toString();});
If all else fails and your QMap isn't consuming a Gig+ of RAM, you can steal inspiration from this stackoverflow snippet.
|QMap<QString, int> myMap; QVariantMap vmap; QMapIterator<QString, int>
i(myMap); while (i.hasNext()) { i.next(); vmap.insert(i.key(),
i.value()); } QJsonDocument json = QJsonDocument::fromVariant(vmap); |
--
Roland Hughes, President
Logikal Solutions
(630)-205-1593
http://www.theminimumyouneedtoknow.com
http://www.infiniteexposure.net
http://www.johnsmith-book.com
http://www.logikalblog.com
http://www.interestingauthors.com/blog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210412/a1e1effb/attachment.html>
More information about the Interest
mailing list