March 13, 2012

KA51O KA51O
Hobby Entomologist
478 posts

[Solved]How to insert table in HTML page at current text cursor position

 

I have written a little WYSIWYG HTML editor for one of my applications. Now i’m trying to have an action in a toolbar which when triggered will open a dialog asking for the numer of rows and columns the table should have. This is not a problem. My problem is how to insert a table into the HTML displayed by my QWebView.

I have tried to achieve this using javascript calls and by using QWebElement methods. See code below.

  1.  QString javaScript = QString("var newTable = document.createElement(\"table\");\n");
  2.  javaScript += QString("var newTableBody = document.createElement(\"tbody\");\n");
  3.  javaScript += QString("var newTableRow = document.createElement(\"tr\");\n");
  4.  javaScript += QString("var newTableCell = document.createElement(\"td\");\n");
  5.  javaScript += QString("newTableCell[removed](\"testtesttest\");\n");
  6.  javaScript += QString("newTable.appendChild(newTableBody);\n");
  7.  javaScript += QString("newTableBody.appendChild(newTableRow);\n");
  8.  javaScript += QString("newTableRow.appendChild(newTableCell);\n");
  9.  javaScript += QString("document.body.insertBefore(newTable);\n");
  10.  page()->mainFrame()->documentElement().evaluateJavaScript(javaScript);
  11.  
  12.  page()->currentFrame()->documentElement().appendInside("<table>"
  13.        "<tr>"
  14.          "<td class=\"entry\"><p>Entry1:</p></td>"
  15.          "<td class=\"tableValue\"><p>n.A.</p></td>"
  16.         "</tr>"
  17.         "<tr>"
  18.          "<td class=\"entry\"><p>Entry2:</p></td>"
  19.          "<td class=\"tableValue\"><p>n.A.</p></td>"
  20.        "</tr>"
  21.      "</table>");

I dont want to insert javascript functions into the HTML content, unless theres absolutely no other option, so I tried the approach shown above without any success. The approach using the QWebElement method kind of works. My problem with this approach is how to insert the table at the current text cursor position. Using the code show above it is inserted at the end of the HTML document.

2 replies

March 15, 2012

KA51O KA51O
Hobby Entomologist
478 posts

Quick update: I have abandoned the approach to insert the table directly via QWebElement since I can’t find a way to insert the table at the position of the text cursor.

Instead I’m now using javascript to insert the table. But there are still some things I’m not happy with. I have to insert the javascript functions into the HTML during runtime. Doing this is not as straight forward as I expected.

This does not work:

  1. QString javaScript = QString("< script type=\"text/javascript\" >\n"
  2.     "function createTable()\n"
  3.     "{\n"
  4.     "Stuff necessary to insert the table..."
  5.     "}\n"
  6. "< /script >");
  7. QWebElementCollection headElem = page()->currentFrame()->findAllElements("head");
  8. headElem[0].appendInside(javaScript);
  9. QString testJS = page()->mainFrame()->documentElement().evaluateJavaScript("createTable()").toString();

Instead I have to do it this way:

  1. QString javaScript = QString("< script type=\"text/javascript\" >\n"
  2.     "function createTable()\n"
  3.     "{\n"
  4.     "Stuff necessary to insert the table..."
  5.     "}\n"
  6. "< /script >");
  7. QWebElementCollection bodyElem = page()->currentFrame()->findAllElements("body");
  8. bodyElem[0].appendInside(javaScript);
  9. QWebElementCollection scriptElem = page()->currentFrame()->findAllElements("script");
  10. QWebElementCollection headElem = page()->currentFrame()->findAllElements("head");
  11. headElem[0].appendInside(scriptElem.at(0));
  12.  
  13. QString test = page()->currentFrame()->documentElement().toOuterXml();
  14. QUrl baseUrl = QUrl::fromLocalFile(someFilePath);
  15. setHtml(test, baseUrl);
  16.  
  17. QString testJS = page()->mainFrame()->documentElement().evaluateJavaScript("createTable()").toString();

Anyone got an idea why I can’t insert the < script … > part directly into the HTML header ? Or is there a way to execute a javascript function which is not written to the HTML file from within my code ?

March 16, 2012

KA51O KA51O
Hobby Entomologist
478 posts

Another update:
I managed to insert a table at the position of the text cursor using the evaluateJavascript() method.

  1. QString javaScript = QString("$(function ()"
  2.     "{"
  3.         "var sel, range, html;"
  4.         "var newTable = document.createElement(\"table\");"
  5.         "var text = document.createTextNode(\"testtesttest\");"
  6.         "var newTableRow = newTable.insertRow(0);"
  7.         "var newTableCell = newTableRow.insertCell(0);"
  8.         "newTableCell.appendChild(text);"
  9.         "if(window.getSelection)"
  10.         "{"
  11.             "sel = window.getSelection();"
  12.             "if(sel.getRangeAt && sel.rangeCount)"
  13.             "{"
  14.                 "range = sel.getRangeAt(0);"
  15.                 "range.deleteContents();"
  16.                 "range.insertNode( newTable );"
  17.             "}"
  18.         "}"
  19.         "else if (document.selection && document.selection.createRange)"
  20.         "{"
  21.             "document.selection.createRange().node = newTable;"
  22.         "}"
  23.     "});");
  24.     QString testJS = page()->mainFrame()->documentElement().evaluateJavaScript(javaScript).toString();

There are still some things missing, but that rather relates to javascript then Qt. And this atleast works as expected and can be used as a hint into the right directions for people who are new to javascript (like me). The fancy browser example [qt.gitorious.org] was very helpfull.

 
  ‹‹ secure websockets and QtWebKit - Qt SDK 4.8 (VS2008)      Java applet window is not a child of QGraphicsWebView ? ››

You must log in to post a reply. Not a member yet? Register here!