Qt World Summit: Register Today!

WebContent inside a custom Widget

  • Hey,

    As far as i know modern applications like discord, or skype(i think?) just use and browser integrated in their desktop application, and I'd like to try this with QT, but I'm kinda getting confused with all the QWeb stuff available.

    My main goal is to display an js/html created chat(or whatever) inside an custom designed Widget. Which QT feature would work in this case?


  • Lifetime Qt Champion

  • Hi @Slei ,
    You should also refer this link: http://doc.qt.io/qt-5/qjsengine.html
    and as @jsulm mentioned link that also you have to refer.

  • http://doc.qt.io/archives/qt-5.5/qtwebenginewidgets-index.html I've look at the QWebEngineView already, but this seems to always create a seperate windows, for me the best would be to have this as ui element(the webcontent) inside my custom widget, but so far I couldn't really see which part of the whole WebEngine stuff i would need for this, even with the Whole c++ Browser example its kinda hard to get it.

  • Lifetime Qt Champion

    @Slei It should not create a window. How do you use QWebEngineView? Can you show your code?

  • oh i think i forgot to pass my widget this ptr to the WebEngineView, now it seems to work

  • @Slei Ok then mark Unsolved to Solved at top of page.

  • still got some questions:

    I was able to call c++ stuff from js with QWebChannel, the only thing that doesn't work is C++ calling JS functions, somehow it always says

    js: Uncaught ReferenceError: jsFun is not defined
    [24368:11572:0618/102139.247:INFO:CONSOLE(1)] "Uncaught ReferenceError: jsFun is not defined", source: (1)
    [24368:11572:0618/102139.247:INFO:CONSOLE(1)] "Uncaught ReferenceError: jsFun is not defined", source: (1)

    	    <script type="text/javascript">
    		function jsFun()
    	    <script type="text/javascript" src="./qwebchannel.js"></script>
            <script type="text/javascript">
                new QWebChannel(qt.webChannelTransport, function (channel) {
                // now you retrieve your object
                var JSobject = channel.objects.Widget;
    			var obj = { "testObject": "yes" };
    Widget::Widget(QWidget *parent) :
        ui(new Ui::Widget)
      , m_wev(new QWebEngineView(this))
        QWebChannel* channel = new QWebChannel(m_wev);
        channel->registerObject(QString("Widget"), this);
        m_wev->page()->runJavaScript("jsFun();",[this](const QVariant &v) { qDebug()<<v.toString();});

  • calling jsFun() directly in js works fine, but somehow it doesn't work from c++, not sure when the call from c++ happens, since it just can't find the function definition

  • ok seems like i've called the js function too early.
    using it in loadFinished from QWebEngineView works fine

Log in to reply