About a PyQt example program

前端 未结 3 354
感情败类
感情败类 2021-01-17 19:27

I\'m currently need GUI library for a project. I\'m familiar with python and found PyQt might be a good choice.

I\'m reading a tutorial about PyQt, and quite confuse

相关标签:
3条回答
  • 2021-01-17 19:37

    From PyQt docs:

    int QApplication.exec_ ()

    Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

    It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.

    From another source:

    sys.exit(app.exec_())

    Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends, if we call the exit() method or the main widget is destroyed. The sys.exit() method ensures a clean exit. The environment will be informed, how the application ended.

    The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_() was used instead.

    About painting:

    4.2.1. When Painting Occurs

    The paintEvent() method is called automatically when

    • Your widget is shown for the first time.

    • After a window has been moved to reveal some part (or all) of the widget.

    • The window in which the widget lies is restored after being minimized.

    • The window in which the widget lies is resized.

    • The user switches from another desktop to the desktop on which the widget's window lies.

    You can generate paint events manually by calling QWidget::update(). QWidget::update() erases the widget before generating the paint event. You can pass arguments to update(), which can restrict painting only to areas (rectangles, in particular) that need it. The two equivalent forms of the method are QWidget::update (int x, int y, int width, int height) and QWidget::update (QRect rectangle), where x and y give the upper-left corner of the rectangle, and width and height are obvious. Because update() places a paint event into the event queue, no painting occurs until the current method exits and control returns to the event handler. This is a good thing because other events may be waiting there to be processed, and events need to be processed in a timely manner for the GUI to operate smoothly.

    You can also invoke painting of the widget by calling QWidget::repaint (int x, int y, int width, int height, bool erase) (or one of several convenience-method forms), where all the arguments mean the same as in the case of the update() method, and erase tells repaint whether to erase the rectangle before painting it. repaint() calls paintEvent() directly. It does not place a paint event into the event queue, so use this method with care. If you try to call repaint() repeatedly from a simple loop to create an animation, for example, the animation will be drawn, but the rest of your user interface will be unresponsive because the events corresponding to mouse button clicks, keyboard presses, and so on will be waiting in the queue. Even if you are not performing a task as potentially time-consuming as animation, it is generally better to use update() to help keep your GUI alive.

    If you paint something on your widget outside the paintEvent(), you still need to include the logic and commands necessary to paint that same thing in paintEvent(). Otherwise, the painting you did would disappear the next time the widget is updated.

    0 讨论(0)
  • 2021-01-17 19:46

    app.exec_() starts the qt main loop, which ends when every created widget is destroyed (e.g. by closing its window). The paintEvent function is a method that you can overload from a QWidget subclass like the given Example class, which gets called when QT displays, updates or repaints the Widget.

    You can look up these things in the Qt documentation or the PyQt Documentation (which is mostly just a copy of the QT Documentation in a different format, but sometimes contains some valuable information regarding PyQt-specific things).

    0 讨论(0)
  • 2021-01-17 20:01

    It becomes more clear when one has some experience with low level programming, for example in Winapi or the X toolkit in C language. PyQt is a (very) high level toolkit. It comes with a huge built-in functionality. Similar example would require hundreds or maybe thousands of lines of C code. As a consequence, there is a lot of going on behind the scenes. Somebody already created code that deals with painting on a basic level. GUI programming is very complex and with modern GUI toolkits the application programmer is shielded from this complexity. It is inevitable that programmers are confused if they do not know all the technical details.

    In PyQt, we are essentially dealing with events in two ways. We connect signals to slots or reimplement event handlers (an event handler is a synonym for a slot). The provided example inherited from the QtGui.QWidget which already has some painting code available. In order to do our custom painting, we have to reimplement the existing paintEvent() event handler. Depending on the situation, we may or may not call the parent's paintEvent() method.

    The sys.exit(app.exec_()) does not call the paintEvent() method. The exec_() method starts an event loop. The event loop catches and dispatches events. Paint events are triggered by users or by the operating system. For example, when we launch the example, the paint event is triggered twice. (Put a print "Event occurred" line in the event handler to see how many times this method is called.) Resizing windows, loosing or gaining focus, minimizing or maximizing windows, all these cause painting events to be triggered.

    0 讨论(0)
提交回复
热议问题