#include "mywidget.h"
#include "second.h"
#include "textedit.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
Second s;
textedit t;
w.show();
// QObject::connect(&w,&MyWidget::jump,&s,&Second::jump_slots);
QObject::connect(&w,&MyWidget::jump,&t,&textedit::jump_slots);
return a.exec();
}
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QDebug>
#include<QIcon>
#include<QLabel>
#include<QMovie>
#include<QLineEdit>
#include<QPushButton>
#include<QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr);
~MyWidget();
signals:
void jump();
public slots:
void edit_Slots();
void btn_Slots();
private:
Ui::MyWidget *ui;
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
QLineEdit *edit1;
QLineEdit *edit2;
QPushButton *btn;
};
#endif // MYWIDGET_H
#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("E:\\hqyjapp\\QT\\pictrue\\pictrue\\qq.png"));//图标
this->setStyleSheet("background-color:white");//图标
// this->setWindowFlag(Qt::FramelessWindowHint);//去头
lab1 = new QLabel(this);//设置标签
lab1->resize(540, 160);//标签大小
QMovie *mv = new QMovie("E:\\hqyjapp\\QT\\pictrue\\pictrue\\qq2.gif");//接收动图
lab1->setMovie(mv);//将动图放入标签
mv->start();//让动图动起来
lab1->setScaledContents(true);//自动适应大小
lab2 = new QLabel(this);//设置标签2
lab2->resize(30,30);//标签大小
lab2->move(120,210);//标签移动位置
lab2->setPixmap(QPixmap("E:\\hqyjapp\\QT\\pictrue\\pictrue\\wodepeizhenshi.png"));//将图片放入标签
lab2->setScaledContents(true);//自动适应大小
lab3 = new QLabel(this);//设置标签3
lab3->resize(30,30);
lab3->move(120, 260);
lab3->setPixmap(QPixmap("E:\\hqyjapp\\QT\\pictrue\\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;
}
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QWidget>
#include <QDebug>
#include <QMessageBox>
#include <QFont>
#include <QFontDialog>
#include <QColorDialog>
#include <QColor>
#include <QFileDialog>
#include <QFile>
#include <QTextEdit>
#include <QPushButton>
namespace Ui {
class textedit;
}
class textedit : public QWidget
{
Q_OBJECT
public:
explicit textedit(QWidget *parent = nullptr);
~textedit();
private:
QTextEdit *text;
QPushButton *fontbtn;
QPushButton *colorbtn;
QPushButton *openfilebtn;
QPushButton *savefilebtn;
public slots:
void jump_slots();
void fontbtn_clicked();
void colorbtn_clicked();
void openfilebtn_clicked();
void savefilebtn_clicked();
};
#endif // TEXTEDIT_H
#include "textedit.h"
#include "ui_textedit.h"
textedit::textedit(QWidget *parent) :
QWidget(parent)
{
this->resize(600,600);
text=new QTextEdit(this);
text->resize(600,400);
fontbtn=new QPushButton("字体",this);
fontbtn->move(100,500);
fontbtn->resize(60,60);
colorbtn=new QPushButton("颜色",this);
colorbtn->move(200,500);
colorbtn->resize(60,60);
openfilebtn=new QPushButton("打开文件",this);
openfilebtn->move(300,500);
openfilebtn->resize(60,60);
openfilebtn->setStyleSheet("font-size:12px");
savefilebtn=new QPushButton("保存文件",this);
savefilebtn->move(400,500);
savefilebtn->resize(60,60);
savefilebtn->setStyleSheet("font-size:12px");
connect(this->fontbtn,&QPushButton::clicked,this,&textedit::fontbtn_clicked);
connect(this->colorbtn,&QPushButton::clicked,this,&textedit::colorbtn_clicked);
connect(this->openfilebtn,&QPushButton::clicked,this,&textedit::openfilebtn_clicked);
connect(this->savefilebtn,&QPushButton::clicked,this,&textedit::savefilebtn_clicked);
}
textedit::~textedit()
{
}
void textedit::jump_slots()
{
this->show();
}
void textedit::fontbtn_clicked()
{
bool ok=false;
QFont f=QFontDialog::getFont(&ok,
QFont("宋体",10,10,true),
this,
"选择字体"
);
if(ok)
{
// this->text->setFont(f);
this->text->setCurrentFont(f);
}
}
void textedit::colorbtn_clicked()
{
QColor c=QColorDialog::getColor(
QColor("white"),
this,
"选择颜色");
if(c.isValid()==true)
{
//this->text->setTextColor(c);
this->text->setTextBackgroundColor(c);
}
}
void textedit::openfilebtn_clicked()
{
QString filename=QFileDialog::getOpenFileName(this,
"选择文件",
"./",
"all(*.*);;images(*.png *.jpg *.gif),源文件(*.cpp)");
qDebug() << filename;
QFile file(filename);
if(file.open(QFile::ReadOnly)==false)
{
QMessageBox::information(this,"提示","文件打开失败");
return;
}
QByteArray msg=file.readAll();
this->text->setText(msg);
file.close();
}
void textedit::savefilebtn_clicked()
{
QString filename=QFileDialog::getSaveFileName(this,
"选择文件",
"./",
"all(*.*);;images(*.png *.jpg *.gif),源文件(*.cpp)");
QByteArray msg=this->text->toPlainText().toUtf8();
QFile file(filename);
if(file.open(QFile::WriteOnly)==false)
{
QMessageBox::information(this,"提示","文件打开失败");
return;
}
file.write(msg);
file.close();
}