QTday3

作业

在之前的登陆界面基础上加上消息对话框

#include "hqyj.h"

HQYJ::HQYJ(QWidget *parent)
    : QWidget(parent)
{
    //主界面设置
    this->resize(540,410);
    this->setFixedSize(540,410);
    this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
    this->setWindowTitle("阿瑞乱聊");
    //主页标签
    this->lab1 = new QLabel(this);//账号
    this->lab2 = new QLabel(this);//密码
    this->lab3 = new QLabel(this);//大图
    this->lab1->resize(50,50);
    this->lab1->move(100,200);
    this->lab1->setPixmap(QPixmap(":/icon/userName.jpg"));
    this->lab1->setScaledContents(true);
    this->lab2->resize(this->lab1->size());
    this->lab2->move(this->lab1->x(),this->lab1->y()+70);
    this->lab2->setPixmap(QPixmap(":/icon/passwd.jpg"));
    this->lab2->setScaledContents(true);
    this->lab3->resize(520,190);
    this->lab3->move(10,0);
    this->lab3->setPixmap(QPixmap(":/icon/logo.png"));
    this->lab3->setScaledContents(true);
    //主页行编辑器
    this->edit1 = new QLineEdit(this);
    this->edit2 = new QLineEdit(this);
    this->edit1->setPlaceholderText("QQ号/手机/邮箱");
    this->edit2->setPlaceholderText("密码");
    this->edit1->resize(300,50);
    this->edit2->resize(this->edit1->size());
    this->edit1->move(this->lab1->x()+60,this->lab1->y());
    this->edit2->move(this->lab2->x()+60,this->lab2->y());
    this->edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    //主页按键
    this->bnt1 = new QPushButton(QIcon(":/icon/login.png"),"登录",this);
    this->bnt2 = new QPushButton(QIcon(":/icon/cancel.png"),"取消",this);
    this->bnt1->resize(70,50);
    this->bnt2->resize(this->bnt1->size());
    this->bnt1->move(this->edit2->x()+40,this->edit2->y()+70);
    this->bnt2->move(this->bnt1->x()+90,this->bnt1->y());
    //按钮链接
    connect(this->bnt2,&QPushButton::clicked,this,&HQYJ::slot_cancel);
    connect(this->bnt1,&QPushButton::clicked,this,&HQYJ::my_slot);
}

HQYJ::~HQYJ()
{
}
void HQYJ::my_slot()
{
    QString userName = this->edit1->text();
    QString pwd = this->edit2->text();
    if(userName=="admin" && pwd=="123456")
    {
        QMessageBox box(QMessageBox::Information,
                        "信息",
                        "登录成功",
                        QMessageBox::Ok,
                        this);
        int ret = box.exec();
        if(ret == QMessageBox::Ok)
        {
            emit(HQYJ::my_signal());
            this->close();
        }
    }else
    {
        int ret = QMessageBox::warning(this,
                                       "错误",
                                       "账号密码不匹配",
                                       QMessageBox::Ok|QMessageBox::Cancel,
                                       QMessageBox::Ok);
        if(ret == QMessageBox::Ok)
        {
            this->edit2->clear();
        }
        else
        {
            this->close();
        }
    }
}

void HQYJ::slot_cancel()
{
    int ret = QMessageBox::warning(this,
                                   "问题",
                                   "是否要退出登录",
                                   QMessageBox::Yes|QMessageBox::No,
                                   QMessageBox::Yes);
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
}

文件编辑器

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_textbnt_clicked()
{
    bool ok;
    QFont f = QFontDialog::getFont(&ok,
                                   QFont("隶书",10,10,false),
                                   this,
                                   "选择字体");
    if(ok)
    {
        //ui->textEdit->setFont(f);
        ui->textEdit->setCurrentFont(f);
    }else
    {
        QMessageBox::information(this,"提示","您取消了选择字体");
    }
}

void Widget::on_colorbnt_clicked()
{
    QColor c = QColorDialog::getColor(QColor("pink"),
                                      this,
                                      "选择颜色");
    if(c.isValid())
    {
        ui->textEdit->setTextColor(c);
        //ui->textEdit->setTextBackgroundColor(c);
    }else
    {
        QMessageBox::information(this,"提示","您取消了选择颜色");
    }
}

void Widget::on_openbnt_clicked()
{
    QString s = QFileDialog::getOpenFileName(this,
                                             "选择文件",
                                             "./",
                                             "image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");
    if(s.isNull())
    {
        QMessageBox::information(this,"提示","您取消了选择文件");
        return;
    }

    QFile fd(s);

    if(!fd.exists())
    {
        return;
    }

    if(!fd.open(QFile::ReadWrite))
    {
        return;
    }

    QByteArray msg = fd.readAll();

    ui->textEdit->setText(QString::fromLocal8Bit(msg));

    fd.close();
}

void Widget::on_savebnt_clicked()
{
    QString s = QFileDialog::getSaveFileName(this,
                                             "选择文件",
                                             "./",
                                             "Text File(*.txt);;All(*.*)");
    if(s.isNull())
    {
        QMessageBox::information(this,"提示","您取消了保存文件");
        return;
    }

    QFile fd(s);

    if(!fd.open(QFile::WriteOnly))
    {
        return;
    }

    QString m = ui->textEdit->toPlainText();
    fd.write(m.toLocal8Bit());

    fd.close();
}

用WSAD控制组件移动

void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case 'W':
    {
        if(ui->label->y() <= 0-ui->label->height())
        {
            ui->label->move(ui->label->x(),this->height());
        }
        ui->label->move(ui->label->x(),ui->label->y()-20);
    }
        break;
    case 'A':
    {
        if(ui->label->x() <= 0-ui->label->width())
        {
            ui->label->move(this->width(),ui->label->y());
        }
        ui->label->move(ui->label->x()-20,ui->label->y());
    }
        break;
    case 'D':
    {
        if(ui->label->x() >= this->width()+ui->label->width())
        {
            ui->label->move(0-ui->label->width(),ui->label->y());
        }
        ui->label->move(ui->label->x()+20,ui->label->y());
    }
        break;
    case 'S':
    {
        if(ui->label->y() >= this->height()+ui->label->height())
        {
            ui->label->move(ui->label->x(),0-ui->label->height());
        }
        ui->label->move(ui->label->x(),ui->label->y()+20);
    }
        break;
    }
}

思维导图

QTday3_第1张图片

你可能感兴趣的:(笔记)