3. Commonly used APIs for button controls

3.1. Two ways to create buttons

In Qt, one of the most commonly used controls is buttons. Buttons can be created in the following two ways:

Note: Be sure to include the header file of the button control before creating the button.#include <QPushButton>

#include <QPushButton>//按钮控件的头文件

1. Create first, then assign value

    //创建一个按键
    QPushButton *btn=new QPushButton;
    //btn->show();//show以顶层的方式弹出窗口控件
    //让btn对象 依赖在myWidget窗口中
    btn->setParent(this);

    //显示文本
    btn->setText("第一个按钮");

In the above code, a button is actually an object under the QPushButton class. If you just create an object, it cannot be displayed in the window, so we need to rely on a parent window, that is, specify a parent to use the setParentfunction. If you want to set The text displayed on the button is used to setTextmove the button position.move


2. Assign value while creating

 //创建第二个按钮  按照控件大小创建窗口
    QPushButton *btn2=new QPushButton("第二个按钮",this);

    //移动btn2按钮
    btn2->move(100,100);

    //重新指定按钮大小
    btn2->resize(200,200);

    //重置窗口大小
    resize(600,400);

    //设置固定窗口大小
    setFixedSize(600,400);

    //设置窗口标题
    setWindowTitle("第一个窗口");

The second creation method assigns a value to the button when a new button is pressed, and binds it to the myWidget parent class window.

For windows, we can modify the title of the window in the upper left corner setWindowTitle, resize the window: resize, or set a fixed window size setFixedSize;


Show results:

summary:

Summarize:

  1. createQPushButton * btn = new QPushButton
  2. set father setParent(this)
  3. Set textsetText(“文字”)
  4. Set location move(宽,高)
  5. Resize windowresize
  6. Set window titlesetWindowTitle
  7. Set the window to a fixed size小setFixedSize

3.2. Create your own button object

1. Create a button class (MyPushButton)

Create a mypushbutton.hheader file named and write the class declaration in it

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>

class MyPushButton : public QPushButton//公有继承于QPushButton
{
    Q_OBJECT
public:
    explicit MyPushButton(QWidget *parent = nullptr);//构造函数参数默认为空

    ~MyPushButton();//默认析构

signals:

};

#endif // MYPUSHBUTTON_H

explicitIt is only used to modify a constructor with only one parameter. It is used to indicate that the constructor is explicit (implicit conversion and automatic type conversion are prohibited). On the contrary, it implicitindicates that the constructor is implicit.

For more information about explicit, please refer to the following articles or go to csdn yourself:

Detailed explanation of C++ explicit keyword_c explicit-CSDN blog

Create mypushbutton.cppa source file named for writing the implementation of the class

#include "mypushbutton.h"//链接类的声明
#include <QDebug>//Debug文字需要头文件

MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)//构造函数初始化赋值
{
    qDebug()<<"我的按钮类构造调用";
}

MyPushButton::~MyPushButton()//析构
{
    qDebug()<<"我的按钮类析构";
}

At this point, the class is created, and we can instantiate the object.


2. Create your own button object

//创建一个自己的按钮对象
    MyPushButton *myBtn=new MyPushButton;
    myBtn->setText("我自己的按钮");
    myBtn->move(200,0);
    myBtn->setParent(this);//设置到对象树中,窗口释放的时候会被自动释放

Based on the method we used to create buttons before, we can instantiate the button we implemented.

Guess you like

Origin blog.csdn.net/qq_63388834/article/details/134997273