Qt——Common properties, methods and signals of QPushButton control

Common properties, methods and signals of QPushButton control in Qt

1. Common properties of QPushButton control
2. Common methods of QPushButton control
3. Common signals of QPushButton control

1. Common properties of QPushButton control (Properties)

1. text:

Description: The text displayed on the button.

usage:

button->setText("Click me");

2. setStyleSheet

Description: Style sheet used to set the control
Usage:

//#00(r红)00(g绿色)00(b蓝色)
button->setStyleSheet("border: 1px solid red;"
                   "border-top: 0;"
                   "border-left: 0;"
                   "border-right: 0;"
                   "background-color: #00aa00"
       );
button2->setStyleSheet(QString("background-color: %1;"
                               "border-radius:%2px;")
                       .arg("green").arg(10));

3. icon:

Description: The icon displayed on the button.

usage:

button->setIcon(QIcon(":/images/icon.png"));

4. resize:

Description: Reset button size.

usage:

button->resize(80, 50); //(width,heigth)

5. move:

Description: Move button position.

usage:

//width():获取控件的宽度 height(): 获取控件高度
button->move(QPoint((width() - button->width()) / 2, (height() - >button->height()) / 2));
//或直接输入x轴和y轴
button->move(30, 50);

6. checkable:

Description: Indicates whether the button has a toggle state (pressed and released).

usage:

button->setCheckable(true);

7. checked:

Description: Set or get the selected state of the button, only valid when checkableis .true

usage:

button->setChecked(true);
// 或
bool isChecked = button->isChecked();

8. enabled:

Description: Set or get the enabled status of the button (available or disabled).

usage:

button->setEnabled(false);
// 或
bool isEnabled = button->isEnabled();

9. visible:

Description: Sets or gets the visibility state of the button (visible or hidden).

usage:

button->setVisible(false);
// 或
bool isVisible = button->isVisible();

10. shortcut:

Description: Set the shortcut key for the button, used to trigger button clicks in the application.

usage:

button->setShortcut(QKeySequence("Ctrl+S"));

11. toolTip:

Description: Sets the tooltip text of the button, displayed when the mouse is hovered.

usage:

button->setToolTip("Click this button to save.");

12. statusTip:

Description: Set the status prompt text of the button, usually displayed in the status bar.

usage:

button->setStatusTip("Save the current changes.");

13. whatsThis:

Description: Sets the "What is this" help text for the button, typically used for context-sensitive help.

usage:

button->setWhatsThis("This button is used to save the changes you've made.");

14. autoDefault:

Description: If set to true, the button can be triggered by the Enter key without having focus.

usage:

button->setAutoDefault(true);

15. default:

Description: If set to true, the button will be designated as the default button, which will usually display a default appearance (for example, bold text).

usage:

button->setDefault(true);

16. flat:

Description: If set to true, the button will have no background and only display text or icon.

usage:

button->setFlat(true);

17. menu:

Description: Sets the popup menu associated with the button.

usage:

button->setMenu(menu);

2. Common methods of QPushButton control (Methods)

1. click():

Description: Simulate the click action of the button and trigger the corresponding slot function of the button.

usage:

button->click();

2. setText(const QString &text):

Description: Sets the text displayed on the button.

usage:

button->setText("New Text");

3. setIcon(const QIcon &icon):

Description: Sets the icon displayed on the button.

usage:

button->setIcon(QIcon(":/images/new_icon.png"));

4. setChecked(bool checked):

Description: Set the selected state of the button, which is only valid when checkableis true.

usage:

button->setChecked(true);

5. setEnabled(bool enabled):

Description: Set the enabled status of the button ( trueenabled, falsedisabled).

usage:

button->setEnabled(false);

6. setVisible(bool visible):

Description: Set the visibility state of the button ( truevisible, falseinvisible).

usage:

button->setVisible(true);

7. setShortcut(const QKeySequence &key):

Description: Set the shortcut key for the button.

usage:

button->setShortcut(QKeySequence("Ctrl+N"));
button->setShortcut(QKeySequence("Shift+Ctrl+S"));
button->setShortcut(QKeySequence("Alt+X"));
//Ctrl+Shift+P
button->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P));

8. setToolTip(const QString &tip):

Description: Sets the tooltip text for the button.

usage:

