QT Beginner's Common QT Control Cognition button series

Source: Internet
Author: User
Tags abstract bool mutex repetition
Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://hongbin0720.blog.51cto.com/3305400/1231118

buttons, often interpreted as triggering an action request or command, are actions that interact with the user for instruction. The QT button series is divided into different definitions according to usage, and its implementation framework provides the versatility of the button for Qabstractbutton, which is an abstract base class, so it cannot be instantiated. The user needs to inherit the definition of this class, implement paintevent, and implement concrete presentation. The common buttons are Qpushbutton,qtoolbutton,qradiobutton and Qcheckbox. This is based on the user's marking (toggle) and non-toggle. This model uses the typical C + + an abstract inheritance pattern, the button general abstraction to the Qabstractbutton class, the rest of the class inherits, and according to their own characteristics to achieve the function of drawing. So we start with the basics.

1, Qabstractbutton: This is the abstract accumulation of buttons, providing the function of the general button. This class cannot be instantiated, so subclasses handle interactive actions, especially how to draw a button. It provides support for pressing the button (push button) and the check (checkable) or toggle (toggle) Flag button. The implementation of the tag button has a Qradiobutton radio button and a qcheckbox multi box. Can be achieved by I have Qpushbutton and Qtoolbutton. They can all be tagged for switching as long as needed.

Buttons can display a label that can contain text text and icon icons, use SetText to set the text, and SetIcon set the icon. The ampersand (' & ') symbol that is included can add a shortcut key Shoarcut key if the button is a text.

The four implementation buttons can display both text and icons.

1.1QAbstractButton can provide the state of most button

1) Isdown () indicates whether the button is pressed down.

2) isChecked () indicates whether the button is checked or marked for switching. Only buttons that can be checked and toggled (labeled) can mark or unmark checked or unchecked

3) isenabled () indicates whether the button can be clicked by the user

4) Setautorepeat () sets whether the button can be auto-repeat (automatically repeated) when the user presses the button, and the properties Autorepeatdelay and Autorepeatinterval define how to repeat the response execution

5) Setcheckable () sets whether the button can be toggled or marked


1.2 Qabstractbutton provides the following signal

1) pressed (), when the mouse on the button and left click on the launch.

2) Released (), when the left mouse button is released

3) clicked (bool Checked=false), when the mouse first presses pressed, then releases, or the accelerator is triggered, or click () or Animateclick () call

4) toggled () transmit when switchable (labeled) button state changes


1.3 Qabstractbutton Abstract Button Properties

1) Autoexclusive:bool flag automatic mutex activation enabled. If it is active, then the labeled chackabled button belonging to the same parent window behaves as if they belong to a mutex group, and only one button state is checked marked.

Note that this property is for buttons that belong to button group buttons. The default is False, for the radio button is true

BOOL Autoexclusive () and setautoexclusive (bool) for access and setup

2) If the AUTOREPEAT:BOOL flag is repeatedly activated, it is off by default. If it is activated, the pressed ()/released ()/clicked () signal will be emitted in the limited interval.

Note: If the button is pressed by the shortcut key shortcut key, this property is activated and the time is defined by the system and not by this class, the signal is normally emitted.

AutoRepeat () and setautorepeat (BOOL) Set whether Auto Repeat button

3) Autorepeatdelay:int indicates the initial delay time of the automatic oh repetition.

Use Autorepeatdelay () and setautorepeatdelay (int) To set the time, unit MS level

4) Autorepeatinterval:int indicates the interval of automatic repetition, setautorepeatinterval (int) is set

5) Chekcable:bool Indicates whether the button can checkable the tag switch,

Ischeckable () Determines whether it can be tagged, setcheckable (bool) setting

6) Checked:bool Indicates whether the token, isChecked () gets is marked, setchecked (BOOL) sets the flag State

Note that this property must be used at the checkabled button to trigger the toggled (bool checked) signal

7) Down:bool Indicates whether the button is pressed. Isdown and Setdown (BOOL) are set.

8) Icon:qicon Indicates whether the icon is displayed on button buttons. The size of the icon is defined according to the GUI type, but can be set by the Iconsize property.

Qicon icon (): void SetIcon (const Qicon &icon)

9) The Text:qstring property saves the problem displayed on the button.

Gets the value through text () and returns an empty string if it is empty. If the character contains ampersand ' & ' symbols, shortcut is created automatically. If the & character is displayed, use ' && ' on it.

This value is set by SetText (const QString &) without a default string.

1.4 The common functions provided by the Qabstractbutton abstract class:

1) void Qabstractbutton::animateclick (int msec = +) [slot], the button is immediately pressed pressed, and is released after msec time. If this function is not released and continues to be called, the release time is reset.

All signals with the click will be fired. If the button is disabled, this function does not work.

2) void Qabstractbutton::click () [slot] performs a one-click operation. All signals associated with a tap are emitted, and if the button is checkable, the status of the buttons toggles toggled. If the button is disabled, it does not work

3) The status of the Void Qabstractbutton::toggle () [Slot] toggle button triggers the toggled (bool checked) signal


2, Qpushbutton, is the realization of the Qabstractbutton body. is a command button, click on it to perform some actions or respond to some problems, common scenes such as Ok,apply,cancel Yse no help, etc.

command buttons often use text to denote his movements.

The most important state of the button is:

is available (disabled or enabled)

Standard push button or ToggleButton or menu button

On and off (on or off), only for switchable buttons

Default or Normal, the defaults button is usually clicked, in a dialog box using the OK or return key

Auto-repeat Automatic Repetition

Pressed down is pressed

