#include <kde/kaction.h> KAction (const QString &text, const KShortcut &cut, const QObject *receiver, const char *slot, QObject *parent, const char *name = 0) # 必須輸入文字、快速鍵、圖示和一個 slot,slot 為 KAction 被選定時被呼叫。
KAction *new_file = new KAction("New", "filenew", KstdAccel::key(KstdAccel::New), this, SLOT(newFile()), this, "newaction");
new_file
加入選單、工具列:
new_file->plug(a_menu); new_file->plug(a_toolbar);
new_file
。
new_file->setEnabled(FALSE);
#include <kde/kaction.h> KAction * openNew (const QObject *recvr, const char *slot, KActionCollection* parent, const char *name = 0 ) KAction * save ... KAction * saveAs ... KAction * revert ... KAction * close ... KAction * print ... etc...
KAction *saveas = KStdAction::saveAs(this, SLOT(saveAs()), actionCollection(),"saveas");
#include <kmainwindow.h> class KDEMenu : public KMainWindow { Q_OBJECT public: KDEMenu(const char * name = 0); private slots: void newFile(); void aboutApp(); };
#include "KDEMenu.h" #include <kapp.h> #include <kaction.h> #include <kstdaccel.h> #include <kmenubar.h> #include <kaboutdialog.h>
KDEMenu::KDEMenu(const char *name = 0) : KMainWindow (0L, name ) { KAction *new_file = new KAction("New", "filenew", KstdAccel::key(KstdAccel::New), this, SLOT(newFile()), this, "newaction"); KAction *quit_action = KStdAction::quit( KApplication::kApplication(), SLOT(quit()), actionCollection()); KAction *help_action = KStdAction::aboutApp(this, SLOT(aboutApp()), actionCollection());
QPopupMenu *file_menu = new QPopupMenu; QPopupMenu *help_menu = new QPopupMenu; menuBar()->insertItem("&File", file_menu); menuBar()->insertItem("&Help", help_menu);
new_file
和 quit_action
之間插入一個分隔行:
new_file->plug(file_menu); file_menu->insertSeparator(); quit_action->plug(file_menu); help_action->plug(help_menu); new_file->plug(toolBar()); quit_action->plug(toolBar()); }
void KDEMenu::newFile() { // Create new File } void KDEMenu::aboutApp() { KAboutDialog *about = new KAboutDialog(this, "dialog"); about->setAuthor(QString("A. N. Author"), QString("an@email.net"), QString("http://url.com"), QString("work")); about->setVersion("1.0"); about->show(); } int main(int argc, char **argv) { KApplication app( argc, argv, "cdapp" );; KDEMenu *window = new KDEMenu("kdemenu"); app.setMainWidget(window); window->show(); return app.exec(); }
TARGET = kdemenu MOC_DIR = moc OBJECTS_DIR = obj INCLUDEPATH = /usr/include/kde QMAKE_LIBDIR_X11 += -L$KDEDIR/lib QMAKE_LIBS_X11 += -lkdeui -lkdecore SOURCES = KDEMenu.cpp HEADERS = KDEMenu.h
$ qmake menu.prop -o Makefile $ make $ ./kdemenu