QT学习笔记一

一、窗口代码实例

二、.pro文件解释

三、创建按钮

四、信号和槽

五、自定义的信号和槽

六、信号连接信号

七、lambda表达式

一、窗口代码实例

#include "widget.h"

#include <QApplication>//包含一个应用程序类的头文件

int main(int argc, char *argv[])
{
    //应用程序对象,在整个qt中有且只有一个
    QApplication a(argc, argv);
    Widget w;
    //窗口对象默认不显示
    w.show();
    
    //让应用程序对象进入消息循环(窗口就不会一闪而过)
    return a.exec();
}

二、.pro文件解释

QT       += core gui    //Qt包含的模块

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets  //大于4版本以上包含widget模块

CONFIG += c++11DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES +=               //源文件
    main.cpp 
    widget.cpp

HEADERS +=     //头文件
    widget.h

FORMS +=     //
    widget.ui

TRANSLATIONS += 
    qt1_zh_CN.ts

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
、

三、创建按钮

1)成员方法

设置父窗口:setParent()

设置文本内容:setText()

设置大小:resize()

设置位置:move()

2)代码实例

#include "widget.h"
#include "ui_widget.h"
#include <qpushbutton>   //包含头文件
Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //法一 // QPushButton *btn1=new QPushButton; // //btn1->show(); //show()以顶层方式显示窗口 // btn1->setParent(this); // btn1->setText("按钮1"); //法二 QPushButton *btn2=new QPushButton("按钮2", this);
    btn2->move(100,100);//移动按钮

    resize(500,500);// 重置窗口大小
} Widget::~Widget() { delete ui; }

四、信号和槽

#include "widget.h"
#include "ui_widget.h"
#include <qpushbutton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //法一
//    QPushButton *btn1=new QPushButton;
//    //btn1->show();     //show()以顶层方式显示窗口
//    btn1->setParent(this);
//    btn1->setText("按钮1");

    //法二
    QPushButton *btn2=new QPushButton("按钮2", this);

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

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

    //参数1:信号发送者
    //参数2:触发事件
    //参数3:信号接受者
    //参数4:处理事件(槽)
    connect(btn2,&QPushButton::clicked,this,&Widget::close);
}

Widget::~Widget()
{
    delete ui;
}

 五、自定义的信号和槽

1)步骤

    1. 先在信号发送者的signals中定义一个信号,只需要声明不需要实现,支持重载;
    2. 然后在信号接受者的public slots中定义一个信号处理事件,支持重载;
    3. 接着定义一个发送信号的函数;
    4. 最后调用connect()函数,并运行发送信号的函数。

注意:①信号和槽的参数类型必须一样,个数可以不一样,但是信号的参数必须包含槽的所有参数。

           ②一个信号可以连接多个槽

           ③多个信号可以连接同一个槽

           ④信号和槽都没有返回值

2)代码实例

发送者类:

#include <QObject>

class Myclass : public QObject
{
    Q_OBJECT
public:
    explicit Myclass(QObject *parent = nullptr);

signals:
    void mmm();//发送信号
    void mmm(QString s);//发送信号
};

接收者类:

#include <QObject>

class Myclass2 : public QObject
{
    Q_OBJECT
public:
    explicit Myclass2(QObject *parent = nullptr);

signals:

public slots:
    void ReceiveMsg();//接收信号
    void ReceiveMsg(QString s);
};

Myclass2::Myclass2(QObject *parent) : QObject(parent)
{

}

void Myclass2::ReceiveMsg()
{
    qDebug()<<"接收信号!";
}

void Myclass2::ReceiveMsg(QString s)
{
    //输出QString的字符串有双引号,去掉双引号
    qDebug()<<s.toUtf8().data();
}

窗口类:

#include <QWidget>
#include <myclass.h>
#include <myclass2.h>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    Myclass *c1;
    Myclass2 *c2;
    void test();
};

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

     this->c1=new Myclass;
     this->c2=new Myclass2;

    //信号或槽发生重载时,这样处理
    void(Myclass:: *c1Signal)(QString)=&Myclass::mmm;

    void(Myclass2:: *c2Slot)(QString)=&Myclass2::ReceiveMsg;

    //参数1:信号发送者
    //参数2:触发事件
    //参数3:信号接受者
    //参数4:处理事件(槽)
    connect(c1,c1Signal,c2,c2Slot);
    test();
}

void Widget::test()
{
    emit this->c1->mmm("mmm");
}

Widget::~Widget()
{
    delete ui;
}

六、信号连接信号

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton *btn =new QPushButton("发送",this);


     this->c1=new Myclass;
     this->c2=new Myclass2;

    //信号或槽发生重载时,这样处理
    void(Myclass:: *c1Signal)(void)=&Myclass::mmm;

    void(Myclass2:: *c2Slot)(void)=&Myclass2::ReceiveMsg;

    //参数1:信号发送者
    //参数2:触发事件
    //参数3:信号接受者
    //参数4:处理事件(槽)
    connect(c1,c1Signal,c2,c2Slot);

    //信号连接信号
    void(Myclass:: *c1Signal1)(void)=&Myclass::mmm;
    connect(btn,&QPushButton::clicked,c1,c1Signal1);

}

Widget::~Widget()
{
    delete ui;
}

七、lambda表达式

参考连接

原文地址:https://www.cnblogs.com/mango1997/p/14636304.html