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:
- Add three
QLabel
, twoQLineEdit
, and oneQPushButton
widget to the form by dragging and dropping threeLabel
, twoLine Edit
, and fourPush Button
widgets on the form. - Set the
text
property of the twoLabel
widgets toEnter First Number
andEnter Second Number
. - Set the
objectName
property...