PDA

View Full Version : Editable content in Webkit



mholmes
16th January 2012, 13:23
Hi there,

I'm building a rudimentary WYSIWYG/code editor widget based on Webkit
and the contenteditable attribute, and I'm finding it quite
straightforward so far. However, there's one issue I can't figure out.
To toggle bold text (for example), it's sufficient to do this:

webView->triggerPageAction(QWebPage::ToggleBold);

However, I would also like to be able to make my menu items and toolbar
buttons reflect the state of the text under the cursor, so that (for
instance) if the cursor is on bold text, the Bold toolbar button would
be pressed and the menu item would be checked. This is clearly possible,
because the default context menu provided the webkit instance works this
way, but I can't find out how to read the state of the current text from
the control.

I found this example project:

http://labs.qt.nokia.com/2009/03/12/wysiwyg-html-editor/

which looks like it might show me what I need, but the codebase is no
longer available; it was in the Trolltech git repository, which is no
longer there, and I can't find it in the qt-project.org git repo.

Can anyone suggest a solution to this, or, failing that, show me where I
might find the wysiwyg-html-editor project?

All help appreciated,
Martin

wysota
16th January 2012, 22:36
Use QWebView::pageAction() to get a pointer to the QAction object associated with the action you want. Then add this action to your toolbar or menu. If you want to manually associate it with a tool button that is not in the toolbar, call setDefaultAction() on it, passing it the action received from QWebView.

mholmes
16th January 2012, 23:02
Use QWebView::pageAction() to get a pointer to the QAction object associated with the action you want. Then add this action to your toolbar or menu. If you want to manually associate it with a tool button that is not in the toolbar, call setDefaultAction() on it, passing it the action received from QWebView.

I'll give that a try. I have a slight complication in that I'm going to be using several of these widgets in one GUI, so I'll need to keep changing the action associated with each menu item or toolbutton each time the focus changes from one webkit widget to another.

Another approach I've tried is to use QWebView::pageAction() to get a pointer to the action, and then read the checked property of the action, but that doesn't seem to work, unfortunately; the bold action comes back unchecked even when the cursor is on bold text.

Thanks,
Martin