Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

JavaScript in QTextEdit?



  • Is it possible to execute JavaScript in QTextEdit? I'd like to integrate an animation I've done in JavaScript for my website into my QTextEdit project, is that possible?



  • QTextEdit is just a text editor widget. It won't directly have anything to do with running a script. You can type JavaScript into it if you want.

    You can use QScriptEngine to run JavaScript: https://doc.qt.io/qt-5/qtscript-script-helloscript-example.html



  • I've tried following the qt-5 documentation but I do not have an int main() but rather test::test(QWidget *parent) : QMainWindow(parent), ui(new Ui::test) so I get error when I try to initialize the app since there is no argv or argc @wrosecrans


  • Lifetime Qt Champion

    @adamsmith said in JavaScript in QTextEdit?:

    so I get error when I try to initialize the app since there is no argv or argc

    Then adapt the example code to your needs or show your code...



  • @adamsmith
    I wonder just what you intend to do with your JavaScript in a QTextEdit?

    A QWebEnginePage would allow you JavaScript, see https://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript. I don't know if that is an alternative to a QTextEdit for your use case.



  • @jsulm I do not know how to pass arguments into my program in qt since I do not have a main function. I would not write that I get an error unless I do not know how to do it, right? Unnecessary and unhelpful comment.

    @JonB I want to do an animation but that animation requires JavaScript inside my QTextEdit.


  • Lifetime Qt Champion

    This post is deleted!

  • Lifetime Qt Champion

    @adamsmith I'm somehow lost: do you write a test where you use QTextEdit?
    Because EVERY C or C++ application has exactly one main function.



  • @adamsmith

    @JonB I want to do an animation but that animation requires JavaScript inside my QTextEdit.

    As I said: QTextEdit may not do what you need since it doesn't support JavaScript. I don't know whether @wrosecrans' answer about using QScriptEngine can be wrapped around a QTextEdit to achieve what you want. But if you want "full" JavaScript like on a web page to do whatever your animation is about you may need to use a QWebEnginePage to achieve.



  • @jsulm I am writing a prototype to see if what I want to achieve is possible in QTextEdit. What I am trying to achieve is an animation when one of the sides of a frame in HTML/CSS is pressed. I have found a way of doing this html/css animation in JavaScript on a prototype website I have already built but I am unable to make this animation happen inside QTextEdit because... well it does not have these types of functions, that is why I want to use JavaScript inside QTextEdit.

    If somebody is wondering: the "animation" is that when one side of a html frame is pressed the frame extends a certain length. I have to be able to write text aroud (under, on the side, above, etc) of this frame as well as inside the frame, which is possible to do in QTextEdit but not the animation thingy since I can't grab the "side of frame pressed" event in QTextEdit or Qt but I can in JavaScript.

    @jsulm I do not have a word-for-word main function. I know EVERY C or C++ program has exactly one main function but mine does not have a "int main" or "void main" or "int main(argc ,...)", etc it only has the initialization function that I mentioned above, no "type main (parameters) { ... }"



  • @adamsmith
    Last time of saying. QWebEnginePage is a true HTML page and has JavaScript. QTextEdit isn't even HTML, it's for "rich text" as defined by Qt, and uses "HTML-style tags" as its language. It doesn't support JavaScript. Nor for that matter CSS: it supports a "QSS" as defined by Qt.



  • I'll try QWebEnginePage instead of QTextEdit then! Thanks!


Log in to reply