Development/Tutorials/KDE2/KHello/Tutorial 4: Difference between revisions

From KDE TechBase
< Development‎ | Tutorials‎ | KDE2‎ | KHello
m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
Line 2: Line 2:
Now we will make our users very happy by adding a menu!
Now we will make our users very happy by adding a menu!
==Source code==
==Source code==
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
/************* khello.h *******************/
/************* khello.h *******************/
// From KHello3
// From KHello3
Line 31: Line 31:
};
};
</code>
</code>
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
/************* khello.cc ******************/
/************* khello.cc ******************/
// From KHello3
// From KHello3

Revision as of 20:32, 29 June 2011

Description

Now we will make our users very happy by adding a menu!

Source code

<syntaxhighlight lang="cpp-qt" line> /************* khello.h *******************/ // From KHello3

  1. include <kapp.h>
  2. include <kmainwindow.h>
  3. include <kmessagebox.h>
  4. include <qpushbutton.h>

// end From KHello3

  1. include <kmenubar.h>
  2. include <kpopupmenu.h>

// From KHello3 class KHello : public KMainWindow {

 Q_OBJECT

public:

 KHello();
 void closeEvent(QCloseEvent *);

public slots:

 void slotHello();
 void slotExit();

private:

 QPushButton *btnHello;
 QPushButton *btnExit;

// end From KHello3

 KMenuBar *menu;
 KPopupMenu *file, *help;

}; <syntaxhighlight lang="cpp-qt" line> /************* khello.cc ******************/ // From KHello3

  1. include "khello.moc"
  2. include <kmessagebox.h>

KHello::KHello() : KMainWindow() {

 btnHello = new QPushButton("Hello", this);
 btnHello->setGeometry(45,30,50,25);
 btnHello->show();
 connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));
 btnExit = new QPushButton("Exit", this);
 btnExit->setGeometry(105,30,50,25);
 btnExit->show();
 connect(btnExit, SIGNAL(clicked()), this, SLOT(slotExit()));

// end From KHello3

 file = new KPopupMenu();
 file->insertItem("&Hello", this, SLOT(slotHello()));
 file->insertItem("&Exit", this, SLOT(slotExit()));
 
 help = helpMenu("KHello\nby Daniel Marjamäki\nUpdated by David Leimbach");
 
 menu = menuBar();
 menu->insertItem("&File", file);
 menu->insertItem("&Help", help);

} // From KHello3 void KHello::closeEvent(QCloseEvent *e) {

 kapp->beep();
 KMainWindow::closeEvent(e);

}

void KHello::slotHello() {

 KMessageBox::information(this, "Hello World!", "Important");

}

void KHello::slotExit() {

 close();

} // end From KHello3

Important code

This code was simple and didn't need any explaining. However, I think one line was important, and will comment it. <syntaxhighlight lang="cpp-qt"> help = helpMenu("KHello\nby Daniel Marjamäki\nUpdated by David Leimbach"); KMainWindow has a built in function which generates a help menu. Use it, in order to make your application consistent to other KDE programs.

Compiling

[on linux where KDEDIR and QTDIR contain the paths to where KDE and Qt are installed respectively] [FreeBSD 5.x users may omit the -ldl]

g++ -c -I$KDEDIR/include -I$QTDIR/include -fno-rtti main.cc
moc khello.h -o khello.moc
g++ -c -I$KDEDIR/include -I$QTDIR/include -fno-rtti khello.cc
g++ -L$KDEDIR/lib -L$QTDIR/lib -lkdeui -lkdecore -lqt -ldl -o khello main.o khello.o