#include "mylogin.h" mylogin::mylogin(QWidget *parent) : QDialog(parent) { this->init_ui(); connect(this->bnt_login, &QPushButton::clicked, this, &mylogin::do_login); connect(this->bnt_register, &QPushButton::clicked , this ,&mylogin::do_enroll); } mylogin::~mylogin() { } void mylogin::init_ui() { this->setFixedSize(QSize(600,350)); this->setWindowTitle(tr("岑超升")); this->setWindowIcon(QIcon(":/src/1.png")); this->lb1 = new QLabel(); this->lb2 = new QLabel(); this->lb3 = new QLabel(); this->lb1->setFixedSize(QSize(560,200)); QPixmap pic; pic.load(":/src/2.png"); //this->lb1->setPixmap(pic.scaled(this->lb1->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); this->lb1->setPixmap(QPixmap(":/src/2.png")); this->lb2->setText(tr("用户名:")); this->lb3->setText(tr("密 码:")); this->usr_name_le = new QLineEdit(); //这两个参数后面传入信号中去,然后emit发射出去 this->usr_pass_le = new QLineEdit(); this->usr_pass_le->setEchoMode(QLineEdit::Password); this->bnt_login = new QPushButton(tr("登 陆")); this->bnt_register = new QPushButton(tr("注 册")); this->hb1 = new QHBoxLayout(); this->hb2 = new QHBoxLayout(); this->hb3 = new QHBoxLayout(); this->hb1->addWidget(this->lb2); this->hb1->addWidget(this->usr_name_le); this->hb2->addWidget(this->lb3); this->hb2->addWidget(this->usr_pass_le); this->hb3->addWidget(this->bnt_login); this->hb3->addWidget(this->bnt_register); this->vb1 = new QVBoxLayout(); this->vb1->addWidget(this->lb1); this->vb1->addLayout(this->hb1); this->vb1->addLayout(this->hb2); this->vb1->addLayout(this->hb3); this->setLayout(this->vb1); } void mylogin::do_login() { emit sig_login(usr_name_le->text(), usr_pass_le->text()); //需要把这里输入的账号密码信息发送到myapp那去,用到的函数是emit //需要自定义一个信号,sig_login //这个槽函数能够发出信号 } void mylogin::do_enroll() { emit sig_enroll(); }解释每一行代码
时间: 2024-03-18 11:43:14 浏览: 321
Qt-study.zip_Qt经典_site:www.pudn.com
#include "mylogin.h" //引入自定义头文件mylogin.h
mylogin::mylogin(QWidget *parent) : QDialog(parent) { //mylogin的构造函数,继承自QDialog类,parent为其父窗口
this->init_ui(); //初始化UI界面
connect(this->bnt_login, &QPushButton::clicked, this, &mylogin::do_login); //连接登陆按钮的点击事件和槽函数do_login
connect(this->bnt_register, &QPushButton::clicked , this ,&mylogin::do_enroll); //连接注册按钮的点击事件和槽函数do_enroll
}
mylogin::~mylogin() { //mylogin的析构函数
}
void mylogin::init_ui() { //初始化UI界面的函数
this->setFixedSize(QSize(600,350)); //设置窗口大小
this->setWindowTitle(tr("岑超升")); //设置窗口标题
this->setWindowIcon(QIcon(":/src/1.png")); //设置窗口图标
this->lb1 = new QLabel(); //新建一个标签
this->lb2 = new QLabel(); //新建一个标签
this->lb3 = new QLabel(); //新建一个标签
this->lb1->setFixedSize(QSize(560,200)); //设置标签大小
QPixmap pic; //新建一个QPixmap类的对象pic
pic.load(":/src/2.png"); //将图片资源载入到pic中
this->lb1->setPixmap(QPixmap(":/src/2.png")); //设置标签的图片为载入的图片
this->lb2->setText(tr("用户名:")); //设置标签显示的文本
this->lb3->setText(tr("密 码:")); //设置标签显示的文本
this->usr_name_le = new QLineEdit(); //新建一个单行文本框
this->usr_pass_le = new QLineEdit(); //新建一个单行文本框
this->usr_pass_le->setEchoMode(QLineEdit::Password); //设置文本框的显示模式为密码模式
this->bnt_login = new QPushButton(tr("登 陆")); //新建一个按钮
this->bnt_register = new QPushButton(tr("注 册")); //新建一个按钮
this->hb1 = new QHBoxLayout(); //新建一个水平布局
this->hb2 = new QHBoxLayout(); //新建一个水平布局
this->hb3 = new QHBoxLayout(); //新建一个水平布局
this->hb1->addWidget(this->lb2); //将标签lb2添加到hb1布局中
this->hb1->addWidget(this->usr_name_le); //将单行文本框usr_name_le添加到hb1布局中
this->hb2->addWidget(this->lb3); //将标签lb3添加到hb2布局中
this->hb2->addWidget(this->usr_pass_le); //将单行文本框usr_pass_le添加到hb2布局中
this->hb3->addWidget(this->bnt_login); //将按钮bnt_login添加到hb3布局中
this->hb3->addWidget(this->bnt_register); //将按钮bnt_register添加到hb3布局中
this->vb1 = new QVBoxLayout(); //新建一个垂直布局
this->vb1->addWidget(this->lb1); //将标签lb1添加到vb1布局中
this->vb1->addLayout(this->hb1); //将hb1布局添加到vb1布局中
this->vb1->addLayout(this->hb2); //将hb2布局添加到vb1布局中
this->vb1->addLayout(this->hb3); //将hb3布局添加到vb1布局中
this->setLayout(this->vb1); //将vb1布局设置为mylogin的布局
}
void mylogin::do_login() { //登陆按钮的槽函数
emit sig_login(usr_name_le->text(), usr_pass_le->text()); //发射自定义信号sig_login,并将单行文本框usr_name_le和usr_pass_le中的内容作为参数传递
}
void mylogin::do_enroll() { //注册按钮的槽函数
emit sig_enroll(); //发射自定义信号sig_enroll
}
阅读全文