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
R$49.99 R$245.99
Paperback
R$306.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781788831000
Vendor :
Qt
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 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
R$500 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 R$25 each
Feature tick icon Exclusive print discounts
R$800 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 R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 852.97
Hands-On GUI Programming with C++ and Qt5
R$272.99
Qt5 Python GUI Programming Cookbook
R$306.99
Python GUI Programming with Tkinter
R$272.99
Total R$ 852.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.