[Qt notes] 3. Simple use of various buttons

1 Overview

QPushButton is a button control that comes with QT.
You need to add a header file to use it.

#include <QPushButton> 

QPushButton is integrated from QAbstractButton, and the structure of other buttons at the same level is as follows:

QPushButton QAbstractButton QRadioButton QCheckBox QWidget QToolButton inherit inherit inherit inherit inherit

2 QPushButton

constructor prototype Instructions for use
QPushButton::QPushButton(QWidget *parent = nullptr) QPushButton *btn = new QPushButton
QPushButton::QPushButton(const QString &text, QWidget *parent = nullptr) QPushButton *btn = new QPushButton("text")
QPushButton::QPushButton(const QIcon &icon, const QString &text, QWidget *parent = nullptr) icon parameter to add icon

When QT is downloaded, the corresponding compiler provides assistant.exe for reference, so in actual development, these related methods and classes do not need to be memorized by yourself.
QT\5.14.2\mingw73_64\bin
insert image description here
Most of the scenarios where we use buttons are Click to trigger the slot function.
It needs a signal sender, the signal sent, and the signal receiver, the received information.
The form is as follows, and the unaccepted information can be processed as an anonymous function.

    connect(btn,&QPushButton::clicked, [=](){
    
    
    	//写个槽函数
        std::cout << "demo";
        }
        );

There is a member function setMenu in QPushButton, which can set the menu drop-down box for the button

    m_btn = new QPushButton(this);
    m_menu = new QMenu;
    m_menu->addMenu("菜单1");
    m_menu->addMenu("菜单2");
    m_menu->addMenu("菜单3");
    m_btn->setMenu(m_menu);

The phenomenon is as follows, click the button to see the drop-down box
insert image description here

3 QRadioButton

radio button construction

    QRadioButton *m_btn = new QRadioButton("QRadioButton",this);

insert image description here

4 QCheckBox

//构造函数
QCheckBox *m_checkbox = new QCheckBox("C&ase sensitive", this);
 //通过此方法来确定当前的勾选状态,总共有三种状态可供选择
m_checkbox->setCheckState(Qt::Checked);
//m_checkbox->setCheckState(Qt::Checked);
//    enum CheckState {
    
    
//        Unchecked,
//        PartiallyChecked,
//        Checked
//    };

insert image description here

5 QToolButton

Tool buttons usually do not need to display text but only display related icons. The construction and usage methods are as follows:

    QToolButton *m_toolButton = new QToolButton(this);
    m_toolButton->setArrowType(Qt::LeftArrow);
    //设置按钮自动凸起
    m_toolButton->setAutoRaise(true);

insert image description here

Guess you like

Origin blog.csdn.net/qq_38753749/article/details/129962191