📄 qpushbutton.cpp
字号:
/****************************************************************************** $Id: qt/src/widgets/qpushbutton.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QPushButton class**** Created : 940221**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the widgets module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qpushbutton.h"#ifndef QT_NO_PUSHBUTTON#include "qdialog.h"#include "qfontmetrics.h"#include "qpainter.h"#include "qdrawutil.h"#include "qpixmap.h"#include "qbitmap.h"#include "qpopupmenu.h"#include "qguardedptr.h"#include "qptrdict.h" // binary compatibility#include "qapplication.h"#include "qtoolbar.h"// NOT REVISED/*! \class QPushButton qpushbutton.h \brief The QPushButton widget provides a command button. \ingroup basic The push button, also referred to as command button, is perhaps the most central widget in any graphical user interface: Push it to command the computer to perform some action. Typical actions are Ok, Apply, Cancel, Close or Help. A command button is rectangular (ca. 80x22 pixel) and typically displays a text label describing its action. An underscored character in the label, marked with an ampersand in the text, signals an accelerator key. This code creates a push button labelled "Rock & Roll". Due to the first ampersand, the c displays underscored and the button gets the automatic accelerator key, Alt-C: \code QPushButton *p = new QPushButton( "Ro&ck && Roll", this ); \endcode The text can be changed anytime later with setText(). You can also define a pixmap with setPixmap(). The text/pixmap is manipulated as necessary to create "disabled" appearance according to the respective GUI style when the button is disabled. A command button can, in addition to the text or pixmap label, also display a little icon. Use the extended constructor or setIconSet() to define this icon. A push button emits the signal clicked() when it is activated, either with the mouse, the spacebar or a keyboard accelerator. Connect to this signal to perform the button's action. Other signals of less importance are pressed() when the button is pressed down and released() when it is released, respectively. 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 command button that is activated when the users hits the Enter or Return key in a dialog. Adjust this behaviour with setAutoDefault(). Note that auto-default buttons reserve a little extra space necessary to draw a default-button indicator. If you do not want this space around your buttons, call setAutoDefault( FALSE ). Being so central, the widget has grown to accomodate a great many variations in the past decade, and by now the Microsoft style guide 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, sorted roughly by importance: <ul> <li> Available or not ("grayed out", disabled). <li> Standard push button, toggling push button or menu button. <li> On or off (only for toggling push buttons). <li> Default or normal. The default button in a dialog can generally be "clicked" using the Enter or Return key. <li> Auto-repeat or not. <li> Pressed down or not. </ul> As a general rule, use a push button when the application or dialog window performs an action when the user clicks on it (like Apply, Cancel, Close, 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 (like for example 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. Also, if you need toggle behaviour (see setToggleButton()) or a button that auto-repeats the activation signal when being pushed down like the arrows in a scrollbar (see setAutoRepeat()), a command button is probably not what you want. In case of doubt, go with a tool button. A variation of a command button is a menu button. It provides not just one command, but several. Use the method setPopup() to associate a popup menu with a push button. Other classes of buttons are option buttons (see QRadioButton) and check boxes (see QCheckBox). <img src="qpushbt-m.png"> <img src="qpushbt-w.png"> In Qt, the QButton class provides most of the modes and other API, and QPushButton provides GUI logic. See QButton for more information about the API. \important text, setText, text, pixmap, setPixmap, accel, setAccel, isToggleButton, setDown, isDown, isOn, state, autoRepeat, isExclusiveToggle, group, setAutoRepeat, toggle, pressed, released, clicked, toggled, state stateChanged \sa QToolButton, QRadioButton QCheckBox <a href="guibooks.html#fowler">GUI Design Handbook: Push Button</a>*/class QPushButtonPrivate{public: QPushButtonPrivate() :iconset( 0 ) {} ~QPushButtonPrivate() { delete iconset; } QGuardedPtr<QPopupMenu> popup; QIconSet* iconset;};static QPtrDict<QPushButtonPrivate> *d_ptr = 0;static void cleanup_d_ptr(){ delete d_ptr; d_ptr = 0;}static QPushButtonPrivate* d( const QPushButton* foo ){ if ( !d_ptr ) { d_ptr = new QPtrDict<QPushButtonPrivate>; d_ptr->setAutoDelete( TRUE ); qAddPostRoutine( cleanup_d_ptr ); } QPushButtonPrivate* ret = d_ptr->find( (void*)foo ); if ( ! ret ) { ret = new QPushButtonPrivate; d_ptr->replace( (void*) foo, ret ); } return ret;}static bool has_d( const QPushButton* foo ){ return d_ptr && d_ptr->find( (void*)foo );}static void delete_d( const QPushButton* foo ){ if ( d_ptr ) d_ptr->remove( (void*) foo );}/*! Constructs a push button with no text. The \a parent and \a name arguments are sent to the QWidget constructor.*/QPushButton::QPushButton( QWidget *parent, const char *name ) : QButton( parent, name ){ init();}/*! Constructs a push button with a text. The \a parent and \a name arguments are sent to the QWidget constructor.*/QPushButton::QPushButton( const QString &text, QWidget *parent, const char *name ) : QButton( parent, name ){ init(); setText( text );}/*! Constructs a push button with an \a icon and a \a text. Note that you can also pass a QPixmap object as icon (thanks to C++' implicit type conversion). The \a parent and \a name arguments are sent to the QWidget constructor.*/QPushButton::QPushButton( const QIconSet& icon, const QString &text, QWidget *parent, const char *name ) : QButton( parent, name ){ init(); setText( text ); setIconSet( icon );}/*! Destructs the push button */QPushButton::~QPushButton(){ delete_d( this );}void QPushButton::init(){ defButton = FALSE; lastEnabled = FALSE; hasMenuArrow = FALSE; flt = FALSE;#ifndef QT_NO_DIALOG autoDefButton = topLevelWidget()->inherits("QDialog");#else autoDefButton = FALSE;#endif setBackgroundMode( PaletteButton ); setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );}/*! Makes the push button a toggle button if \a enable is TRUE, or a normal push button if \a enable is FALSE. Toggle buttons have an on/off state similar to \link QCheckBox check boxes. \endlink A push button is initially not a toggle button. \sa setOn(), toggle(), isToggleButton() toggled()*/void QPushButton::setToggleButton( bool enable ){ QButton::setToggleButton( enable );}/*! Switches a toggle button on if \a enable is TRUE or off if \a enable is FALSE. \sa isOn(), toggle(), toggled(), isToggleButton()*/void QPushButton::setOn( bool enable ){ if ( !isToggleButton() ) return; QButton::setOn( enable );}/*! Toggles the state of a toggle button. \sa isOn(), setOn(), toggled(), isToggleButton()*/void QPushButton::toggle(){ if ( !isToggleButton() ) return; QButton::setOn( !isOn() );}/*! \fn bool QPushButton::autoDefault() const Returns TRUE if the button is an auto-default button. \sa setAutoDefault()*//*! Sets the push buttons to an auto-default button if \a enable is TRUE, or to a normal button if \a enable is FALSE. An auto-default button becomes automatically the default push button in a dialog when it receives the keyboard input focus. 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. \sa autoDefault(), setDefault()*/void QPushButton::setAutoDefault( bool enable ){ if ( (bool)autoDefButton == enable ) return; autoDefButton = enable; update(); updateGeometry();}/*! \fn bool QPushButton::isDefault() const Returns TRUE if the button is currently default. \sa setDefault()*//*! Sets this button to be the current default button of a \link QDialog dialog\endlink if \a enable is TRUE, or to be a normal button if \a enable is FALSE. The current default button gets clicked when the user presses the "Enter" key, independently of which widget in the dialog currently has the keyboard input focus. Only one push button can at any time be the default button. This button is then displayed with an additional frame ( depending on the GUI style ). The default button behaviour is only provided in dialogs. Buttons can always be clicked from the keyboard by pressing return or the spacebar when the button has focus. \sa isDefault(), setAutoDefault(), QDialog*/void QPushButton::setDefault( bool enable ){ if ( (bool)defButton == enable ) return; // no change defButton = enable;#ifndef QT_NO_DIALOG if ( defButton && topLevelWidget()->inherits( "QDialog" ) ) ((QDialog*)topLevelWidget())->setDefault( this );#endif update();}/*!\reimp*/QSize QPushButton::sizeHint() const{ constPolish(); int w = 0; int h = 0; if ( pixmap() ) { QPixmap *pm = (QPixmap *)pixmap(); w = pm->width() + style().buttonMargin();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -