[ Pobierz całość w formacie PDF ]
.Reading incomingIn the majority of cases, it is waiting for input from the mousemouse dataor keyboard, but the input can be from a light pen, a mousewheel, a graphics tablet, a trackball, or some other inputControlling the mousedevice.Most applications can be written using the predefinedand its cursorset of signals and slots that are included as part of the wid-gets, but it is sometimes necessary to attach your programInterpreting eventsdirectly to the incoming stream of events.Also, if you arefrom the keyboardgoing to create a widget of your own, you need to know howto translate the incoming events into signals.&' &' &' &'From a Port to a SlotThe following is a brief, simple description of the life cycle ofan event.An event starts with the hardware.The mouse is moved to anew location, a keyboard key is released, a mouse button ispressed, or a keyboard button is held down long enough forthe auto-repeat mechanism to kick in.The device issuing theevent is physically connected to the computer, so the eventwill cause an interrupt, and a small program  known as adevice driver  reads the information from the port.The pri-mary job of the device driver is to translate the hardwareevent into a software event.The device driver must be instructed to wait for events ona specific port.This is done from inside a program when itopens a port, in much the same way that an application wouldopen a file.The ports are all found in the /devdirectory, and 4682-1 ch08.f.qc 11/13/00 14:11 Page 176Part II &' Step by Step176a port can be addressed by a program just as if it were a file.When a programopens a port (that is, a name in the /devdirectory), the Linux kernel selects adevice driver and gives it the job of passing information back and forth betweenthe port and the application.For Qt and KDE, the application retrieving events from the driver is the X WindowingSystem.Each event from the device driver is formatted into a special internal formatknown as an XEvent.The window manager inspects the XEventto determine itsdestination window.If, for example, the event is a keyboard click, the event is to besent to the window that currently has the focus.If it is a mouse click, the x and ycoordinates of the mouse pointer usually determines which window.By knowingwhich window is to receive the event, the window manager can determine whichapplication is to receive the event.For an X Window program to process events, it sets up a loop to continuouslycall the low-level queue reading function XNextEvent().Each time this functionreturns, it has retrieved an event from the queue.The events are dispatched toyour application by calling a method assigned to handle events of its type.When your program calls QApplication::exec()or KApplication::exec()inits mainline, it executes a continuous loop, calling XNextEvent(), which reads anXEvent, translates it into one of the Qtevents, and calls the appropriate methodto deal with it.These event-receiving methods are defined as part of the QWidgetclass, and are declared as being both virtualand private.The actions performedby these methods are minimal, so it is necessary to create subclasses of QWidgetthat override the methods to receive the events.For example, the QButtonclassoverrides the QWidgetmethod named mouseReleaseEvent()so QButtoncanemit a clicked()signal.This enables your application to set up a slot to receivethe clicked()signal whenever the mouse is used to activate the button.The Mouse EventsA mouse event contains the current location of the mouse pointer and indicatorsof which, if any, buttons are currently being pressed.A method in the QWidgetclass is called whenever a mouse event arrives.If you wish to write your owndetailed mouse event processing, you can override the event handling methodsdefined in QWidget.The following program tracks the mouse and displays the buttons, keys, and posi-tion of the mouse pointer.As shown in Figure 8-1, the blank widget on the right isthe one being monitored for mouse movement, while each mouse action is listed onthe left.This example demonstrates how an incoming event is translated into a sig-nal.Whenever a mouse event arrives, the data from it is used to create a descrip-tive string.This string is emitted as a signal and is received by a slot of anotherclass. 4682-1 ch08.f.qc 11/13/00 14:11 Page 177Chapter 8 &' The Mouse and the Keyboard177Figure 8-1: A program to track and display mouse activityMouseSensor Header1 /* mousesensor [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • centka.pev.pl
  •