在登录界面的登录取消按钮进行以下设置:
使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数
将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,弹出另一个界面。如果匹配失败,则输出登录失败,并将密码框中的内容清空
自己完成一个使用qss的登陆窗口界面【资源文件的添加、qss语法】。
代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QDockWidget>
#include <QMainWindow>
#include <QIcon>
#include <QMessageBox>
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;
signals:
void my_login();//登录信号
public slots:
void my_quit();
private slots:
void on_pushButton_clicked();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/Logo/logo.jpeg"));
this->setWindowTitle("QQ音乐");
this->setWindowFlag(Qt::FramelessWindowHint);
//去掉组件空白部分
this->setAttribute(Qt::WA_TranslucentBackground);
//手动链接系统提供的信号和自定义的槽函数
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(my_quit()));
//登录对应的槽函数
}
Widget::~Widget()
{
delete ui;
}
void Widget::my_quit()
{
this->close();
}
void Widget::on_pushButton_clicked()
{
if(ui->lineEdit->text() == "admin" && ui->lineEdit_2->text() == "123456")
{
qDebug() << "登录成功!";
this->close();//关闭当前页面
emit my_login();
}
else
{
qDebug() << "登录失败!";
QMessageBox::critical(this, "错误", "用户名或密码错误!");
ui->lineEdit_2->setText("");
}
}
widget.ui
*{
background-color: rgb(255, 255, 255);
}
QFrame#frame{
border-image: url(:/Logo/bg.png);
border-radius:30px;
}
#frame_2{
background-color: rgba(185, 185, 185, 120);
border-radius:30px;
}
QLabel#label{
border-radius:30px;
background-color: rgba(98, 98, 98, 120);
}
#label_2{
font: 14pt "新宋体";
background:transparent;
color: rgba(255, 255, 255, 180);
}
QLineEdit{
background:transparent;
border:none;
border-bottom:1px solid rgba(255, 255, 255, 150);
color: rgba(255, 255, 255, 150);
}
QPushButton{
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(107, 171, 255, 255), stop:1 rgba(255, 255, 255, 255));
border-radius:5px;
}
QPushButton:hover{
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(140, 200, 255, 255), stop:1 rgba(255, 255, 255, 255));
border-radius:5px;
}
QPushButton:pressed{
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(107, 171, 255, 255), stop:1 rgba(255, 255, 255, 255));
border-radius:5px;
padding-top:5px;
padding-left:5px;
}
#btn2{
background:transparent;
}
#btn2:hover{
background-color: rgb(255, 0, 0);
}
#btn3{
background:transparent;
}
#btn3:hover{
background-color: rgb(255, 0, 0);
}
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
#include <QIcon>
namespace Ui {
class second;
}
class second : public QWidget
{
Q_OBJECT
public:
explicit second(QWidget *parent = nullptr);
~second();
private:
Ui::second *ui;
public slots:
void success_login();
};
#endif // SECOND_H
second.cpp
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setWindowIcon(QIcon(":/Logo/logo.jpeg"));
this->setWindowTitle("QQ音乐");
}
second::~second()
{
delete ui;
}
void second::success_login()
{
this->show();
}
second.ui
*{
background-color: rgb(255, 255, 255);
}
QLabel{
border-image: url(:/Logo/sucai.png);
}
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
//实例化第二界面
second s;
QObject::connect(&w,&Widget::my_login,&s,&second::success_login);
return a.exec();
}
效果展示:登录界面
登录失败
登录成功跳转(仅贴图,无功能)
xmind