mardi 26 avril 2016

Installing Event Filters in Qt c++

One really powerful feature of Qt's event model is that a QObjectinstance can be set to monitor the events of another QObject instance before the latter object even sees them.
Let's suppose that we have a CustomerInfoDialogwidget composed of several QLineEdits and that we want to use the Space key to move the focus to the next QLineEdit. This non-standard behavior might be appropriate for an in-house application whose users are trained in its use. A straightforward solution is to subclass QLineEdit and reimplement
keyPressEvent()to call focusNextChild(), like this:

void MyLineEdit::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space) {
focusNextChild();
} else {
QLineEdit::keyPressEvent(event);
}
}

 This approach has one main disadvantage: If we use several different kinds of widgets in the form (e.g., QComboBoxes and QSpinBoxes), we must also subclass them to make them exhibit the same behavior. A better solution is to make CustomerInfoDialogmonitor its child widgets' key press events and implement the required behavior in the monitoring
code. This can be achieved using event filters. Setting up an event filter involves two steps:

 1. Register the monitoring object with the target object by calling installEventFilter()on the target.
2. Handle the target object's events in the monitor's eventFilter()function.
A good place to register the monitoring object is in the constructor:
CustomerInfoDialog::CustomerInfoDialog(QWidget *parent)
: QDialog(parent)
{
...
firstNameEdit->installEventFilter(this);
lastNameEdit->installEventFilter(this);
cityEdit->installEventFilter(this);
phoneNumberEdit->installEventFilter(this);
}
Once the event filter is registered, the events that are sent to the firstNameEdit, lastNameEdit, cityEdit, and phoneNumberEditwidgets are first sent to the CustomerInfoDialog's eventFilter()function before they are sent on to their intended destination.
Here's the eventFilter()function that receives the events:
bool CustomerInfoDialog::eventFilter(QObject *target, QEvent *event)
{
if (target == firstNameEdit || target == lastNameEdit
|| target == cityEdit || target == phoneNumberEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Space) {
focusNextChild();
return true;
}
}
}
return QDialog::eventFilter(target, event);
}
 First, we check to see whether the target widget is one of the QLineEdits. If the event was a key press, we cast it to
QKeyEventand check which key was pressed.
If the pressed key was Space, we call focusNextChild()to pass focus on to the next widget in the focus chain, and we return true to tell Qt that we have handled the event. If we returned false, Qt would send the event to its intended target, resulting in a spurious space being inserted into the QLineEdit.
If the target widget isn't a QLineEdit, or if the event isn't a Spacekey press, we pass control to the base class's implementation of eventFilter(). The target widget could also be some widget that the base class, QDialog, is monitoring. (In Qt 4.3, this is not the case for QDialog. However, other Qt widget classes, such as QScrollArea, do monitor some of their child widgets for various reasons.)
Qt offers five levels at which events can be processed and filtered:
We can reimplement a specific event handler.
Reimplementing event handlers such as mousePressEvent(), keyPressEvent(), and paintEvent()is by far the most common way to process events. We have already seen many examples of this.
1. We can reimplement QObject::event().
By reimplementing the event()function, we can process events before they reach the specific event handlers. This approach is mostly needed to override the default meaning of the Tab key, as shown earlier (p. 168). This is also used to handle rare types of events for which no specific event handler exists (e.g., QEvent::HoverEnter). When we reimplement event(), we must call the base class's event()function for handling the cases we don't explicitlyhandle.
2. We can install an event filter on a single QObject.
Once an object has been registered using installEventFilter(), all the events for the target object are first sent to the monitoring object's eventFilter()function. If multiple event filters are installed on the same object, the filters are activated in turn, from the most recently installed back to the first installed.
3. We can install an event filter on the QApplication object.
Once an event filter has been registered for qApp(the unique QApplicationobject), every event for every object in the application is sent to the eventFilter()function before it is sent to any other event filter. This approach is mostly useful for debugging. It can also be used to handle mouse events sent to disabled widgets, which QApplication normally discards.
4. We can subclass QApplication and reimplement notify().
Qt calls QApplication::notify()to send out an event. Reimplementing this function is the only way to get all the events, before any event filters get the opportunity to look at them. Event filters are generally more useful, because there can be any number of concurrent event filters, but only one notify()function.
5. Many event types, including mouse and key events, can be propagated. If the event has not been handled on the way to its target object or by the target object itself, the whole event processing process is repeated, but this time with the target object's parent as
the new target. This continues, going from parent to parent, until either the event is handled or the top-level object is reached.

how to install python in windows linux and mac os

Before we can begin programming you'll need to install software called the Python interpreter on your computer. (You may need to ask an adult for help here.) The interpreter is a program that understands the instructions that you’ll write (or rather, type out) in the Python language. Without the interpreter, your computer won't be able to run your Python programs. We'll just refer to ―the Python interpreter‖ as ―Python‖ from now on.
The Python interpreter software can be downloaded from the official website of the Python
programming language, http://www.python.org. You might want the help of someone else to download and install the Python software. The installation is a little different depending on if your computer’s operating system is Windows, Mac OS X, or a Linux OS such as Ubuntu. You can also find videos online of people installing the Python software on their computers at http://invpy.com/installing.

Windows Instructions:

 When you get to http://python.org, you should see a list of links on the left . Click on the Download link to go to the download page, then look for the file called ―Python 3.2 Windows Installer (Windows binary --does not include source)‖ and click on its link to download Python for Windows.
Double-click on the python-3.2.msi file that you've just downloaded to start the Python installer.
(If it doesn’t start, try right-clicking the file and choosing Install.) Once the installer starts up,
just keep clicking the Next button and just accept the choices in the installer as you go (no need to make any changes). When the install is finished, click Finish.

Mac OS X Instructions

 Mac OS X 10.5 comes with Python 2.5.1 pre-installed by Apple. Currently, Pygame only
supports Python 2 and not Python 3. However, the programs in this book work with both Python 2 and 3.
The Python website also has some additional information about using Python on a Mac at
http://docs.python.org/dev/using/mac.html.

Ubuntu and Linux Instructions

Pygame for Linux also only supports Python 2, not Python 3. If your operating system is Ubuntu, you can install Python by opening a terminal window (from the desktop click on Applications > Accessories > Terminal) and entering ―sudo apt-get install python2.7
 then pressing Enter. You will need to enter the root password to install Python, so ask the person who owns the computer to type in this password if you do not know it.
You also need to install the IDLE software. From the terminal, type in ―sudo apt-get
install idle‖. The root password is also needed to install IDLE (ask the owner of your
computer to type in this password for you)