优化登录框,点击登录按钮,如果账号和密码匹配,则弹出 信息对话框 给出提示信息“登录成功”,并给出一个 ok 按钮,当用户点击 ok 后,关闭当前界面,跳转到另一个界面;如果账号和密码不匹配,则弹出 错误对话框 给出提示信息“登录失败,是否重新登录”,并给出两个按钮,yes 和 no,当用户点击yes后,清空两个行编辑器中的文本内容,继续登录,当用户点击no按钮后,关闭整个登录界面
#include "mywidget.h"
#include "ui_mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent), ui(new Ui::MyWidget)
{ui->setupUi(this);this->resize(540,415);//窗口大小this->setFixedSize(540,415);//固定窗口大小this->setWindowTitle("QQ");//标题this->setWindowIcon(QIcon("C:\\Users\\14123\\Desktop\\qt\\pictrue\\qq.png"));//图标this->setStyleSheet("background-color:white");//图标// this->setWindowFlag(Qt::FramelessWindowHint);//去头lab1 = new QLabel(this);//设置标签lab1->resize(540, 160);//标签大小QMovie *mv = new QMovie("C:\\Users\\14123\\Desktop\\qt\\pictrue\\zz.gif");//接收动图lab1->setMovie(mv);//将动图放入标签mv->start();//让动图动起来lab1->setScaledContents(true);//自动适应大小lab2 = new QLabel(this);//设置标签2lab2->resize(30,30);//标签大小lab2->move(120,210);//标签移动位置lab2->setPixmap(QPixmap("C:\\Users\\14123\\Desktop\\qt\\pictrue\\wodepeizhenshi.png"));//将图片放入标签lab2->setScaledContents(true);//自动适应大小lab3 = new QLabel(this);//设置标签3lab3->resize(30,30);lab3->move(120, 260);lab3->setPixmap(QPixmap("C:\\Users\\14123\\Desktop\\qt\\pictrue\\passwd.jpg"));lab3->setScaledContents(true);edit1 = new QLineEdit(this);//设置行编辑器edit1->resize(275,30);//行编辑器大小edit1->move(155,210);//移动行编辑器位置edit1->setPlaceholderText("QQ号/手机号/邮箱");//行编辑器占位edit2 = new QLineEdit(this);edit2->resize(275,30);edit2->move(155,260);edit2->setPlaceholderText("密码");//行编辑器占位edit2->setEchoMode(QLineEdit::Password);//行编辑器密码模式显示btn = new QPushButton("登录",this);//按钮组件btn->resize(300,45);//按钮组件大小btn->move(120,345);//按钮组件移动位置//按钮背景色,边框倒角,字体颜色btn->setStyleSheet("background-color:red;border-radius:5px;color:white");btn->setEnabled(false);connect(this->edit1,&QLineEdit::textChanged,this,&MyWidget::edit_Slots);connect(this->edit2,&QLineEdit::textChanged,this,&MyWidget::edit_Slots);connect(this->btn,&QPushButton::clicked,this,&MyWidget::btn_Slots);}void MyWidget::btn_Slots()
{if(this->edit1->text()=="admin" && this->edit2->text()=="123456"){qDebug() << "登录成功";QMessageBox box(QMessageBox::Information,"信息对话框","登陆成功",QMessageBox::Ok,this);int res = box.exec();if(res==QMessageBox::Ok){emit jump();this->close();}}else{int res = QMessageBox::critical(this,"错误对话框","登录失败,账号或密码错误,是否重新登陆",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);if(res==QMessageBox::Yes){this->edit1->clear();this->edit2->clear();}else if(res==QMessageBox::No){this->close();}//qDebug() << "登录失败,账号或密码错误";}
}
void MyWidget::edit_Slots()
{QString s1=this->edit1->text();QString s2=this->edit2->text();if(s1.length()>=5 && s2.length()>=6 ){this->btn->setStyleSheet("background-color:rgb(8,189,253)");this->btn->setEnabled(true);}else if(s1.length()<5 || s2.length()<6 ){this->btn->setStyleSheet("background-color:red");this->btn->setEnabled(false);}}MyWidget::~MyWidget()
{delete ui;
}
文本编辑器实现完整
#include "mywidget.h"
#include "ui_mywidget.h"Mywidget::Mywidget(QWidget *parent): QWidget(parent), ui(new Ui::Mywidget)
{ui->setupUi(this);
}Mywidget::~Mywidget()
{delete ui;
}void Mywidget::on_fontbtn_clicked()
{//调用函数选择字体bool ok = false;QFont f = QFontDialog::getFont(&ok,QFont("宋体",10,10,true),this,"选择字体");//使用选择的字体if(ok){ui->textEdit->setCurrentFont(f);}}void Mywidget::on_colorbtn_clicked()
{//调用函数选择颜色QColor c = QColorDialog::getColor(QColor("red"),this,"选择颜色");if(c.isValid() == true){//使用颜色ui->textEdit->setTextColor(c); //更改选中的文字颜色//设置选中的背景颜色// ui->textEdit->setTextBackgroundColor(c);}}void Mywidget::on_openfirebtn_clicked()
{// 调用静态成员函数,让用户选择打开的文件QString filename = QFileDialog::getOpenFileName(this, //父组类"选择文件","./", //起始路径"all(*.*)::image(*.jpg*.gif)::源文件(*.cpp)" //过滤器);qDebug() << filename;//文件操作//1实例化一个文本对象QFile file(filename);//2,打开文件if(file.open(QFile::ReadOnly) == false){QMessageBox::information(this,"提示","文件打开失败");return ;}//3 读取文件内容QByteArray msg = file.readAll();//4将内容放到编辑器中ui->textEdit->setText(msg);//5关闭文件file.close();}void Mywidget::on_cunbtn_clicked()
{QString filename = QFileDialog::getSaveFileName(this,"保存文件","./","all(*.*)");//文件操作QFile file(filename);QByteArray msg = ui->textEdit->toPlainText().toLatin1();if(file.open(QFile::WriteOnly) == false){QMessageBox::information(this,"提示","文件打开失败");return ;}file.write(msg);file.close();}