KDE UI Tutorial
Preface: Conventions
Step 0: Getting Started
Step 1: Initial Code
   Header File
   Source File
Step 2: Core Dump
Step 2: Line by Line
Step 3: Detail... Details..
Step 4: App Specific Details

Additional Notes

Kurt Granroth <granroth@kde.org>

The Header File

We'll start with 'kedit.h'. Here are the relevant (heavily edited) parts to the file that deal with the menus and toolbar.

OLD: kedit.h
#ifndef _KEDIT_H_
#define _KEDIT_H_

class QPopupMenu;

class TopLevel : public KMainWindow
{
    QPopupMenu *file, *edit, *options, *recentpopup;

protected:
    void setupMenuBar();
    void setupToolBar();
};
#endif

This is very standard: a bunch of pointers to menus and a function each to setup the menubar and the toolbar. In our new framework, we don't need any of that. A single method to setup our actions will suffice.

NOTE: as you will see later, you can access all of your actions in any class method without storing a pointer to them. However, if you anticipate using your actions a lot later, you can store a pointer here.

NEW: kedit.h
#ifndef _KEDIT_H_
#define _KEDIT_H_

class TopLevel : public KMainWindow
{
protected:
    void setupActions();
};
#endif

The Source File

This is where the vast bulk of the work is done. I like to open up the file with a split window. In the top window, I create my 'setupActions()' function while I'm deleting the code in 'setupMenubar()' and 'setupToolbar()' functions in the lower window.

All of the methods shown here are heavily edited to show only the relevant code (and maybe a few lines before and after).

First, we start with the original constructor:

OLD: Constructor
#include <qpopupmenu.h>
#include <ktoolbar.h>

TopLevel::TopLevel (QWidget *, const char *name)
    : KMainWindow ( name)
{
  setupMenuBar();
  setupToolBar();
  setupStatusBar();

  readSettings();

  recentpopup->clear();

  for ( int i =0 ; i < (int)recent_files.count(); i++)
    recentpopup->insertItem(recent_files[i], i, i);

  options->setItemChecked( toolID, show_toolbar );
  options->setItemChecked( statusID, show_statusbar );
}

The 'recentpopup' item is a QPopupMenu containing the most recently opened files. 'options' is also a QPopupMenu

This is all much too verbose. Let's consolidate this to:

NEW: Constructor
#include <kaction.h>
#include <kstdaction.h>

TopLevel::TopLevel (QWidget *, const char *name)
    : KMainWindow ( name)
{
  setupStatusBar();
  setupActions();

  readSettings();
}

Then, we get to the destructor. Typically, you need to delete your menus, here.

OLD: Destructor
TopLevel::~TopLevel()
{
  delete file;
  delete edit;
  delete options;
  delete recentpopup;
}

With our new framework, all of our UI objects are deleted for us behind the scenes.

NEW: Destructor
TopLevel::~TopLevel()
{
}

We'll continue on to the bulk of the work on the next page.


<< Prev Next >>