Qt5 Python GUI Programming Cookbook
eBook - ePub

Qt5 Python GUI Programming Cookbook

Building responsive and powerful cross-platform applications with PyQt

B.M. Harwani

Share book
  1. 462 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Qt5 Python GUI Programming Cookbook

Building responsive and powerful cross-platform applications with PyQt

B.M. Harwani

Book details
Book preview
Table of contents
Citations

About This Book

Over 60 recipes to help you design interactive, smart, and cross-platform GUI applications

Key Features

  • Get succinct QT solutions to pressing GUI programming problems in Python
  • Learn how to effectively implement reactive programming
  • Build customized applications that are robust and reliable

Book Description

PyQt is one of the best cross-platform interface toolkits currently available; it's stable, mature, and completely native. If you want control over all aspects of UI elements, PyQt is what you need. This book will guide you through every concept necessary to create fully functional GUI applications using PyQt, with only a few lines of code.

As you expand your GUI using more widgets, you will cover networks, databases, and graphical libraries that greatly enhance its functionality. Next, the book guides you in using Qt Designer to design user interfaces and implementing and testing dialogs, events, the clipboard, and drag and drop functionality to customize your GUI. You will learn a variety of topics, such as look and feel customization, GUI animation, graphics rendering, implementing Google Maps, and more. Lastly, the book takes you through how Qt5 can help you to create cross-platform apps that are compatible with Android and iOS. You will be able to develop functional and appealing software using PyQt through interesting and fun recipes that will expand your knowledge of GUIs

What you will learn

  • Use basic Qt components, such as a radio button, combo box, and sliders
  • Use QSpinBox and sliders to handle different signals generated on mouse clicks
  • Work with different Qt layouts to meet user interface requirements
  • Create custom widgets and set up customizations in your GUI
  • Perform asynchronous I/O operations and thread handling in the Python GUI
  • Employ network concepts, internet browsing, and Google Maps in UI
  • Use graphics rendering and implement animation in your GUI
  • Make your GUI application compatible with Android and iOS devices

Who this book is for

If you're an intermediate Python programmer wishing to enhance your coding skills by writing powerful GUIs in Python using PyQT, this is the book for you.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Qt5 Python GUI Programming Cookbook an online PDF/ePUB?
Yes, you can access Qt5 Python GUI Programming Cookbook by B.M. Harwani in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation en Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788830461

Event Handling - Signals and Slots

In this chapter, we will learn about the following topics:
  • Using Signal/Slot Editor
  • Copying and pasting text from one Line Edit widget to another
  • Converting data types and making a small calculator
  • Using the Spin Box widget
  • Using scrollbars and sliders
  • Using List Widget
  • Selecting multiple list items from one List Widget and displaying them in another
  • Adding items into List Widget
  • Performing operations in List Widget
  • Using the Combo Box widget
  • Using the Font Combo Box widget
  • Using the Progress Bar widget

Introduction

Event handling is an important mechanism in every application. The application should not only recognize the event, but must take the respective action to serve the event, too. The action taken on any event determines the course of the application. Each programming language has a different technique for handling or listening to events. Let's see how Python handles its events.

Using Signal/Slot Editor

In PyQt, the event handling mechanism is also known as signals and slots. An event can be in the form of clicking or double-clicking on a widget, or pressing the Enter key, or selecting an option from a radio button, checkbox, and so on. Every widget emits a signal when any event is applied on it and, that signal needs to be connected to a method, also known as a slot. A slot refers to the method containing the code that you want to be executed on the occurrence of a signal. Most widgets have predefined slots; you don't have to write code to connect a predefined signal to a predefined slot.
You can even edit a signal/slot by navigating to the Edit | Edit Signals/Slots tool in the toolbar.

How to do it...

To edit the signals and slots of different widgets placed on the form, you need to switch to signals and slots editing mode by performing the following steps:
  1. You can press the F4 key, navigate to the Edit | Edit Signals/Slots option, or select the Edit Signals/Slots icon from the toolbar. The mode displays all the signal and slot connections in the form of arrows, indicating the connection of a widget with its respective slot.
You can also create new signal and slot connections between widgets in this mode and delete an existing signal.
  1. To establish a signal and slot connection between two widgets in a form, select a widget by left-clicking the mouse on the widget, dragging the mouse towards another widget to which you want to connect, and releasing the mouse button over it.
  2. To cancel the connection while dragging the mouse, simply press the Esc key.
  3. On releasing the mouse over the destination widget, a Connection Dialog box appears, prompting you to select a signal from the source widget and a slot from the destination widget.
  4. After selecting the respective signal and slot, select OK to establish the signal and slot connection.
The following screenshot shows dragging a Push Button over a Line Edit widget:
  1. On releasing the mouse button on the Line Edit widget, you get the list of predefined signals and slots, as shown in the following screenshot:
You can also select Cancel in the Configure Connection dialog box to cancel the signal and slot connection.
  1. When connected, the selected signal and slot will appear as labels in the arrow, connecting the two widgets.
  2. To modify a signal and slot connection, double-click the connection path or one of its labels to display the Configure Connection dialog box.
  3. From the Configure Connection dialog, you can edit a signal or a slot as desired.
  4. To delete a signal and slot connection, select its arrow on the form and press the Delete key.
The signal and slot connection can also be established between any widget and the form. To do so, you can perform the following steps:
  1. Select the widget, drag the mouse, and release the mouse button over the form. The end point of the connection changes to the electrical ground symbol, representing that a connection has been established with the form.
  2. To come out of signal and slot editing mode, navigate to Edit | Edit Widgets or press the F3 key.

Copying and pasting text from one Line Edit widget to another

This recipe will make you understand how an event performed on one widget invokes a predefined action on the associated widget. Because we want to copy content from one Line Edit widget on clicking the push button, we need to invoke the selectAll() method on the occurrence of the pressed() event on push button. Also, we need to invoke the copy() method on occurrence of the released() event on the push button. To paste the content in the clipboard into another Line Edit widget on clicking of another push button, we need to invoke the paste() method on the occurrence of the clicked() event on another push button.

Getting ready

Let's create an application that consists of two Line Edit and two Push Button widgets. On clicking the first push button, the text in the first Line Edit widget will be copied and on clicking the second push button, the text copied from the first Line Edit widget will be pasted onto the second Line Edit widget.
Let's create a new application based on the Dialog without Buttons template by performing the following steps:
  1. Begin by adding QLineEdit and QPushButton to the form by dragging and dropping the Line Edit and Push Button widgets from the Widget box on the form.
To preview a form while editing, select either Form, Preview, or use Ctrl + R .
  1. To copy the text of the Line Edit widget when the user selects the push button on the form, you need to connect the push button's signal to the slot of Line Edit. Let's learn how to do it.

How to do it...

Initially, the form is in widget editing mode, and to apply signal and slot connections, you need to first switch to signals and slots editing mode:
  1. Select the Edit Signals/Slots icon from the toolbar to switch to signals and slots editing mode.
  1. On the form, select the push button, drag the mouse to the Line Edit widget, and release the mouse button. The Configure Connection dialog will pop up, allowing you to establish a signal and slot connection between the Push Button and the Line Edit...

Table of contents