本文主要是介绍Qt---day2-信号与槽,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、思维导图
2、 拖拽式
源文件
#include "mywidget.h"#include "ui_mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent), ui(new Ui::MyWidget){ui->setupUi(this);//按钮2this->btn2=new QPushButton("按钮2",this);btn2->resize(ui->pushButton->size());btn2->move(ui->pushButton->x(),ui->pushButton->y()+80);connect(this->btn2,&QPushButton::clicked,this,&MyWidget::bnt2_clicked);//按钮3this->btn3 =new QPushButton("按钮3",this);btn3->resize(ui->pushButton->size());btn3->move(this->btn2->x(),this->btn2->y()+80);connect(this->btn3,&QPushButton::clicked,[&](){qDebug() << "lalalla";});}MyWidget::~MyWidget(){delete ui;}//通过槽创建的按钮生成的连接void MyWidget::on_pushButton_clicked(){qDebug() << "你点击了按钮1";}//按钮2的连接函数void MyWidget::bnt2_clicked(){static int num=0;if(num%3==0){this->setStyleSheet("background-color:red");}else if(num%3==1){this->btn2->setStyleSheet("background-color:green");}else{this->btn2->setStyleSheet("background-color:blue");}num++;}
头文件
#ifndef MYWIDGET_H#define MYWIDGET_H#include <QWidget>#include <QDebug>#include <QPushButton>#include <QtTextToSpeech/QTextToSpeech>QT_BEGIN_NAMESPACEnamespace Ui { class MyWidget; }QT_END_NAMESPACEclass MyWidget : public QWidget{Q_OBJECT//signals:// void mySignal(void);//public slots:// void mySlots(void);void bnt2_clicked();public:MyWidget(QWidget *parent = nullptr);~MyWidget();private slots:void on_pushButton_clicked();private:Ui::MyWidget *ui;QPushButton *btn2;QPushButton *btn3;QTextToSpeech *speecher;};#endif // MYWIDGET_H
3、代码式
头文件
#ifndef MYWIDGET_H#define MYWIDGET_H#include <QWidget>#include<QIcon> //图标类#include<QLabel> //标签类#include<QMovie> //动图类#include<QLineEdit> //行编辑器类#include<QPushButton> //按钮类#include <QDebug>class MyWidget : public QWidget{Q_OBJECT;QPushButton *btn;QLineEdit *edit1;QLineEdit *edit2;public:MyWidget(QWidget *parent = nullptr);~MyWidget();void signal();};#endif // MYWIDGET_H
源文件
#include "mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent){//==============窗口相关设置=======this->resize(540,415);this->setFixedSize(540,415);//窗口标题this->setWindowTitle("盗版QQ");//窗口图标this->setWindowIcon(QIcon(":/pictrue/qq.png"));//背景颜色this->setStyleSheet("background-color:white");//去掉头部this->setWindowFlag(Qt::FramelessWindowHint);//============标签相关设置=======QLabel *lab1 = new QLabel(this);//设置大小lab1->resize(540, 160);lab1->setStyleSheet("background-color:pink");//动图类 接收动图QMovie *mv = new QMovie(":/pictrue/zz.gif");//将动图放入标签中lab1->setMovie(mv);//让动图动起来mv->start();//自动适应lab1->setScaledContents(true);QLabel *lab2 = new QLabel(this);lab2->resize(30,30);lab2->move(120,210);lab2->setPixmap(QPixmap(":/pictrue/wodepeizhenshi.png"));lab2->setScaledContents(true);QLabel *lab3 = new QLabel(this);lab3->resize(30,30);lab3->move(120, 260);lab3->setPixmap(QPixmap(":/pictrue/passwd.jpg"));lab3->setScaledContents(true);//============行编辑器相关设置=======QLineEdit *edit1 = new QLineEdit(this);edit1->resize(275,30);edit1->move(155,210);edit1->setPlaceholderText("QQ号/手机号/邮箱");QLineEdit *edit2 = new QLineEdit(this);edit2->resize(275,30);edit2->move(155,260);edit2->setPlaceholderText("密码");edit2->setEchoMode(QLineEdit::Password);//============按钮相关设置=======QPushButton *btn = new QPushButton("登录",this);btn->resize(300,45);btn->move(120,345);//样式函数setStyleSheet()btn->setStyleSheet("background-color:rgb(8,189,253);border-radius:5px;color:white");// connect(btn,&QPushButton::clicked,edit1,&QLineEdit::textChanged);// connect(btn,&QPushButton::clicked,edit2,&QLineEdit::textChanged);connect(edit1,&QLineEdit::textChanged,this,&MyWidget::signal);connect(edit2,&QLineEdit::textChanged,this,&MyWidget::signal);}MyWidget::~MyWidget(){}void MyWidget::signal(){if(edit1->text().size()>=5 && edit2->text().size()>=5){btn->setStyleSheet("background-color:green");}if(edit1->text()=="paomao"&&edit2->text()=="555555"){qDebug() << "登录成功";this->close();}else{qDebug() << "登录失败,密码账号有错误,请重新输入";edit1->clear();edit2->clear();}}
这篇关于Qt---day2-信号与槽的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!