[ Pobierz całość w formacie PDF ]
.cxxmoc_up_down.cxx: up_down.hxx$(MOC) up_down.hxx -o moc_up_down.cxxPROGRAMMINGUSINGQTGUI3672316072 CH31 7/26/99 2:08 PM Page 554Programming the User Interface554PART IVAssume the $(MOC) refers to the absolute path to the moc utility in the Qt distribution sbin directory.If the header file is changed, a new source file (here, moc_state_lcd.cxxand/or moc_up_down.cxx) is automatically generated by moc.The following rules in theMakefile file compile these generated source files:moc_state_lcd.o: moc_state_lcd.cxx state_lcd.hxxmoc_up_down.o: moc_up_down.cxx up_down.hxxMakefile contains rules for building.o files from C or C++ source files.The generatedmoc files contain code required for a widget to both declare slots and to bind signals tothe slots contained in other widgets.Using Signals and SlotsWe developed the StateLCDWidget class in the last section as an example of a simpleclass that defines slots that other Qt widgets can use.In this section, we will developanother simple widget class UpDownWidget that contains one instance of the widgetclasses StateLCDWidget and two instances of the Qt QPushButton widget class.Onepush button will have its clicked signal bound to the decreaseValue slot of the instanceof class StateLCDWidget and the other push button will have its clicked signal bound tothe increaseValue slot of the instance of StateLCDWidget.The class header file up_down.hxx requires three include files to define the QWidget,QPushButton, and StateLCDWidget classes:#include#include#include state_lcd.hxxThe class definition for UpDownWidget uses two tokens that are defined away for theC++ compiler, but that have special meaning for the moc utility: Q_OBJECT and signals.As we saw in the last section, the Q_OBJECT symbol causes the moc utility to generate so-called meta object protocol information to support binding code to slots of a specificobject, rather than all instances of a class.The moc utility uses the symbol signals in a class definition to determine which methodscan be called for specific instances of the class through slot connections.The importantthing to understand here is that just because an object has defined slots, an applicationprogram might not bind these slots to signals from another widget object.This bindingoccurs on an object-to-object basis and not to all instances of a class.The class definitionfor UpDownWidget shows that three other widget objects are contained in this class: twopush-button objects and a StateLCDWidget object:class UpDownWidget : public QWidget {Q_OBJECT3672316072 CH31 7/26/99 2:08 PM Page 555GUI Programming Using Qt555CHAPTER 31public:31UpDownWidget(QWidget *parent=0, const char *name=0);protected:void resizeEvent(QResizeEvent *);private:QPushButton *up;QPushButton *down;StateLCDWidget *lcd;};The class definition in the up_down.cxx file contains the definition of the class construc-tor and the resizeEvent method.The constructor definition shows how to bind signalsof one widget to the slot of another:UpDownWidget::UpDownWidget( QWidget *parent, const char *name ): QWidget( parent, name ){lcd = new StateLCDWidget(parent, name);lcd->move( 0, 0 );up = new QPushButton( Up , this);down = new QPushButton( Down , this);connect(up, SIGNAL(clicked()), lcd, SLOT(increaseValue()) );connect(down, SIGNAL(clicked()), lcd, SLOT(decreaseValue()) );}The connect method is inherited from the QObject which is the base class for QWidget.The method signature, defined in the qobject.h file in Qt distribution s src/kerneldirectory, is as follows:bool connect(const QObject *sender, const char *signal,const char *member );The SIGNAL macro is defined in the qobjectdefs.h file as the following:#define SIGNAL(a) 2 #aIf you add the following line of code to the end of the class constructor:printf( SIGNAL(clicked()) = |%s|\n , SIGNAL(clicked()));you will see the following output from the SIGNAL macro when the constructor executes:SIGNAL(clicked()) = |2clicked()|The code generated by moc will recognize this character string (generated by the SIGNALmacro) and bind the correct signal at runtime.The SLOT macro is defined in the qob-jectdefs.h file as follows:#define SLOT(a) 1 #aPROGRAMMINGUSINGQTGUI3672316072 CH31 7/26/99 2:08 PM Page 556Programming the User Interface556PART IVIf you add the following line of code to the end of the class constructor:printf( SLOT(increaseValue()) = |%s|\n , SLOT(increaseValue()));you will see the following output from the SIGNAL macro:SLOT(increaseValue()) = |1increaseValue()|The code generated by moc will recognize this character string (generated by the SLOTmacro) and bind the correct member function at runtime.The resizeEvent method sim-ply re-sizes the StateLCDWidget widget and repositions the two Push Button widgets:void UpDownWidget::resizeEvent(QResizeEvent *) {lcd->resize(width(), height() - 59);up->setGeometry(0, lcd->height() + 5, width(), 22);down->setGeometry(0, lcd->height() +31, width(), 22);}Here, the width and height of UpDownWidget are used to calculate the position and size ofthe three widgets contained in UpDownWidget.Running the Signal/Slot Example ProgramWe now have developed the StateLCDWidget and UpDownWidget widgets.The main.cxxfile contains a simple example program to test these widgets:#include#include up_down.hxxint main(int argc, char **argv) {QApplication a(argc, argv);QWidget top;top.setGeometry(0, 0, 222, 222);UpDownWidget w(&top);w
[ Pobierz całość w formacie PDF ]