In the case of an editable web page, ideally we'd want to be able to ask the QWebFrame
in what QWebElement
is the caret, at any time. Then we could tweak this element with C++ code.
Unfortunately, according to the documentation, no such method is provided.
Anyway, there's a workaround in webkit's javascript since we can access the user selection or the caret's position in the DOM with window.getSelection()
. Here's some code that finds the nearest <p>
tag above the selection and changes its style as needed:
QString js=
" var node = window.getSelection().getRangeAt(0).startContainer;"
" while (node) {"
" if (node.nodeName.toUpperCase()=='P') {"
" node.style.display='none';"
" break;"
" }"
" node=node.parentNode;"
" }" ;
QVariant res = page()->mainFrame()->evaluateJavaScript(js);
You might want to generalize it to take node types and styles as parameters, and possibly test it a bit (I'm no JS expert), but the I believe the principle is sound.