Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Qt5 Python GUI Programming Cookbook
Qt5 Python GUI Programming Cookbook

Qt5 Python GUI Programming Cookbook: Building responsive and powerful cross-platform applications with PyQt

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Qt5 Python GUI Programming Cookbook

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 EditEdit Signals/Slots tool in the toolbar.

How to do it...

...

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...

Converting data types and making a small calculator

The most commonly used widget for accepting one-line data is the Line Edit widget, and the default data type in a Line Edit widget is string. In order to do any computation on two integer values, you need to convert the string data entered in the Line Edit widget to the integer data type and then convert the result of computation, which will be a numeric data type, back to string type before being displaying through a Label widget. This recipe does exactly that.

How to do it...

To understand how data is accepted by the user and how type casting is done, let's create an application based on the Dialog without Buttons template by performing the following steps:

  1. Add...

Using the Spin Box widget

The Spin Box widget is used for displaying integer values, floating-point values, and text. It applies a constraint on the user: the user cannot enter any random data, but can select only from the available options displayed through Spin Box. A Spin Box widget displays an initial value by default that can be increased or decreased by selecting the up/down button or up/down arrow key on the keyboard. You can choose a value that is displayed by either clicking on it or typing it in manually.

Getting ready

A Spin Box widget can be created using two classes, QSpinBox and QDoubleSpinBox, where QSpinBox displays only integer values, and the QDoubleSpinBox class displays floating-point values....

Using scrollbars and sliders

Scrollbars are useful while looking at large documents or images that cannot appear in a limited visible area. Scrollbars appear horizontally or vertically, indicating your current position in the document or image and the size of the region that is not visible. Using the slider handle provided with these bars, you can access the hidden part of the document or image.

Sliders are a way of selecting an integer value between two values. That is, a slider can represent a minimum and maximum range of values, and the user can select a value within this range by moving the slider handle to the desired location in the slider.

Getting ready

Scrollbars are used for viewing documents or images that are larger...

Using List Widget

To display several values in an easier and expandable format, you can use List Widget, which is an instance of the QListWidget class. List Widget displays several items that can not only be viewed, but can be edited and deleted, too. You can add or remove list items one at a time from the List Widget item, or collectively you can set list items by using its internal model.

Getting ready

Items in the list are instances of the QListWidgetItem class. The methods provided by QListWidget are shown in the following list:

  • insertItem(): This method inserts a new item with the supplied text into List Widget at the specified location.
  • insertItems(): This method inserts multiple items from the supplied list, starting...

Selecting multiple list items from one List Widget and displaying them in another

In the preceding application, you were selecting only a single diagnosis test from the List Widget item. What if I want to do multiple selections from the List Widget item? In the case of multiple selections, instead of a Line Edit widget, you need another List Widget to store the selected diagnosis test.

How to do it...

Let's create an application that displays certain diagnosis tests through List Widget and when user selects any test from List Widget, the selected test will be displayed in another List Widget:

  1. So, create a new application of the Dialog without Buttons template and drag and drop two Label widgets and two...

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 EditEdit 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...

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...

Converting data types and making a small calculator


The most commonly used widget for accepting one-line data is the Line Edit widget, and the default data type in a Line Edit widget is string. In order to do any computation on two integer values, you need to convert the string data entered in the Line Edit widget to the integer data type and then convert the result of computation, which will be a numeric data type, back to string type before being displaying through a Label widget. This recipe does exactly that.

How to do it...

To understand how data is accepted by the user and how type casting is done, let's create an application based on the Dialog without Buttons template by performing the following steps:

  1. Add three QLabel, two QLineEdit, and one QPushButton widget to the form by dragging and dropping three Label, two Line Edit, and four Push Button widgets on the form.
  2. Set the text property of the two Label widgets to Enter First Number and Enter Second Number.
  3. Set the objectName property...

Using the Spin Box widget


