Qt5 Slots Example

2021年10月8日
Register here: http://gg.gg/w5yeq
*Qt5 Signal Slot Example
*Qt Slot Example
*Qt5 Slots Examples
*Qt5 Slot Example
Qt already provides signals and slots for its classes, which you can use in your application. For example, QPushButton has a signal clicked, which will be triggered when the user clicks on the button. The QApplication class has a slot quit function, which can be called when you want to terminate your application. PyQt examples 2020. These PyQt examples show you how to create a desktop app with Python and Qt. Start with ’Hello World’ or browse the official PyQt demos. You can run every example yourself on Windows, Mac or Linux. All you need is Python 3. For instructions, please see below. The following are 30 code examples for showing how to use PyQt5.QtCore.pyqtSlot.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don’t like, and go to the original project or source file by following the links above each example. Qt5 Tutorial: QTcpSocket with Signals and Slots. In this tutorial, we will learn how to download a file using QTcpSocket.This is a continued tutorial from the previous one, Qt 5 QTcpSocket. Qt 5 Hello World Tutorial Steps. The following steps show how to create the Qt5 Hello World application using Qt Creator. Before continuing, be sure to install Qt Creator and other required software packages.
This example was ported from the PyQt4 version by Guðjón Guðjónsson. Introduction
In some applications it is often necessary to perform long-running tasks, such as computations or network operations, that cannot be broken up into smaller pieces and processed alongside normal application events. In such cases, we would like to be able to perform these tasks in a way that does not interfere with the normal running of the application, and ensure that the user interface continues to be updated. One way of achieving this is to perform these tasks in a separate thread to the main user interface thread, and only interact with it when we have results we need to display.
This example shows how to create a separate thread to perform a task - in this case, drawing stars for a picture - while continuing to run the main user interface thread. The worker thread draws each star onto its own individual image, and it passes each image back to the example’s window which resides in the main application thread. The User Interface
We begin by importing the modules we require. We need the math and random modules to help us draw stars.
The main window in this example is just a QWidget. We create a single Worker instance that we can reuse as required.
The user interface consists of a label, spin box and a push button that the user interacts with to configure the number of stars that the thread wil draw. The output from the thread is presented in a QLabel instance, viewer.
We connect the standard finished() and terminated() signals from the thread to the same slot in the widget. This will reset the user interface when the thread stops running. The custom output(QRect, QImage) signal is connected to the addImage() slot so that we can update the viewer label every time a new star is drawn.
The start button’s clicked() signal is connected to the makePicture() slot, which is responsible for starting the worker thread.
We place each of the widgets into a grid layout and set the window’s title:
The makePicture() slot needs to do three things: disable the user interface widgets that are used to start a thread, clear the viewer label with a new pixmap, and start the thread with the appropriate parameters.
Since the start button is the only widget that can cause this slot to be invoked, we simply disable it before starting the thread, avoiding problems with re-entrancy.
We call a custom method in the Worker thread instance with the size of the viewer label and the number of stars, obtained from the spin box.
Whenever is star is drawn by the worker thread, it will emit a signal that is connected to the addImage() slot. This slot is called with a QRect value, indicating where the star should be placed in the pixmap held by the viewer label, and an image of the star itself:
We use a QPainter to draw the image at the appropriate place on the label’s pixmap.
The updateUi() slot is called when a thread stops running. Since we usually want to let the user run the thread again, we reset the user interface to enable the start button to be pressed:
Now that we have seen how an instance of the Window class uses the worker thread, let us take a look at the thread’s implementation. The Worker Thread
The worker thread is implemented as a PyQt thread rather than a Python thread since we want to take advantage of the signals and slots mechanism to communicate with the main application.
We define size and stars attributes that store information about the work the thread is required to do, and we assign default values to them. The exiting attribute is used to tell the thread to stop processing.
Each star is drawn using a QPainterPath that we define in advance:
Before a Worker object is destroyed, we need to ensure that it stops processing. For this reason, we implement the following method in a way that indicates to the part of the object that performs the processing that it must stop, and waits until it does so. Qt5 Signal Slot Example
For convenience, we define a method to set up the attributes required by the thread before starting it.
The start() method is a special method that sets up the thread and calls our implementation of the run() method. We provide the render() method instead of letting our own run() method take extra arguments because the run() method is called by PyQt itself with no arguments.
The run() method is where we perform the processing that occurs in the thread provided by the Worker instance:
Information stored as attributes in the instance determines the number of stars to be drawn and the area over which they will be distributed.
We draw the number of stars requested as long as the exiting attribute remains False. This additional check allows us to terminate the thread on demand by setting the exiting attribute to True at any time.
The drawing code is not particularly relevant to this example. We simply draw on an appropriately-sized transparent image.
For each star drawn, we send the main thread information about where it should be placed along with the star’s image by emitting our custom output() signal:

