When we use existing widgets, PyQt's signals and slots mechanism is often all we need to get the behaviors we want. But when we create custom widgets—for example, to exercise fine control over the appearance and behavior of a widget—we must reimplement low-level event handlers.
For appearance, reimplementing paintEvent() is often sufficient, although in some cases we may also need to reimplement resizeEvent(). We normally don't call the base class implementation for these events. For behavior it is common to reimplement keyPressEvent() and some of the mouse events such as mouse-PressEvent() or mouseMoveEvent(). We often don't call the base class implementation for mouse events, although it is usually harmless to do so. If we exercise lower-level control by reimplementing QWidget.event(), we must return True for those events that we handle ourselves, and we must return the result of calling the base class implementation for those events that we don't handle.
We don't often need to handle the clipboard in our own code, since most of PyQt's text editing widgets automatically interact with the clipboard in the way we would expect. But if we do want to work with the clipboard in code, setting and getting text and image data is straightforward, using the QClipboard object returned by QApplication.clipboard(). Setting HTML data is slightly more involved since we must wrap the HTML in a QMimeData object, although retrieving HTML is easy. When we use MIME data with the clipboard we are not limited to HTML; we can store and retrieve any kind of data by using the same techniques we used to handle drag-and-drop data.
The built-in drag-and-drop support provided by the standard PyQt widgets is very easy to set up and use. In some cases though, we need to drag and drop our own custom data types. The code required to do so is not difficult to write, and using QByteArray ensures that we can drag and drop any amount of data of any C++ or PyQt data type. However, if the amount of data is very large, it may be faster and less memory demanding to pass a token to stand for the data (say, an index position in a data structure), rather than the data itself, and actually copy data only when necessary.
It is also possible to bypass PyQt's drag-and-drop facilities entirely, and implement our own drag-and-drop system by reimplementing the mouse event handlers. This is not as difficult as it sounds, but it is clearly less convenient than using what PyQt already provides.
PyQt's event-handling system is very powerful, and yet quite easy to use. In most cases using the higher-level signals and slots mechanism is much easier and is more appropriate. But when we need fine control and customization, reimplementing event handlers will let us get the precise appearance and behavior we want—and we will see this in action in the next chapter, when we implement some custom widgets.
Was this article helpful?
The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.