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

From KDE TechBase
< Development‎ | Tutorials‎ | KDE2‎ | KHello
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
m (Text replace - "</code>" to "</syntaxhighlight>")
 
Line 22: Line 22:
   QPushButton *btnExit;
   QPushButton *btnExit;
};
};
</code>
</syntaxhighlight>
<syntaxhighlight lang="cpp-qt" line>
<syntaxhighlight lang="cpp-qt" line>
/************* khello.cc ******************/
/************* khello.cc ******************/
Line 56: Line 56:
   close();
   close();
}
}
</code>
</syntaxhighlight>
==Explaining the code==
==Explaining the code==
I'll only explain what I believe is necessary this time.
I'll only explain what I believe is necessary this time.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
public slots:
public slots:
</code>
</syntaxhighlight>
This section is new, and it used by the meta object compiler. In the QT world, slots are event-handlers and signals are events. Put all your event handlers in this section.
This section is new, and it used by the meta object compiler. In the QT world, slots are event-handlers and signals are events. Put all your event handlers in this section.
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));
connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));
</code>
</syntaxhighlight>
Connect the event "clicked" from the btnHello object to the event handler "slotHello".
Connect the event "clicked" from the btnHello object to the event handler "slotHello".



Latest revision as of 20:52, 29 June 2011

Description

Now is the time to add a few buttons.

Source code

The old code is gray. I have not included main.cc below, because it is the same as in KHello #2.

/************* khello.h *******************/
#include <kapp.h>                 // From KHello2
#include <kmainwindow.h>          // From KHello2
#include <qpushbutton.h>

class KHello : public KMainWindow // From KHello2
{                                 //  |
  Q_OBJECT                        //  |
public:                           // From KHello2
  KHello();
  void closeEvent(QCloseEvent *); // From KHello2
public slots:
  void slotHello();
  void slotExit();
private:
  QPushButton *btnHello;
  QPushButton *btnExit;
};
/************* khello.cc ******************/
#include "khello.moc"             // From KHello2
#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()));  
}

void KHello::closeEvent(QCloseEvent *e) // From KHello2
{                                       //  |
  kapp->beep();                         //  |
  KMainWindow::closeEvent(e);           //  |
}                                       // From KHello2

void KHello::slotHello()
{
  KMessageBox::messageBox(0, KMessageBox::Information, "Hello World!");
}

void KHello::slotExit()
{
  close();
}

Explaining the code

I'll only explain what I believe is necessary this time.

public slots:

This section is new, and it used by the meta object compiler. In the QT world, slots are event-handlers and signals are events. Put all your event handlers in this section.

connect(btnHello, SIGNAL(clicked()), this, SLOT(slotHello()));

Connect the event "clicked" from the btnHello object to the event handler "slotHello".

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

[if moc is not in your path try $QTDIR/bin/moc]