qt部分核心机制
作业
1> 手动将登录项目实现,不要使用拖拽编程
并且,当点击登录按钮时,后台会判断账号和密码是否相等,如果相等给出登录成功的提示,并且关闭当前界面,发射一个跳转信号,如果登录失败,则给出登录失败的提示,并清空密码框
当点击取消按钮时,直接关闭当前登录框
.h文件
#ifndef MYJOB_H
#define MYJOB_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QWidget>
#include<QLineEdit>
#include <QObject>
#include <QMessageBox>
class myjob : public QMainWindow
{
Q_OBJECT
signals:
void my_signal(); //定义一个信号函数
public slots:
void btn1_yes_slot(); //自定义槽函数
void btn2_no_slot();
private slots:
void showMessage()
{
QMessageBox::information(this,"Information","登录失败!");
}
public:
myjob(QWidget *parent = nullptr);
~myjob();
private:
QLabel *btn1_Account_label;
QLabel *btnlogo_Icon_label;
QLabel *btn2_Cryptographic_tag_label;
QLineEdit *btn1_Account_entry;
QLineEdit *btn2_Password_input;
QPushButton *btn1_yes;
QPushButton *btn2_no;
};
#endif // MYJOB_H
源码文件
#include "myjob.h"
myjob::myjob(QWidget *parent)
: QMainWindow(parent)
{
//主页面设置
this->setFixedSize(400,600); //主页面大小
//logo图标(也是标签)
this->btnlogo_Icon_label = new QLabel;
btnlogo_Icon_label->setParent(this); //设置父组件
btnlogo_Icon_label->resize(400,150); //设置标签宽高
btnlogo_Icon_label->move(0,0); //移动标签位置
btnlogo_Icon_label->setText("霜雪阅读"); //设置文本
btnlogo_Icon_label->setStyleSheet("background-color:green;"); //设置背景颜色,否则看不见
btnlogo_Icon_label->setAlignment(Qt::AlignCenter); //设置对齐方式
//账户标签
btn1_Account_label = new QLabel; //无参构造
btn1_Account_label->setParent(this); //设置父组件
btn1_Account_label->resize(50,20); //设置标签宽高
btn1_Account_label->move(50,200); //移动标签位置
btn1_Account_label->setText("账户"); //设置文本
btn1_Account_label->setStyleSheet("background-color:red;"); //设置背景颜色,否则看不见
btn1_Account_label->setAlignment(Qt::AlignCenter); //设置对齐方式
//账户输入
btn1_Account_entry = new QLineEdit(this); //有参构造并设置父组件
btn1_Account_entry->resize(150,20); //设置宽和高
btn1_Account_entry->move(btn1_Account_label->x()+btn1_Account_label->width()+10, btn1_Account_label->y());
//btn1_Account_entry->setStyleSheet("background-color:pick;"); //设置背景颜色,否则看不见
btn1_Account_entry->setAlignment(Qt::AlignCenter); //设置对齐方式
btn1_Account_entry->setPlaceholderText("请输入账号"); //设置占位文本
//密码标签
btn2_Cryptographic_tag_label = new QLabel;
btn2_Cryptographic_tag_label->setParent(this); //设置父组件
btn2_Cryptographic_tag_label->resize(50,20); //设置标签宽高
btn2_Cryptographic_tag_label->move(btn1_Account_label->x(),btn1_Account_label->y()+btn1_Account_label->height()+15); //移动标签位置
btn2_Cryptographic_tag_label->setText("密码"); //设置文本
btn2_Cryptographic_tag_label->setStyleSheet("background-color:red;"); //设置背景颜色,否则看不见
btn2_Cryptographic_tag_label->setAlignment(Qt::AlignCenter); //设置对齐方式
//密码输入
btn2_Password_input = new QLineEdit(this); //有参构造并设置父组件
btn2_Password_input->resize(150,20); //设置宽和高
btn2_Password_input->move(btn2_Cryptographic_tag_label->x()+btn2_Cryptographic_tag_label->width()+10, btn2_Cryptographic_tag_label->y());
//btn2_Password_input->setStyleSheet("background-color:pick;"); //设置背景颜色,否则看不见
btn2_Password_input->setAlignment(Qt::AlignCenter); //设置对齐方式
btn2_Password_input->setPlaceholderText("请输入密码"); //设置占位文本
btn2_Password_input->setEchoMode(QLineEdit::Password); //设置密文
//登录按钮
btn1_yes = new QPushButton("确认",this); //有参构造并设置父组件及填充文本
btn1_yes->resize(50,35); //设置按键的宽和高
btn1_yes->move(btn2_Cryptographic_tag_label->x()+btn2_Cryptographic_tag_label->x(),
btn2_Cryptographic_tag_label->y()+200);
//取消按钮
btn2_no = new QPushButton("取消",this); //有参构造并设置父组件及填充文本
btn2_no->resize(50,35); //设置按键的宽和高
btn2_no->move(btn1_yes->x()+100,
btn1_yes->y());
//连接登录按钮
QObject::connect(this->btn1_yes,&QPushButton::clicked(,this,&Mainlogin::btn1_yes_slot));
//连接取消按钮
}
myjob::~myjob()
{
}
测试文件
#include "myjob.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myjob w;
w.show();
return a.exec();
}
2> 思维导图
3>两篇刷题28/30