The Spin Box widget is used for displaying integer values, floating-point values, and text. It applies a constraint on the user: the user cannot enter any random data, but can select only from the available options displayed through Spin Box. A Spin Box widget displays an initial value by default that can be increased or decreased by selecting the up/down button or up/down arrow key on the keyboard. You can choose a value that is displayed by either clicking on it or typing it in manually.

Getting ready

A Spin Box widget can be created using two classes, QSpinBox and QDoubleSpinBox, where QSpinBox displays only integer values, and the QDoubleSpinBox class displays floating-point values. Methods provided by QSpinBox are shown in the following list:

  • value(): This method returns the current integer value selected from the spin box.
  • text(): This method returns the text displayed by the spin box.
  • setPrefix(): This method assigns the prefix text that is prepended to the value...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 30, 2018
Length: 462 pages
Edition : 1st
Language : English
ISBN-13 : 9781788830461
Vendor :
Qt
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 30, 2018
Length: 462 pages
Edition : 1st
Language : English
ISBN-13 : 9781788830461
Vendor :
Qt
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 115.97
Hands-On GUI Programming with C++ and Qt5
€36.99
Qt5 Python GUI Programming Cookbook
€41.99
Python GUI Programming with Tkinter
€36.99
Total 115.97 Stars icon
Banner background image

Table of Contents

14 Chapters
Creating a User Interface with Qt Components Chevron down icon Chevron up icon
Event Handling - Signals and Slots Chevron down icon Chevron up icon
Working with Date and Time Chevron down icon Chevron up icon
Understanding OOP Concepts Chevron down icon Chevron up icon
Understanding Dialogs Chevron down icon Chevron up icon
Understanding Layouts Chevron down icon Chevron up icon
Networking and Managing Large Documents Chevron down icon Chevron up icon
Doing Asynchronous Programming in Python Chevron down icon Chevron up icon
Database Handling Chevron down icon Chevron up icon
Using Graphics Chevron down icon Chevron up icon
Implementing Animation Chevron down icon Chevron up icon
Using Google Maps Chevron down icon Chevron up icon
Running Python Scripts on Android and iOS Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.4
(9 Ratings)
5 star 11.1%
4 star 0%
3 star 22.2%
2 star 55.6%
1 star 11.1%
Filter icon Filter
Top Reviews

Filter reviews by




Caroline Rose Sep 21, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I agree with the intro to this book that says Harwani can explain "even the most complicated topics in a straightforward and easily understandable fashion.” His organization and presentation reflect his experience teaching actual live students the topics he writes about. Each task section starts out with minimal introductory info, followed by clear, succinct, amply illustrated steps to take (“How to do it”), and only then does it give more details (“How it works”), which you can read to the extent that you want to or need to. I’m not a Python programmer and so have not gone through this particular book thoroughly, but I’ve used parts of other books by this author and found them to be excellent. What I mainly am is an experienced, very fussy technical writer and editor, and I don’t heap praise on authors lightly; in this case, I think it’s well deserved. You can’t go wrong with a book by B.M. Harwani.
Amazon Verified review Amazon
TORDJMAN Jan 03, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Good for base programming
Amazon Verified review Amazon
snigg Mar 13, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Its a cookbook. So there is almost zero technical background and sometimes one is asking whether the author really does know the things behind. Technical people who want to learn should find something else. This book is for the ones that want to do and do not ask how it works and why.For me as a none native english reader (obviously) the stereotypical figures of speech start to annoy me after a while. I wish that book publisher would invest more in correction and keep a cleaner english language.
Amazon Verified review Amazon
schloss5020 Aug 25, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Unter ein Kochbuch stelle ich mir etwas anderes vor
Amazon Verified review Amazon
Quel Geek Jun 30, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
If you've read no similar book then by all means consider this one. As I write, this book is the just latest of its sort. It probably fills a place in its publisher's catalogue. It is not a bad book but there were already others just as good. It covers no new ground and you won't be building "powerful" applications just because you read it. That book is yet to be written.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.