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

Additional Notes

Kurt Granroth <granroth@kde.org>

Details.. Details

In some apps, that would be it for the code. However, in most existing apps, there are some remnents of the menus and toolbars scattered about the other functions. We'll hunt them down quickly now.

This was a snippet of code that added a file to the Open Recent menu.

OLD: add_recent_file()
void TopLevel::add_recent_file(const QString &newfile)
{
  recentpopup->clear();

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

We replace it with this:

NEW: add_recent_file()
void TopLevel::add_recent_file(const KURL &url)
{
  KRecentFilesAction *recent;
  recent = (KListAction*)actionCollection()->action(KStdAction::stdName(KStdAction::OpenRecent));
  recent->addURL(url);
}

Okay, now I know that you jumped when you saw that second line of code there. I did that just to illustrate how you can access one of your actions without having to store any pointers. To quickly summarize what that's doing, QActionCollection::action() returns a pointer to a stored action. action() takes the internal name of the action as the parameter. This name is supplied by KStdAction::stdName() which returns the internal names of all of the standard actions.

If you had stored a pointer earlier in the constructor, you wouldn't have to do this.

Next, we look at the toggle functions. The toggleToolBar() and toggleStatusBar() functions are nearly identical functionally, so we'll only look at one of them:

OLD: toggleToolBar()
void TopLevel::toggleToolBar()
{
  show_toolbar = show_toolbar == true ? false : true;
  options->setItemChecked( toolID, show_toolbar );
  
  if(show_toolbar)
    enableToolBar( KToolBar::Show );
  else
    enableToolBar( KToolBar::Hide );
}

That was overkill. We can accomplish the same thing by doing:

NEW: toggleToolBar()
void TopLevel::toggleToolBar()
{
  if(toolBar()->isVisible())
    toolBar()->hide();
  else
    toolBar()->show();
}

Note that we no longer care about checking the options menu item, anymore. The action framework takes care of that for us.

Finally, the readSettings() and writeSettings() methods also need updating.. but there is nothing new there so I'll leave that as an exercise for the reader.


<< Prev Next >>