要求如下:
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <iostream>
#include <QMainWindow>
#include <QWidget>
#include <QDebug>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QPicture>
#include <synchapi.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
//系统定义的btn1发送的信号对应的槽函数
void on_btn1_clicked();
//系统定义的btn2发送的信号对应的槽函数
void on_btn2_clicked();
private:
Ui::Widget *ui;
QLineEdit *edit1;
QLineEdit *edit2;
};
#endif // WIDGET_H
功能函数:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置窗口标题
this->setWindowTitle("英雄联盟手游登录界面"); //使用字符串传递
//this->setWindowTitle(tr("我的第一个窗口")); //使用tr函数将c风格字符串转换为QString进行传递
//this->setWindowTitle(QString("我的第一个窗口")); //使用匿名对象
//设置窗口图标C:\Users\韦新宇\Desktop
this->setWindowIcon(QIcon("C:/Users/韦新宇/Desktop/icon.png")); //一个“/”相当于两个\\
//QIcon是图标类,此处使用的是匿名对象进行传递,调用了QIon的有参构造函数
//设置logo标签
QLabel *lab=new QLabel(this);
lab->setPixmap(QPixmap("C:/Users/韦新宇/Desktop/ying.png"));
//设置窗口最小、最大尺寸
this->setMinimumSize(400,300);
this->setMaximumSize(1000,800);
this->setFixedSize(794,385);
//设置背景颜色
this->setBackgroundRole(QPalette::Dark);
this->setAutoFillBackground(true);
//设置登录标签
QLabel *lab1=new QLabel("账号 ",this);
lab1->move(100,200); //移动组件位置
//设置登录账号
edit1=new QLineEdit(this);
edit1->move(140,200); //移动组件位置
//设置密码标签
QLabel *lab2=new QLabel("密码 ",this);
lab2->move(100,250); //移动组件位置
//设置登录密码
edit2=new QLineEdit(this);
edit2->move(140,250); //移动组件位置
edit2->setEchoMode(QLineEdit::Password);
//按钮登录
QPushButton *btn1=new QPushButton("登录",this); //有参构造
btn1->move(150,300); //移动组件位置
//按钮取消
QPushButton *btn2=new QPushButton("取消",this); //有参构造
btn2->move(240,300); //移动组件位置
//系统定义的btn1发送的信号对应的槽函数
connect(btn1,&QPushButton::clicked,this,& Widget::on_btn1_clicked);
//系统定义的btn2发送的信号对应的槽函数
connect(btn2,&QPushButton::clicked,this,& Widget::on_btn2_clicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn1_clicked()
{
//获取文本
QString msg=edit1->text();
QString msg1=edit2->text();
if(strcmp("admin",qPrintable(msg))==0 && strcmp("123456",qPrintable(msg1))==0)
{
qDebug()<<"密码正确登陆成功!";
Sleep(2000);
this->close();
}
}
void Widget::on_btn2_clicked()
{
this->close();
qDebug()<<"退出成功!";
}
主函数:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
实现结果
两秒后自动退出
退出直接点击取消