The K Desktop Environment

Next Previous Table of Contents

7. Provided Views

As mentioned earlier, a usual application interface contains a so-called "view" or "content" area, usually the center widget surrounded by menubar, toolbars and a statusbar. Besides the required construction of a view widget by inheritance of QWidget, the KDE and Qt libraries offer several complete widgets that can serve as view areas already. These are widgets that are used commonly over a lot of applications and do not necessarily have to be used as a main view; they may also get used in dialogs.

Qt only offers one of these: the QMultiLineEdit class, offering a text editing widget. By using a QMultiLineEdit, a developer can create a full-functional editor rapidly. The class interface may also serve you as an example what functionality a view area should provide.

KDE on the other hand contains two complete widgets. One is again an editor widget, derived from QMultiLineEdit and is encapsulated in the KEdit class of the kdeui library; the other a HTML- interpreter widget, KHTMLWidget. The following will discuss these classes and explain the interface usage by example.

7.1 The KEdit View

As mentioned, the KEdit view is a full-functional editor class that offers a complete interface to build an editor. As it inherits QMultiLineEdit, it makes use of Qt's basic functionality with the intention to complete the interface beyond the facilities a text editor view should provide such as insertion or clipboard communication.

The idea is to construct a KTMainWindow based main view with the according items like a menubar, toolbar and statusbar. The slots that get called by the popup menus or the toolbar icons are all placed as public methods within the KEdit class. This requires the implementation of slots on the main widget that call the according methods.

An example containing a slot implementation and the use of a slot provided already by QMultiLineEdit:


void MyEditor::initMenuBar(){

  // create the "Edit" menu
  edit_menu= new QPopupMenu();
  // here we call a slot selectAll() provided by QMultiLineEdit to select the whole text:
  edit_menu->insertItem(i18n("&Select All"), kedit, SLOT(selectAll()), 0, ID_EDIT_SELECT_ALL);

  // construct a view-menu and insert a menuentry "Font"
  view_menu= new QPopupMenu();
  // we have to call a self-created slot to call
  // the public method selectFont() of KEdit
  view_menu->insertItem(i18n("&Font"), this, SLOT(slotViewFont()), 0, ID_VIEW_FONT);


  // insert the view_menu into the menubar
  menuBar()->insertItem(i18n("&Edit"), edit_menu);
  menuBar()->insertItem(i18n("&View"), view_menu);
}

void MyEditor::slotViewFont(){
  // call a method of KEdit to call the font selection dialog
  selectFont ();
}

7.2 The KHTML View

The second view area provided by KDE is originally the KHTMLWidget. This class actually is the visible widget that interprets HTML files including graphics, tables etc. What it doesn't offer is a scrolling functionality which limits the use of the widget. Therefore, the class KHTMLView is introduced which serves as a wrapper class that offers this and other functionality.

A lot of applications make wide use of either KHTMLWidget, adding the scrolling functionality by code, or using KHTMLView directly, such as KDEHelp, KDevelop and KFM.

The main reason for a complete HTML widget is that today's applications tend to use HTML-based help functionality almost everywhere, so a reuse of this funcitionality is very certain.

The usage can be compared to the implementation example of the last chapter for KEdit, as KHTMLView provides signals, slots and public methods as well that can be called similar to provide a user interface that handles the widget. When it comes to configuring the behavoir of the displaying KHTMLWidget, you have to create a pointer to a KHTMLWidget and retrieve the instance of your KHTMLView's widget by calling getKHTMLWidget(). Then you can set options like color settings and font sizes for your view area.

Comment

In KDE2 you will have an extended KHTMLWidget that directly inherits QScrollView, so scrolling is provided by default. KHTMLView is removed instead. Also, the new HTML widget provides support for Java Script.

Next Previous Table of Contents