button->setToolTip("Click this button to create a new item.");

9. setStatusTip(const QString &tip):

Description: Set the status prompt text of the button.

usage:

button->setStatusTip("Create a new item.");

11. setWhatsThis(const QString &text):

Description: Sets the "What is this" help text for the button.

usage:

button->setWhatsThis("This button is used to create a new item.");

12. setAutoDefault(bool enabled):

Description: Set whether the button responds to the Enter key ( trueresponse, falsenot response).

usage:

button->setAutoDefault(true);

13. setDefault(bool enabled):

Description: Set whether the button is the default button.

usage:

button->setDefault(true);

14. setFlat(bool enable):

Description: Set whether the button is flat style (no background).

usage:

button->setFlat(true);

15. setMenu(QMenu *menu):

Description: Sets the popup menu associated with the button.

usage:

button->setMenu(menu);

3. Common signals of QPushButton control

These signals represent different button interactions and state changes, and you can connect them with the corresponding slot functions to perform the desired action when a specific event occurs.
1. clicked():

Description: Emitted when the button is clicked.
Usage: Connect this signal to perform an action when the button is clicked.

2. customContextMenuRequested(const QPoint &pos):

Description: Emitted when the user requests a custom context menu.
Usage: Connect this signal to perform an action when the user requests a context menu.

3. destroyed(QObject *obj = nullptr):

Description: Emitted when the object is destroyed.
Usage: This signal is typically not connected directly but is used for object management and cleanup.

4. objectNameChanged(const QString &objectName):

Description: Emitted when the object name changes.
Usage: Connect this signal to perform an action when the object name changes.

5. pressed():

Description: Emitted when the button is pressed.
Usage: Connect this signal to perform an action when the button is pressed.

6. released():

Description: Emitted when the button is released.
Usage: Connect this signal to perform an action when the button is released.

7. toggled(bool checked):

Description: Emitted when the selected state of the button changes. It is only truevalid when checkable is .
Usage: Connect this signal to perform an action when the button's selected state switches.

8. windowIconChanged(const QIcon &icon):

Description: Emitted when the window icon changes.
Usage: Connect this signal to perform an action when the window icon changes.

9. windowIconTextChanged(const QString &iconText):

Description: Emitted when the text of the window icon changes.
Usage: Connect this signal to perform an action when the window icon text changes.

10. windowTitleChanged(const QString &title):

Description: Emitted when the window title changes.
Usage: Connect this signal to perform an action when the window title changes.

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    
    
    //this->setFixedSize(640, 480);//设置窗口大小为固定尺寸
    this->setFixedSize(QSize(640, 480));//通过匿名对象设置固定窗口大小
    this->setWindowTitle(QString("按钮"));//设置标题
    //this->setWindowIcon("");
    //构造按钮,指定父对象为this,实际就是当前界面的构造按钮
    QPushButton *btn = new QPushButton(this);
    btn->setText(QString("按钮1"));//setText():设置文本
    btn->resize(80, 50);//resize():重置按钮大小(w,h)
    //width():获取控件的宽度 height(): 获取控件高度
    btn->move(QPoint((width() - btn->width()) / 2, (height() - btn->height()) / 2));//移动按钮位置
    //#00(r红)00(g绿色)00(b蓝色)
    btn->setStyleSheet("border: 1px solid red;"
                        "border-top: 0;"
                        "border-left: 0;"
                        "border-right: 0;"
                        "background-color: #00aa00"
            );

    QPushButton *btn1 = new QPushButton(QString("按钮2"));
    btn1->setParent(this);
    //setGeometry():设置控件的位置和尺寸 x():获取控件的x坐标 y():获取控件y坐标
    btn1->setGeometry(btn->x() - 80, btn->y() - 50, 80, 50);
    btn1->setIcon(QIcon(":/qq.jpg"));
    btn1->setIconSize(QSize(80, 25));//设置图标的尺寸

    QPushButton *btn2 = new QPushButton(QString("按钮3"),this);
    btn2->setFixedSize(80, 50);
    btn2->move(btn->x() + btn->width(), btn->y() + btn->height());
    //btn2->setGeometry(btn->x() + 80, btn->y() + 50, 80, 50);

}

Widget::~Widget()
{
    
    

}

Guess you like

Origin blog.csdn.net/qq_57737603/article/details/132462936