chat.h
#ifndef CHAT_H
#define CHAT_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlRecord>
namespace Ui {
class chat;
}
class chat : public QWidget
{
Q_OBJECT
public:
explicit chat(QWidget *parent = nullptr);
~chat();
void jump_slot();
private slots:
void on_pushbutton1_clicked();
void on_pushbutton2_clicked();
private:
Ui::chat *ui;
QSqlDatabase db;
};
#endif // CHAT_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QComboBox>
#include <QObject>
#include <QDebug>
#include <QMessageBox>
#include <QPixmap>
#include "chat.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlRecord>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_btn2_clicked();
void on_btn3_clicked();
void on_btn4_clicked();
signals:
void jump();
private:
Ui::Widget *ui;
chat *s1;
};
#endif // WIDGET_H
chat.cpp
#include "chat.h"
#include "ui_chat.h"
chat::chat(QWidget *parent) :
QWidget(parent),
ui(new Ui::chat)
{
ui->setupUi(this);
if(!db.contains("mydatabase.db"))
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mydatabase.db");
}
if(!db.open())
{
QMessageBox::information(this," 失败","数据库打开失败");
return ;
}
QString sql = "create table if not exists usr("
"account varchar(10),"
"password varchar(15))";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","创建表失败");
return ;
}
}
chat::~chat()
{
delete ui;
}
void chat::jump_slot()
{
this->show();
}
void chat::on_pushbutton1_clicked()
{
QString account = ui->pushbutton1->text();
QString password = ui->pushbutton2->text();
if(account.isEmpty() || password.isEmpty())
{
QMessageBox::information(this,"提示","请讲信息填写完整");
return ;
}
QString sql = QString("insert into usr(account,password)"
"values('%1','%2')").arg(account).arg(password);
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"失败","注册失败");
return ;
}
else
{
QMessageBox::information(this,"成功","注册成功");
}
}
void chat::on_pushbutton2_clicked()
{
this->close();
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
s1 = new chat;
connect(this,&Widget::jump,s1,&chat::jump_slot);
this->setWindowTitle("221#223社畜登录框"); //设置窗口标题
this->setWindowIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置窗口图标
this->setStyleSheet("background-color:skyblue;"); //设置窗口样式表
this->setWindowOpacity(1); //设置窗口透明度
ui->lab1->setPixmap(QPixmap(":/new/prefix1/C:/Users/Administrator/Desktop/t/(}OLKA@NAP5WIO@0SMS9OLG.png"));
ui->lab1->setScaledContents(true); //填充标签
ui->btn1->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
ui->btn2->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
ui->btn3->setIcon(QIcon(":/new/prefix1/C:/Users/Administrator/Desktop/t/QQ.png")); //设置图标
ui->btn4->setStyleSheet("background-color:skyblue; border-radius:10px; ");
}
Widget::~Widget()
{
delete ui;
}
//登录按钮
void Widget::on_btn2_clicked()
{
QString sql = "select * from usr";
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","匹配失败");
return ;
}
int i = 0;
while(querry.next())
{
for(int j = 0; j < querry.record().count(); j++)
{
//登录失败
if(ui->edit1->text() != querry.record().value(j) && ui->edit2->text() != querry.record().value(j))
{
//调用构造函数实例化对象
QMessageBox box(QMessageBox::Critical,//图标
"错误",//对话框标题
"账号密码不匹配,是否重新登录",//对话框文本
QMessageBox::Yes|QMessageBox::No,//提供的按钮
this);//父组件
box.setDefaultButton(QMessageBox::Yes);
box.setButtonText(QMessageBox::Yes,"OK");
box.setButtonText(QMessageBox::No,"cancel");
//调用exec函数运行对话框
int ret = box.exec();
//对结果进行判断
if(ret==QMessageBox::Yes)
{
//清空内容
ui->edit1->clear();
ui->edit2->clear();
}
else
{
//关闭界面
this->close();
}
}
//登录成功
else
{
//调用构造函数实例化对象
QMessageBox box(QMessageBox::NoIcon,//图标
" ",//对话框标题
"登录成功",//对话框文本
QMessageBox::Ok,//提供的按钮
this);//父组件
box.exec();//运行对话框
emit jump();//跳转界面
this->close();//关闭当前界面
}
}
i++;
}
}
void Widget::on_btn3_clicked()
{
int ret=QMessageBox::warning(this,
"警告",
"是否确定要退出登录",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
//对结果进行判断如果点yes则关闭
if(ret==QMessageBox::Yes)
{
this->close();//关闭界面
}
}
void Widget::on_btn4_clicked()
{
emit jump();
}