Specify parent objects and signals and slots

Specify parent objects and signals and slots

1. Specify the parent object

  1. setWindowTitlefunction:
void setWindowTitle(const QString &)

参数:const QString &相当于string类型
作用:给窗口设置标题

  1. setTextfunction:
void setText(const QString &text)

参数:const QString &相当于string类型
作用:给按钮设置内容

  1. move function:
void move(int x, int y)

Parameter: integer value, is a coordinate
function: set the position, for example, the button is by default in the upper left corner of the window, we use this function to change the position of the button

  1. QWidget::setParentfunction:
void QWidget::setParent(QWidget *parent)

参数: QWidget *parent传递一个窗口类型的指针
Role: Specify the parent object

  1. 什么叫指定父对象???
    Suppose one of the following scenarios:
#include <QApplication>
#include <QWidget>//窗口基类
#include <QPushButton>//按钮类

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);//定义一个Qt应用程序对象

    QWidget w;//定义一个窗口对象
    w.setWindowTitle("hello QT!!!");//给窗口设置标题
    w.show();//显示窗口

    QPushButton b;//定义一个按钮
    b.setText("is me");//给按钮设置内容
    
    b.show();//显示按钮
    app.exec();//循环执行
    return 0;
}

a. 如果没有指定父对象,对象和对象(窗口和窗口)之间没有任何关系You will find that your window and button are two windows respectively. Our purpose is to put the button in the window, so here you need to specify the parent object and place the button in the window.
b. c. d. , the button does not need to show指定父对象的规则:a指定b为它的父对象,就将a放在b的上面
指定父对象的方法:1).setParent;2).通过构造函数传参
指定父对象后只需要父窗口显示

#include <QApplication>
#include <QWidget>
#include <QPushButton>

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);

    QWidget w;
    w.setWindowTitle("hello QT!!!");

    QPushButton b;
    b.setText("is me");
    b.setParent(&w);//通过setParent函数指定父对象
    b.move(100,100);

	QPushButton b1(&w);//通过构造函数指定父对象
    b1.setText("also me");
    b1.move(80,80);

    w.show();
    app.exec();
    return 0;
}

In QT,父对象/父窗口/父控件是一个意思,子对象/子窗口/子控件是一个意思

2. Signals and slots

1. Sort out the basic process:
main.cpp Medium:

#include "mainwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();

    return a.exec();
}

First program execution QApplication a(argc, argv); complete reference base class constructor, then perform MainWidget w;configuration completion window object, then execute w.show();display the main window, the last execution return a.exec();block waiting for a user's operation.

2. Some considerations

. a 这就是main函数的基本框架,我们用户自定义的功能都是在w这个派生类的构造函数当中完成a
. b a wrong code:
mainwidget.cpp:

#include "mainwidget.h"
#include <QPushButton>


MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
	QPushButton b1;
    b1.setParent(this);
    b1.setText("b1");
    b1.move(10,10);
}

MainWidget::~MainWidget()
{
}

你们决定这个程序会在窗口上显示b1按钮吗???答案是不会Because the b1 object is destroyed after the b1是构造函数的局部变量,出了这个作用域后就被销毁execution is completed , the b1 button is not displayed because it has not MainWidget w;yet come and executed w.show();.
c. 注意QT中的指针是必须要new对象的Otherwise, an error will occur.
mainwidget.h:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QPushButton>

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();
private:
    QPushButton b1;
    QPushButton* b2;
};

#endif // MAINWIDGET_H

mainwidget.cpp:

#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    b1.setParent(this);
    b1.setText("b1");
    b1.move(10,10);

    //b2->serParent(this);
    b2 = new QPushButton(this);
    b2->setText("b2");
    b2->move(99,99);
}

MainWidget::~MainWidget()
{
    delete b2;
}

3. Signals and slots

The basic function has been understood here, but we have a button, but there is no response when the button is clicked. Here, the signal and slot are derived.

  1. 信号:当对象改变其状态时,信号就由该对象发射 (emit) 出去,而且对象只负责发送信号,它不知道另一端是谁在接收这个信号。这样就做到了真正的信息封装,能确保对象被当作一个真正的软件组件来使用
  2. 槽:用于接收信号,而且槽只是普通的对象成员函数。一个槽并不知道是否有任何信号与自己相连接。而且对象并不了解具体的通信机制

4. connect function:

bool QObject::connect ( const QObject * sender, const char * signal, 
		 const QObject * receiver, const char * member ) [static]
parameter Explanation
const QObject * sender Who sent the signal
const char * signal What signal is sent, the class name of the sender :: the name of the signal
const QObject * receiver Who receives
const char * member What the receiver does, the signal processing function, the class name of the receiver :: slot function name
Function of function Connect the signal and the slot. When the signal sender sends out a signal, it is received by the receiver. When the signal is triggered, the receiver executes the signal processing function.
#include "mainwidget.h"

//该函数的功能是当按下的时候发生的,接收者的处理是关闭窗口
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    b1.setParent(this);
    b1.setText("close");
    b1.move(10,10);
    
    //QPushButton::pressed:代表发送者发送的信号
    //MainWidget::close:代表接收者执行的动作
    /*[signal] void QAbstractButton::pressed()
	This signal is emitted when the button is pressed down.
	See also released() and clicked().*/
    connect(&b1,&QPushButton::pressed,this,&MainWidget::close);
}

MainWidget::~MainWidget()
{
    delete b2;
}

5. Custom slot function

  1. 自定义槽函数(和普通函数用法基本一致)
  2. QT5的槽函数:任意的函数、普通全局函数、静态函数
  3. 槽函数需要和信号一致(参数,返回值)
  4. 由于信号是没有返回值的,所以槽一定是没有返回值的

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QPushButton>

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();
    void MySolt();//自定义槽函数声明
private:
    QPushButton b1;
    QPushButton* b2;
};

#endif // MAINWIDGET_H

mainwidget.cpp

#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    b1.setParent(this);
    b1.setText("close");
    b1.move(10,10);

    //b2->serParent(this);
    b2 = new QPushButton(this);
    b2->setText("99999");
    b2->move(99,99);

	/*void QAbstractButton::released()
	This signal is emitted when the button is released.
	void QWidget::hide()
	Hides the widget. This function is equivalent to setVisible(false).
	Note: If you are working with QDialog or its subclasses and you 				invoke the show() function after this function, the dialog will be 	
	displayed in its original position.*/

	//关联b2按钮和主窗口,当按钮抬起时执行MySolt自定义槽函数
    connect(b2,&QPushButton::released,this,&MainWidget::MySolt);
    //关联b2按钮和b1按钮,当按钮抬起时执行hide槽函数
    connect(b2,&QPushButton::released,&b1,&QPushButton::hide);
}

void MainWidget::MySolt()//自定义槽函数实现
{
	//将b2按钮当中的内容换为666
    b2->setText("6666");
}

MainWidget::~MainWidget()
{
    delete b2;
}

Published 158 original articles · praised 117 · 90,000 views

Guess you like

Origin blog.csdn.net/wolfGuiDao/article/details/105461276