文本编辑器
#include "mywidget.h"
#include "ui_mywidget.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyWidget)
{
ui->setupUi(this);
//设置窗口大小
this->resize(700,600);
//实例化text文本窗口
text = new QTextEdit(this);
//设置文本窗口大小
text->resize(700,450);
//实例化按钮
btn1 = new QPushButton("字体",this);
btn2 = new QPushButton("颜色",this);
btn3 = new QPushButton("打开文件",this);
btn4 = new QPushButton("保存到文件",this);
//设置四个按钮的大小和位置
btn1->resize(100,50);
btn2->resize(100,50);
btn3->resize(100,50);
btn4->resize(100,50);
btn1->move(70,500);
btn2->move(220,500);
btn3->move(370,500);
btn4->move(520,500);
//设置信号与槽的连接
connect(btn1,&QPushButton::clicked,this,&MyWidget::btn1_clicked);
connect(btn2,&QPushButton::clicked,this,&MyWidget::btn2_clicked);
connect(btn3,&QPushButton::clicked,this,&MyWidget::btn3_clicked);
connect(btn4,&QPushButton::clicked,this,&MyWidget::btn4_clicked);
}
void MyWidget::btn1_clicked(){
bool ok;
QFont t = QFontDialog::getFont(&ok,QFont("楷体",10,10,true),this,"字体");
if(ok){
text->setFont(t);
}
}
void MyWidget::btn2_clicked(){
QColor c = QColorDialog::getColor(QColor("green"),this,"颜色");
if(c.isValid()==true){
text->setTextColor(c);
}
}
void MyWidget::btn3_clicked(){
QString file1 = QFileDialog::getOpenFileName(this,"文件","./","all(*.*)");
QFile file(file1);
if(file.open(QFile::ReadOnly)==false){
QMessageBox::information(this,"提示","打开文件失败");
return;
}
QByteArray msg = file.readAll();
text->setText(msg);
file.close();
}
void MyWidget::btn4_clicked(){
QString file1 = QFileDialog::getOpenFileName(this,"文件","./","all(*.*)");
QFile file(file1);
if(file.open(QFile::WriteOnly)==false){
QMessageBox::information(this,"提示","打开文件失败");
return;
}
QString msg = text->toPlainText();
file.write(msg.toUtf8().constData());
}
MyWidget::~MyWidget()
{
delete ui;
}
登录框
#include "mywnd.h"
#include "ui_mywnd.h"
#include<QMovie>
MyWnd::MyWnd(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyWnd)
{
ui->setupUi(this);
//对ui界面上拖拽的组件进行设置
//将logo标签设置进行自适应
ui->label->setScaledContents(true);
QMovie *moviePtr=new QMovie(":/pictrue/zz.gif");
ui->label->setMovie(moviePtr);
moviePtr->start(); //让动图动起来
ui->usr->setScaledContents(true);
ui->usr->setPixmap(QPixmap(":/pictrue/wodepeizhenshi.png"));
ui->pass->setScaledContents(true);
ui->pass->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->word->setEchoMode(QLineEdit::Password);
connect(ui->sign,&QPushButton::clicked,this,&MyWnd::sign_clicked);
connect(ui->name,&QLineEdit::textChanged,
this,&MyWnd::on_lineEdit_textChanged);
connect(ui->word,&QLineEdit::textChanged,
this,&MyWnd::on_lineEdit_textChanged);
}
MyWnd::~MyWnd()
{
delete ui;
}
void MyWnd::mySlots(void)
{
}
void MyWnd::sign_clicked()
{
if(ui->name->text()=="Admin"&&ui->word->text()=="123456")
{
qDebug() << "登录成功";
//1.实例化一个Qmessagebox类对象
QMessageBox box(QMessageBox::Information,
"成功对话框",
"登录成功",
QMessageBox::Ok,
this);
int res=box.exec();
if(res==QMessageBox::Ok)
{
//发射跳转信号
emit jump();
}
this->close();
}else
{
qDebug() << "登录失败,账号密码错误,请重新输入";
ui->name->clear();
ui->word->clear();
}
}
void MyWnd::on_lineEdit_textChanged()
{
if(ui->name->text().length()>=5&&ui->word->text().length()>=5)
{
ui->sign->setStyleSheet("background-color:blue");
ui->sign->setEnabled(true);
}else if(ui->name->text().length()<5&&ui->word->text().length()<5)
{
ui->sign->setStyleSheet("background-color:red");
ui->sign->setEnabled(false);
}
}