聊天框完善
#include "mywidget.h"
#include "ui_mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent), ui(new Ui::MyWidget)
{ui->setupUi(this);//设置窗口大小this->resize(400,560);//设置窗口图标和标题this->setWindowTitle("QQ");this->setWindowIcon(QIcon(":/pictrue/qq.png"));//设置固定窗口大小this->setFixedSize(400,560);//设置窗口去头部this->setWindowFlag(Qt::FramelessWindowHint);//设置窗口的背景lab1 = new QLabel(this);//设置文本大小lab1->resize(400,560);//设置动图QMovie *mv = new QMovie(":/pictrue/10.gif");//设置lab1自适应 + 动图放入lab1lab1->setMovie(mv);//设置动图运动mv->start();lab1->setScaledContents(true);//设置头像框 152*82lab2 = new QLabel(this);//设置头像框大小和位置lab2->resize(100,100);lab2->move(152,82);//设置头像框为圆形lab2->setStyleSheet("border-radius:50%");//设置头像自适应lab2->setScaledContents(true);//设置头像框头像//lab2->setPixmap(QPixmap(":/pictrue/15.gif"));QMovie *mv2 = new QMovie(":/pictrue/15.gif");lab2->setMovie(mv2);mv2->start();//设置账号文本line1 = new QLineEdit(this);//设置账号文本位置和大小 41*210 310*60line1->resize(310,50);line1->move(41,210);//设置文本背景色,边角圆弧line1->setStyleSheet("background-color:white;border-radius:10px;font:bold 20px");//设置文本提示(占位)line1->setPlaceholderText("输入QQ号");//设置密码文本line2 = new QLineEdit(this);//设置密码文本位置大小line2->resize(310,50);line2->move(41,275);//设置文本背景色和边角圆弧 41*402line2->setStyleSheet("background-color:white;border-radius:10px;font:bold 20px");//设置文本提示(占位)line2->setPlaceholderText("输入QQ密码");//设置为密码模式line2->setEchoMode(QLineEdit::Password);//设置按钮btn1 = new QPushButton("登录",this);//设置按钮位置和大小btn1->resize(310,50);btn1->move(41,400);//设置按钮不可用btn1->setEnabled(false);//设置按钮的颜色,背景色,字体大小,居中btn1->setStyleSheet("background-color:rgb(80,80,80);color:white;font:bold 20px;border-radius:10px;text-align:center");//设置关闭按钮btn2 = new QPushButton("X",this);//设置关闭按钮位置和大小btn2->resize(20,20);btn2->move(380,0);//实例化一个checkbox按钮che1 = new QCheckBox("我已阅读并同意服务协议和QQ隐私保护指引",this);che1->move(38,350);che1->resize(320,15);che1->setStyleSheet("color:white");//设置信号与槽connect(this->btn1,&QPushButton::clicked,this,&MyWidget::btn1_clicked);connect(btn2,&QPushButton::clicked,this,&MyWidget::btn2_clicked);connect(this->line1,&QLineEdit::textChanged,this,&MyWidget::btn1_perception1);connect(this->line2,&QLineEdit::textChanged,this,&MyWidget::btn1_perception2);
}MyWidget::~MyWidget()
{delete ui;
}void MyWidget::btn1_clicked(){if(this->line1->text() == this->line2->text()){QMessageBox box(QMessageBox::Information,"提示", "登陆成功",QMessageBox::Ok,this);int res = box.exec();if(res==QMessageBox::Ok){this->close();emit main_sign();}}else{QMessageBox box(QMessageBox::Warning,"警告","密码错误",QMessageBox::Ok|QMessageBox::Help,this);int res = box.exec();if(res==QMessageBox::Ok){line2->clear();}else if(res==QMessageBox::Help){line2->clear();QDesktopServices::openUrl(QUrl("https://accounts.qq.com/find/password?from=11"));}}
}void MyWidget::btn2_clicked(){int res=QMessageBox::question(this,"提示","你确定要关闭QQ吗",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);if(res==QMessageBox::Yes){this->close();}
}void MyWidget::btn1_perception1(){if(this->line1->text().length()<5 || this->line2->text().length()<5){btn1->setStyleSheet("background-color:rgb(80,80,80);color:white;font:bold 20px;border-radius:10px;text-align:center");btn1->setEnabled(false);}else{btn1->setStyleSheet("background-color:rgb(0,141,235);color:white;font:bold 20px;border-radius:10px;text-align:center");btn1->setEnabled(true);}
}
void MyWidget::btn1_perception2(){if(this->line2->text().length()<5 || this->line1->text().length()<5){btn1->setStyleSheet("background-color:rgb(80,80,80);color:white;font:bold 20px;border-radius:10px;text-align:center");btn1->setEnabled(false);}else{btn1->setStyleSheet("background-color:rgb(0,141,235);color:white;font:bold 20px;border-radius:10px;text-align:center");btn1->setEnabled(true);}
}
对话框功能实现
#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;
}