Since QRect and QImage objects can be serialized for transmission via the signals and slots mechanism, they can be sent between threads in this way, making it convenient to use threads in a wide range of situations where built-in types are used. Running the Example
We only need one more piece of code to complete the example:
Demonstrates multi-thread programming using Qt.
Contents:Overview
In the Custom Type Example, we showed how to integrate custom types with the meta-object system, enabling them to be stored in QVariant objects, written out in debugging information and used in signal-slot communication.
In this example, we create a new value class, Block, and register it with the meta-object system to enable us to send instances of it between threads using queued signals and slots.The Block Class
The Block class is similar to the Message class described in the Custom Type Example. It provides the default constructor, copy constructor and destructor in the public section of the class that the meta-object system requires. It describes a colored rectangle.
We will still need to register it with the meta-object system at run-time by calling the qRegisterMetaType() template function before we make any signal-slot connections that use this type. Even though we do not intend to use the type with QVariant in this example, it is good practice to also declare the new type with Q_DECLARE_METATYPE().
The implementation of the Block class is trivial, so we avoid quoting it here.The Window Class
We define a simple Window class with a public slot that accepts a Block object. The rest of the class is concerned with managing the user interface and handling images.
The Window class also contains a worker thread, provided by a RenderThread object. This will emit signals to send Block objects to the window’s addBlock(Block) slot.
The parts of the Window class that are most relevant are the constructor and the addBlock(Block) slot.
The constructor creates a thread for rendering images, sets up a user interface containing a label and two push buttons that are connected to slots in the same class.
In the last of these connections, we connect a signal in the RenderThread object to the addBlock(Block) slot in the window.Qt Slot Example
The rest of the constructor simply sets up the layout of the window.
The addBlock(Block) slot receives blocks from the rendering thread via the signal-slot connection set up in the constructor:
We simply paint these onto the label as they arrive.The RenderThread ClassQt5 Slots Examples
The RenderThread class processes an image, creating Block objects and using the sendBlock(Block) signal to send them to other components in the example.
The constructor and destructor are not quoted here. These take care of setting up the thread’s internal state and cleaning up when it is destroyed.
Processing is started with the processImage() function, which calls the RenderThread class’s reimplementation of the QThread::run() function:
Ignoring the details of the way the image is processed, we see that the signal containing a block is emitted in the usual way:
Each signal that is emitted will be queued and delivered later to the window’s addBlock(Block) slot.Registering the Type
In the example’s main() function, we perform the registration of the Block class as a custom type with the meta-object system by calling the qRegisterMetaType() template function:
This call is placed here to ensure that the type is registered before any signal-slot connections are made that use it.
The rest of the main() function is concerned with setting a seed for the pseudo-random number generator, creating and showing the window, and setting a default image. See the source code for the implementation of the createImage() function.Further ReadingQt5 Slot Example
This example showed how a custom type can be registered with the meta-object system so that it can be used with signal-slot connections between threads. For ordinary communication involving direct signals and slots, it is enough to simply declare the type in the way described in the Custom Type Example.
In practice, both the Q_DECLARE_METATYPE() macro and the qRegisterMetaType() template function can be used to register custom types, but qRegisterMetaType() is only required if you need to perform signal-slot communication or need to create and destroy objects of the custom type at run-time.
More information on using custom types with Qt can be found in the Creating Custom Qt Types document.
Files:
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Register here: http://gg.gg/w5yeq

https://diarynote.indered.space

コメント

最新の日記 一覧

<<  2025年7月  >>
293012345
6789101112
13141516171819
20212223242526
272829303112

お気に入り日記の更新

テーマ別日記一覧

まだテーマがありません

この日記について

日記内を検索