A variant of the other command button is a menu button. Not only does it provide a command, it pops up a menu option when clicked. Use the SetMenu method to associate this button with a pop-up menu option

In a dialog box, the buttons can be set to the default and Auto-default buttons. For example, when the dialog box receives the focus, it automatically gets the focus and can use the Setautodefault () setting.

It is important to note that the Auto-default button takes up a little more space and is wider than a normal button, and is used to indicator the default button of the session, and if it is not required, set Setautodefault (false).

2.1 Adding attributes:

1) Autodefault:bool

This property maintains whether the command button automatically defaults to the Auto default button

2) Default: Flag This command button is a default button.

This property is set by bool IsDefault () GET, SetDefault (BOOL)


The default and Autodefault properties determine what happens when a user presses enter in a dialog box

If a property is set to default, this button automatically launches the pressed signal, unless the Autodefault button currently acquires the focus, the Autodefault button pressed is fired.

If the dialog box dialog only the Autodefault button does not have a default button, the Autodefault button that is currently getting focus emits pressed, or a button that does not have the focus, the Next button that is connected to the focus is emitted.

A dialog box with only one button is the default button, which requires additional space representation.

The default button defaults behavior is used only in dialog boxes. The button that gets the focus can always be clicked through the spacebar of the keyboard

3) Flat:bool Indicates whether the border border of the button is displayed

BOOL Isflat () gets, Setflat (BOOL) is set.

The default value is False, and if set to true, the background of this button will not be painted unless the button is clicked.

Setautofillbackground () can be used to ensure that the background can be painted using the Qpalette::button format brush.

2.2 Common functions:

Qpushbutton::qpushbutton (qwidget * parent = 0)

Qpushbutton::qpushbutton (const QString & Text, Qwidget * parent = 0)

Qpushbutton::qpushbutton (const QICON & icon, const QString & text, Qwidget *parent = 0)

Create a command button text for description, Qicon for displaying small icons

Qmenu * Qpushbutton::menu () const

Gets the pop-up menu associated with the button, if no context menu returns 0


void Qpushbutton::setmenu (Qmenu * menu)

Associate a menu to the button so that the button forms a menu button


void Qpushbutton::showmenu () [slot]

Displays the buttons that are associated with the button, and nothing is done if none.

This function knows that the popup menu is closed before it is returned.


3. Qradiobutton: Radio button

The radio button provides a button and a label,label to display the corresponding text information.

The radio button is a switch button that can be switched on or off i.e. checked or unchecked. The main purpose is to provide the user with the ' one of many ' selection. If the user chooses a different radio button, the previous one is reversed. The default is autoexclusive, if it is active, the radio button belonging to the same parent class can only select one, if more than one

Exclusive mutex options are used in the same parent window, they are known to be inside Qbuttongroup.

The toggle signal is emitted regardless of the switching switch.

Like Qpushbutton, a string of text displays, text Plus & automatically creates shortcut keys

Sets the text or icon using an abstract class interface SetText and SetIcon implementations.


4. Qcheckbox: check box, provide a checkbox and a text label

With Qradiobutton, is a switch button that toggles the status on or off, i.e. checked or unchecked.

Generally, this check box can enable or disable but does not affect other check buttons. However, if this is placed inside a mutually exclusive qbuttongroup, only one check box can be selected. This is a property that is qualified by the Qbuttongroup with the mutex option.

When the checkbox check box is checked marked or clear, the statechangeed signal will be triggered. You can use ischecked () to query whether the check box button is marked.

In addition to the frequently selected and unchecked states, the Qcheckbox provides a third state of "unchanged" status. This is useful when you give the user a check box that is neither selected nor reversed. If you use this state, set its properties settristate (), and then use CheckState () to query the status of the current tag toggle.

4.1 Tristate:bool Indicates whether this check box is a tri-state three-state check box

BOOL Istristate () settristate (bool) View and set

4.2 Common functions:

Qt::checkstate qcheckbox::checkstate () const

Check the status of the check box, if you do not need tristatus, you can directly use ischecked, which returns a Boolean value


void Qcheckbox::setcheckstate (Qt::checkstatestate)

Sets the status of the current check box. If you do not need to support tristate, you can use setchecked (bool) directly

statechanged (int state) signal: When the check box status changes.


5, Qtoolbutton: As the name implies, is the tool operation related buttons. It is a quick access button command or option. So it is usually used in conjunction with Qtoolbar. Tool buttons typically do not display text, and the icon Qicon is displayed. It is primarily paired with Qtoolbar on the qaction behavior created and used.

The general Qtoolbutton is created when qtoolbar::addaction, or when an action already exists is added to Qtoolbar. Of course, this key can be initialized and used as normal cases.

Qtoolbutton supports auto-force display mode. You can use the Setautoraise () to set the button float.

Qtoolbutton A classic usage is the ability to select tools, and this button has a switch, such as drawing in the use of brushes, choose this button can draw things.

The tool button icon is set to Qicon, which allows you to differentiate between different resolutions in disabled and active active states. If the function of the button is not available, the non-enabled disabled pixels are used, and the active pixmap pixels are displayed when the button floats because the mouse is on top.

Tool button appearance and features can be modified by setting the button style Settoolbuttonstyle and setting seticonsize.

Qtoolbutton Another option is the pop-up menu, which can use SetMenu and Setpopupmode () to configure different available buttons to carry a menu collection. Pop-up lists can use Setpopupdelay.

This article is from the "ZHOUHB" blog, make sure to keep this source http://hongbin0720.blog.51cto.com/3305400/1231118

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.