2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2012-09-19 12:28:29 +00:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the documentation of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:FDL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Free Documentation License Usage
|
2011-04-27 10:05:43 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
|
|
** Documentation License version 1.3 as published by the Free Software
|
2011-05-24 09:34:08 +00:00
|
|
|
** Foundation and appearing in the file included in the packaging of
|
2012-09-19 12:28:29 +00:00
|
|
|
** this file. Please review the following information to ensure
|
|
|
|
** the GNU Free Documentation License version 1.3 requirements
|
|
|
|
** will be met: http://www.gnu.org/copyleft/fdl.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
2012-08-29 08:25:56 +00:00
|
|
|
\example widgets/calculator
|
2011-04-27 10:05:43 +00:00
|
|
|
\title Calculator Example
|
|
|
|
|
|
|
|
The example shows how to use signals and slots to implement the
|
|
|
|
functionality of a calculator widget, and how to use QGridLayout
|
|
|
|
to place child widgets in a grid.
|
|
|
|
|
|
|
|
\image calculator-example.png Screenshot of the Calculator example
|
|
|
|
|
|
|
|
The example consists of two classes:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c Calculator is the calculator widget, with all the
|
2011-04-27 10:05:43 +00:00
|
|
|
calculator functionality.
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c Button is the widget used for each of the calculator
|
2011-04-27 10:05:43 +00:00
|
|
|
button. It derives from QToolButton.
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
We will start by reviewing \c Calculator, then we will take a
|
|
|
|
look at \c Button.
|
|
|
|
|
|
|
|
\section1 Calculator Class Definition
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.h 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c Calculator class provides a simple calculator widget. It
|
|
|
|
inherits from QDialog and has several private slots associated
|
|
|
|
with the calculator's buttons. QObject::eventFilter() is
|
|
|
|
reimplemented to handle mouse events on the calculator's display.
|
|
|
|
|
|
|
|
Buttons are grouped in categories according to their behavior.
|
2012-08-01 12:36:13 +00:00
|
|
|
For example, all the digit buttons (labeled \uicontrol 0 to \uicontrol 9)
|
2011-04-27 10:05:43 +00:00
|
|
|
append a digit to the current operand. For these, we connect
|
|
|
|
multiple buttons to the same slot (e.g., \c digitClicked()). The
|
2012-08-01 12:36:13 +00:00
|
|
|
categories are digits, unary operators (\uicontrol{Sqrt}, \uicontrol{x\unicode{178}},
|
|
|
|
\uicontrol{1/x}), additive operators (\uicontrol{+}, \uicontrol{-}), and
|
|
|
|
multiplicative operators (\uicontrol{\unicode{215}}, \uicontrol{\unicode{247}}). The other buttons
|
2011-04-27 10:05:43 +00:00
|
|
|
have their own slots.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.h 1
|
|
|
|
\snippet widgets/calculator/calculator.h 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The private \c createButton() function is used as part of the
|
|
|
|
widget construction. \c abortOperation() is called whenever a
|
|
|
|
division by zero occurs or when a square root operation is
|
|
|
|
applied to a negative number. \c calculate() applies a binary
|
2012-08-01 12:36:13 +00:00
|
|
|
operator (\uicontrol{+}, \uicontrol{-}, \uicontrol{\unicode{215}}, or \uicontrol{\unicode{247}}).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.h 3
|
|
|
|
\snippet widgets/calculator/calculator.h 4
|
|
|
|
\snippet widgets/calculator/calculator.h 5
|
|
|
|
\snippet widgets/calculator/calculator.h 6
|
|
|
|
\snippet widgets/calculator/calculator.h 7
|
|
|
|
\snippet widgets/calculator/calculator.h 8
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
These variables, together with the contents of the calculator
|
|
|
|
display (a QLineEdit), encode the state of the calculator:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c sumInMemory contains the value stored in the calculator's memory
|
2012-08-01 12:36:13 +00:00
|
|
|
(using \uicontrol{MS}, \uicontrol{M+}, or \uicontrol{MC}).
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c sumSoFar stores the value accumulated so far. When the user
|
2012-08-01 12:36:13 +00:00
|
|
|
clicks \uicontrol{=}, \c sumSoFar is recomputed and shown on the
|
|
|
|
display. \uicontrol{Clear All} resets \c sumSoFar to zero.
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c factorSoFar stores a temporary value when doing
|
2011-04-27 10:05:43 +00:00
|
|
|
multiplications and divisions.
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c pendingAdditiveOperator stores the last additive operator
|
2011-04-27 10:05:43 +00:00
|
|
|
clicked by the user.
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c pendingMultiplicativeOperator stores the last multiplicative operator
|
2011-04-27 10:05:43 +00:00
|
|
|
clicked by the user.
|
2012-03-01 14:28:31 +00:00
|
|
|
\li \c waitingForOperand is \c true when the calculator is
|
2011-04-27 10:05:43 +00:00
|
|
|
expecting the user to start typing an operand.
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
Additive and multiplicative operators are treated differently
|
2012-08-01 12:36:13 +00:00
|
|
|
because they have different precedences. For example, \uicontrol{1 + 2 \unicode{247}
|
|
|
|
3} is interpreted as \uicontrol{1 + (2 \unicode{247} 3)} because \uicontrol{\unicode{247}} has higher
|
|
|
|
precedence than \uicontrol{+}.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The table below shows the evolution of the calculator state as
|
|
|
|
the user enters a mathematical expression.
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li User Input \li Display \li Sum so Far \li Add. Op. \li Factor so Far \li Mult. Op. \li Waiting for Operand?
|
|
|
|
\row \li \li 0 \li 0 \li \li \li \li \c true
|
2012-08-01 12:36:13 +00:00
|
|
|
\row \li \uicontrol{1} \li 1 \li 0 \li \li \li \li \c false
|
|
|
|
\row \li \uicontrol{1 +} \li 1 \li 1 \li \uicontrol{+} \li \li \li \c true
|
|
|
|
\row \li \uicontrol{1 + 2} \li 2 \li 1 \li \uicontrol{+} \li \li \li \c false
|
|
|
|
\row \li \uicontrol{1 + 2 \unicode{247}} \li 2 \li 1 \li \uicontrol{+} \li 2 \li \uicontrol{\unicode{247}} \li \c true
|
|
|
|
\row \li \uicontrol{1 + 2 \unicode{247} 3} \li 3 \li 1 \li \uicontrol{+} \li 2 \li \uicontrol{\unicode{247}} \li \c false
|
|
|
|
\row \li \uicontrol{1 + 2 \unicode{247} 3 -} \li 1.66667 \li 1.66667 \li \uicontrol{-} \li \li \li \c true
|
|
|
|
\row \li \uicontrol{1 + 2 \unicode{247} 3 - 4} \li 4 \li 1.66667 \li \uicontrol{-} \li \li \li \c false
|
|
|
|
\row \li \uicontrol{1 + 2 \unicode{247} 3 - 4 =} \li -2.33333 \li 0 \li \li \li \li \c true
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
2012-08-01 12:36:13 +00:00
|
|
|
Unary operators, such as \uicontrol Sqrt, require no special handling;
|
2011-04-27 10:05:43 +00:00
|
|
|
they can be applied immediately since the operand is already
|
|
|
|
known when the operator button is clicked.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.h 9
|
2011-04-27 10:05:43 +00:00
|
|
|
\codeline
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.h 10
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Finally, we declare the variables associated with the display and the
|
|
|
|
buttons used to display numerals.
|
|
|
|
|
|
|
|
\section1 Calculator Class Implementation
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
In the constructor, we initialize the calculator's state. The \c
|
|
|
|
pendingAdditiveOperator and \c pendingMultiplicativeOperator
|
|
|
|
variables don't need to be initialized explicitly, because the
|
|
|
|
QString constructor initializes them to empty strings.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 1
|
|
|
|
\snippet widgets/calculator/calculator.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
We create the QLineEdit representing the calculator's display and
|
|
|
|
set up some of its properties. In particular, we set it to be
|
|
|
|
read-only.
|
|
|
|
|
|
|
|
We also enlarge \c{display}'s font by 8 points.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For each button, we call the private \c createButton() function with
|
|
|
|
the proper text label and a slot to connect to the button.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 5
|
|
|
|
\snippet widgets/calculator/calculator.cpp 6
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The layout is handled by a single QGridLayout. The
|
|
|
|
QLayout::setSizeConstraint() call ensures that the \c Calculator
|
|
|
|
widget is always shown as its optimal size (its
|
|
|
|
\l{QWidget::sizeHint()}{size hint}), preventing the user from
|
|
|
|
resizing the calculator. The size hint is determined by the size
|
|
|
|
and \l{QWidget::sizePolicy()}{size policy} of the child widgets.
|
|
|
|
|
|
|
|
Most child widgets occupy only one cell in the grid layout. For
|
|
|
|
these, we only need to pass a row and a column to
|
|
|
|
QGridLayout::addWidget(). The \c display, \c backspaceButton, \c
|
|
|
|
clearButton, and \c clearAllButton widgets occupy more than one
|
|
|
|
column; for these we must also pass a row span and a column
|
|
|
|
span.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 7
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Pressing one of the calculator's digit buttons will emit the
|
|
|
|
button's \l{QToolButton::clicked()}{clicked()} signal, which will
|
|
|
|
trigger the \c digitClicked() slot.
|
|
|
|
|
|
|
|
First, we find out which button sent the signal using
|
|
|
|
QObject::sender(). This function returns the sender as a QObject
|
|
|
|
pointer. Since we know that the sender is a \c Button object, we
|
|
|
|
can safely cast the QObject. We could have used a C-style cast or
|
|
|
|
a C++ \c static_cast<>(), but as a defensive programming
|
|
|
|
technique we use a \l qobject_cast(). The advantage is that if
|
|
|
|
the object has the wrong type, a null pointer is returned.
|
|
|
|
Crashes due to null pointers are much easier to diagnose than
|
|
|
|
crashes due to unsafe casts. Once we have the button, we extract
|
|
|
|
the operator using QToolButton::text().
|
|
|
|
|
|
|
|
The slot needs to consider two situations in particular. If \c
|
2012-08-01 12:36:13 +00:00
|
|
|
display contains "0" and the user clicks the \uicontrol{0} button, it
|
2011-04-27 10:05:43 +00:00
|
|
|
would be silly to show "00". And if the calculator is in
|
|
|
|
a state where it is waiting for a new operand,
|
|
|
|
the new digit is the first digit of that new operand; in that case,
|
|
|
|
any result of a previous calculation must be cleared first.
|
|
|
|
|
|
|
|
At the end, we append the new digit to the value in the display.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 8
|
|
|
|
\snippet widgets/calculator/calculator.cpp 9
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c unaryOperatorClicked() slot is called whenever one of the
|
|
|
|
unary operator buttons is clicked. Again a pointer to the clicked
|
|
|
|
button is retrieved using QObject::sender(). The operator is
|
|
|
|
extracted from the button's text and stored in \c
|
|
|
|
clickedOperator. The operand is obtained from \c display.
|
|
|
|
|
2012-08-01 12:36:13 +00:00
|
|
|
Then we perform the operation. If \uicontrol Sqrt is applied to a
|
|
|
|
negative number or \uicontrol{1/x} to zero, we call \c
|
2011-04-27 10:05:43 +00:00
|
|
|
abortOperation(). If everything goes well, we display the result
|
|
|
|
of the operation in the line edit and we set \c waitingForOperand
|
|
|
|
to \c true. This ensures that if the user types a new digit, the
|
|
|
|
digit will be considered as a new operand, instead of being
|
|
|
|
appended to the current value.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 10
|
|
|
|
\snippet widgets/calculator/calculator.cpp 11
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c additiveOperatorClicked() slot is called when the user
|
2012-08-01 12:36:13 +00:00
|
|
|
clicks the \uicontrol{+} or \uicontrol{-} button.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Before we can actually do something about the clicked operator,
|
|
|
|
we must handle any pending operations. We start with the
|
|
|
|
multiplicative operators, since these have higher precedence than
|
|
|
|
additive operators:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 12
|
|
|
|
\snippet widgets/calculator/calculator.cpp 13
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-01 12:36:13 +00:00
|
|
|
If \uicontrol{\unicode{215}} or \uicontrol{\unicode{247}} has been clicked earlier, without clicking
|
|
|
|
\uicontrol{=} afterward, the current value in the display is the right
|
|
|
|
operand of the \uicontrol{\unicode{215}} or \uicontrol{\unicode{247}} operator and we can finally
|
2011-04-27 10:05:43 +00:00
|
|
|
perform the operation and update the display.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 14
|
|
|
|
\snippet widgets/calculator/calculator.cpp 15
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-01 12:36:13 +00:00
|
|
|
If \uicontrol{+} or \uicontrol{-} has been clicked earlier, \c sumSoFar is
|
2011-04-27 10:05:43 +00:00
|
|
|
the left operand and the current value in the display is the
|
|
|
|
right operand of the operator. If there is no pending additive
|
|
|
|
operator, \c sumSoFar is simply set to be the text in the
|
|
|
|
display.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 16
|
|
|
|
\snippet widgets/calculator/calculator.cpp 17
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Finally, we can take care of the operator that was just clicked.
|
|
|
|
Since we don't have the right-hand operand yet, we store the clicked
|
|
|
|
operator in the \c pendingAdditiveOperator variable. We will
|
|
|
|
apply the operation later, when we have a right operand, with \c
|
|
|
|
sumSoFar as the left operand.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 18
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c multiplicativeOperatorClicked() slot is similar to \c
|
|
|
|
additiveOperatorClicked(). We don't need to worry about pending
|
|
|
|
additive operators here, because multiplicative operators have
|
|
|
|
precedence over additive operators.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 20
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Like in \c additiveOperatorClicked(), we start by handing any
|
|
|
|
pending multiplicative and additive operators. Then we display \c
|
|
|
|
sumSoFar and reset the variable to zero. Resetting the variable
|
|
|
|
to zero is necessary to avoid counting the value twice.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 22
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c pointClicked() slot adds a decimal point to the content in
|
|
|
|
\c display.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 24
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c changeSignClicked() slot changes the sign of the value in
|
|
|
|
\c display. If the current value is positive, we prepend a minus
|
|
|
|
sign; if the current value is negative, we remove the first
|
|
|
|
character from the value (the minus sign).
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 26
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c backspaceClicked() removes the rightmost character in the
|
|
|
|
display. If we get an empty string, we show "0" and set \c
|
|
|
|
waitingForOperand to \c true.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 28
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c clear() slot resets the current operand to zero. It is
|
2012-08-01 12:36:13 +00:00
|
|
|
equivalent to clicking \uicontrol Backspace enough times to erase the
|
2011-04-27 10:05:43 +00:00
|
|
|
entire operand.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 30
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c clearAll() slot resets the calculator to its initial state.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 32
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c clearMemory() slot erases the sum kept in memory, \c
|
|
|
|
readMemory() displays the sum as an operand, \c setMemory()
|
|
|
|
replace the sum in memory with the current sum, and \c
|
|
|
|
addToMemory() adds the current value to the value in memory. For
|
|
|
|
\c setMemory() and \c addToMemory(), we start by calling \c
|
|
|
|
equalClicked() to update \c sumSoFar and the value in the
|
|
|
|
display.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 34
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The private \c createButton() function is called from the
|
|
|
|
constructor to create calculator buttons.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 36
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The private \c abortOperation() function is called whenever a
|
|
|
|
calculation fails. It resets the calculator state and displays
|
|
|
|
"####".
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/calculator.cpp 38
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The private \c calculate() function performs a binary operation.
|
|
|
|
The right operand is given by \c rightOperand. For additive
|
|
|
|
operators, the left operand is \c sumSoFar; for multiplicative
|
|
|
|
operators, the left operand is \c factorSoFar. The function
|
|
|
|
return \c false if a division by zero occurs.
|
|
|
|
|
|
|
|
\section1 Button Class Definition
|
|
|
|
|
|
|
|
Let's now take a look at the \c Button class:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/button.h 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \c Button class has a convenience constructor that takes a
|
|
|
|
text label and a parent widget, and it reimplements QWidget::sizeHint()
|
|
|
|
to provide more space around the text than the amount QToolButton
|
|
|
|
normally provides.
|
|
|
|
|
|
|
|
\section1 Button Class Implementation
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/button.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The buttons' appearance is determined by the layout of the
|
|
|
|
calculator widget through the size and
|
|
|
|
\l{QWidget::sizePolicy}{size policy} of the layout's child
|
|
|
|
widgets. The call to the
|
|
|
|
\l{QWidget::setSizePolicy()}{setSizePolicy()} function in the
|
|
|
|
constructor ensures that the button will expand horizontally to
|
|
|
|
fill all the available space; by default, \l{QToolButton}s don't
|
|
|
|
expand to fill available space. Without this call, the different
|
|
|
|
buttons in a same column would have different widths.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/calculator/button.cpp 1
|
|
|
|
\snippet widgets/calculator/button.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
In \l{QWidget::sizeHint()}{sizeHint()}, we try to return a size
|
|
|
|
that looks good for most buttons. We reuse the size hint of the
|
|
|
|
base class (QToolButton) but modify it in the following ways:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li We add 20 to the \l{QSize::height()}{height} component of the size hint.
|
|
|
|
\li We make the \l{QSize::width()}{width} component of the size
|
2011-04-27 10:05:43 +00:00
|
|
|
hint at least as much as the \l{QSize::width()}{height}.
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
This ensures that with most fonts, the digit and operator buttons
|
|
|
|
will be square, without truncating the text on the
|
2012-08-01 12:36:13 +00:00
|
|
|
\uicontrol{Backspace}, \uicontrol{Clear}, and \uicontrol{Clear All} buttons.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The screenshot below shows how the \c Calculator widget would
|
|
|
|
look like if we \e didn't set the horizontal size policy to
|
|
|
|
QSizePolicy::Expanding in the constructor and if we didn't
|
|
|
|
reimplement QWidget::sizeHint().
|
|
|
|
|
|
|
|
\image calculator-ugly.png The Calculator example with default size policies and size hints
|
|
|
|
|
|
|
|
*/
|