⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qpushbutton.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qapplication.h"#include "qbitmap.h"#include "qdesktopwidget.h"#include "qdialog.h"#include <private/qdialog_p.h>#include "qdrawutil.h"#include "qevent.h"#include "qfontmetrics.h"#include "qmenu.h"#include "qstylepainter.h"#include "qpixmap.h"#include "qpointer.h"#include "qpushbutton.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtoolbar.h"#include "qdebug.h"#include "qlayoutitem.h"#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endif#include "private/qabstractbutton_p.h"class QPushButtonPrivate : public QAbstractButtonPrivate{    Q_DECLARE_PUBLIC(QPushButton)public:    enum AutoDefaultValue { Off = 0, On = 1, Auto = 2 };    QPushButtonPrivate()        : QAbstractButtonPrivate(QSizePolicy::PushButton), autoDefault(Auto),          defaultButton(false), flat(false), menuOpen(false) {}    inline void init() { resetLayoutItemMargins(); }    void resetLayoutItemMargins();    void _q_popupPressed();    QDialog *dialogParent() const;    QPointer<QMenu> menu;    uint autoDefault : 2;    uint defaultButton : 1;    uint flat : 1;    uint menuOpen : 1;};/*!    \class QPushButton qpushbutton.h    \brief The QPushButton widget provides a command button.    \ingroup basicwidgets    \mainclass    The push button, or command button, is perhaps the most commonly    used widget in any graphical user interface. Push (click) a button    to command the computer to perform some action, or to answer a    question. Typical buttons are OK, Apply, Cancel, Close, Yes, No    and Help.    A command button is rectangular and typically displays a text    label describing its action. A shortcut key can be specified by    preceding the preferred character with an ampersand in the    text. For example:    \code        QPushButton *button = new QPushButton("&Download", this);    \endcode    In this example the shortcut is \e{Alt+D}. See the \l    {QShortcut#mnemonic}{QShortcut} documentation for details (to    display an actual ampersand, use '&&').    Push buttons display a textual label, and optionally a small    icon. These can be set using the constructors and changed later    using setText() and setIcon().  If the button is disabled the    appearance of the text and icon will be manipulated with respect    to the GUI style to make the button look "disabled".    A push button emits the signal clicked() when it is activated by    the mouse, the Spacebar or by a keyboard shortcut. Connect to    this signal to perform the button's action. Push buttons also    provide less commonly used signals, for example, pressed() and    released().    Command buttons in dialogs are by default auto-default buttons,    i.e. they become the default push button automatically when they    receive the keyboard input focus. A default button is a push    button that is activated when the user presses the Enter or Return    key in a dialog. You can change this with setAutoDefault(). Note    that auto-default buttons reserve a little extra space which is    necessary to draw a default-button indicator. If you do not want    this space around your buttons, call setAutoDefault(false).    Being so central, the button widget has grown to accommodate a    great many variations in the past decade. The Microsoft style    guide now shows about ten different states of Windows push buttons    and the text implies that there are dozens more when all the    combinations of features are taken into consideration.    The most important modes or states are:    \list    \i Available or not (grayed out, disabled).    \i Standard push button, toggling push button or menu button.    \i On or off (only for toggling push buttons).    \i Default or normal. The default button in a dialog can generally       be "clicked" using the Enter or Return key.    \i Auto-repeat or not.    \i Pressed down or not.    \endlist    As a general rule, use a push button when the application or    dialog window performs an action when the user clicks on it (such    as Apply, Cancel, Close and Help) \e and when the widget is    supposed to have a wide, rectangular shape with a text label.    Small, typically square buttons that change the state of the    window rather than performing an action (such as the buttons in    the top-right corner of the QFileDialog) are not command buttons,    but tool buttons. Qt provides a special class (QToolButton) for    these buttons.    If you need toggle behavior (see setCheckable()) or a button    that auto-repeats the activation signal when being pushed down    like the arrows in a scroll bar (see setAutoRepeat()), a command    button is probably not what you want. When in doubt, use a tool    button.    A variation of a command button is a menu button. These provide    not just one command, but several, since when they are clicked    they pop up a menu of options. Use the method setMenu() to    associate a popup menu with a push button.    Other classes of buttons are option buttons (see QRadioButton) and    check boxes (see QCheckBox).    \table 100%    \row \o \inlineimage macintosh-pushbutton.png Screenshot of a Macintosh style push button         \o A push button shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.         Note that when a button's width becomes smaller than 50 or         its height becomes smaller than 30, the button's corners are         changed from round to square. Use the setMinimumSize()         function to prevent this behavior.    \row \o \inlineimage windowsxp-pushbutton.png Screenshot of a Windows XP style push button         \o A push button shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.    \row \o \inlineimage plastique-pushbutton.png Screenshot of a Plastique style push button         \o A push button shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.    \endtable    In Qt, the QAbstractButton base class provides most of the modes    and other API, and QPushButton provides GUI logic.    See QAbstractButton for more information about the API.    \sa QToolButton, QRadioButton, QCheckBox, {fowler}{GUI Design Handbook: Push Button}*//*!    \property QPushButton::autoDefault    \brief whether the push button is an auto default button    If this property is set to true then the push button is an auto    default button.    In some GUI styles a default button is drawn with an extra frame    around it, up to 3 pixels or more. Qt automatically keeps this    space free around auto-default buttons, i.e. auto-default buttons    may have a slightly larger size hint.    This property's default is true for buttons that have a QDialog    parent; otherwise it defaults to false.    See the \l default property for details of how \l default and    auto-default interact.*//*!    \property QPushButton::default    \brief whether the push button is the default button    Default and autodefault buttons decide what happens when the user    presses enter in a dialog.    A button with this property set to true (i.e., the dialog's    \e default button,) will automatically be pressed when the user presses enter,    with one exception: if an \a autoDefault button currently has focus, the autoDefault    button is pressed. When the dialog has \l autoDefault buttons but no default button,    pressing enter will press either the \l autoDefault button that currently has focus, or if no    button has focus, the next \l autoDefault button in the focus chain.    In a dialog, only one push button at a time can be the default    button. This button is then displayed with an additional frame    (depending on the GUI style).    The default button behavior is provided only in dialogs. Buttons    can always be clicked from the keyboard by pressing Spacebar when    the button has focus.    If the default property is set to false on the current default button    while the dialog is visible, a new default will automatically be    assigned the next time a pushbutton in the dialog receives focus.    This property's default is false.*//*!    \property QPushButton::flat    \brief whether the button border is raised    This property's default is false. If this property is set, most    styles will not paint the button background unless the button is    being pressed. setAutoFillBackground() can be used to ensure that    the background is filled using the QPalette::Button brush.*//*!    Constructs a push button with no text and a \a parent.*/QPushButton::QPushButton(QWidget *parent)    : QAbstractButton(*new QPushButtonPrivate, parent){    Q_D(QPushButton);    d->init();}/*!    Constructs a push button with the parent \a parent and the text \a    text.*/QPushButton::QPushButton(const QString &text, QWidget *parent)    : QAbstractButton(*new QPushButtonPrivate, parent){    Q_D(QPushButton);    setText(text);    d->init();}/*!    Constructs a push button with an \a icon and a \a text, and a \a parent.    Note that you can also pass a QPixmap object as an icon (thanks to    the implicit type conversion provided by C++).*/QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent)    : QAbstractButton(*new QPushButtonPrivate, parent){    Q_D(QPushButton);    setText(text);    setIcon(icon);    d->init();}/*!    Destroys the push button.*/QPushButton::~QPushButton(){}QDialog *QPushButtonPrivate::dialogParent() const{    Q_Q(const QPushButton);    const QWidget *p = q;    while (p && !p->isWindow()) {        p = p->parentWidget();        if (const QDialog *dialog = qobject_cast<const QDialog *>(p))            return const_cast<QDialog *>(dialog);    }    return 0;}/*!    Initialize \a option with the values from this QPushButton. This method is useful    for subclasses when they need a QStyleOptionButton, but don't want to fill    in all the information themselves.    \sa QStyleOption::initFrom()*/void QPushButton::initStyleOption(QStyleOptionButton *option) const{    if (!option)        return;    Q_D(const QPushButton);    option->initFrom(this);    option->features = QStyleOptionButton::None;    if (d->flat)        option->features |= QStyleOptionButton::Flat;#ifndef QT_NO_MENU    if (d->menu)        option->features |= QStyleOptionButton::HasMenu;#endif    if (autoDefault() || d->defaultButton)        option->features |= QStyleOptionButton::AutoDefaultButton;    if (d->defaultButton)        option->features |= QStyleOptionButton::DefaultButton;    if (d->down || d->menuOpen)        option->state |= QStyle::State_Sunken;    if (d->checked)        option->state |= QStyle::State_On;    if (!d->flat && !d->down)        option->state |= QStyle::State_Raised;    option->text = d->text;    option->icon = d->icon;    option->iconSize = iconSize();}void QPushButton::setAutoDefault(bool enable){    Q_D(QPushButton);    uint state = enable ? QPushButtonPrivate::On : QPushButtonPrivate::Off;    if (d->autoDefault != QPushButtonPrivate::Auto && d->autoDefault == state)        return;    d->autoDefault = state;    d->sizeHint = QSize();    update();    updateGeometry();}bool QPushButton::autoDefault() const{    Q_D(const QPushButton);    if(d->autoDefault == QPushButtonPrivate::Auto)        return ( d->dialogParent() != 0 );    return d->autoDefault;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -