QT 第三天

一.实现组件的上下左右移动

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QKeyEvent>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void keyPressEvent(QKeyEvent *event) override;

    void keyReleaseEvent(QKeyEvent *event) override;

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#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::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "键盘被按下了" << event->text() <<"键值为:" << event->key();
    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()-10);
        break;
    }
    case 'S':  //向下移动
    {
        if(ui->label->y() >= this->height())
        {
            ui->label->move(ui->label->x(),0-ui->label->height());
        }
        ui->label->move(ui->label->x(),ui->label->y()+10);
        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()-10,ui->label->y());
        break;
    }
    case 'D':  //向右移动
    {
        if(ui->label->x() >= this->width())
        {
            ui->label->move(0-ui->label->width(),ui->label->y());
        }
        ui->label->move(ui->label->x()+10,ui->label->y());
        break;
    }
    }
}
//键盘抬起事件处理函数的定义
void Widget::keyReleaseEvent(QKeyEvent *event)
{

}

二.优化登录界面

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include <QLineEdit>
#include <QLabel>
#include <QString>
#include <QMessageBox>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

signals:
    void my_signal();  //自定义信号函数

public slots:
    void my_slot();    //自定义的登录槽函数
    void on_btn1_clicked();
    void on_btn3_clicked();
signals:
    void jump();  //自定义跳转函数



public:
    Widget(QWidget *parent = nullptr);
    ~Widget();



private:
    Ui::Widget *ui;

    QLabel *lab1;
    QLabel *lab2;
    QLineEdit *edit2;
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QLabel *lab3;
    QLineEdit *edit1;
};
#endif // WIDGET_H

widget.cpp

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

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

    //更改窗口标题
    this->setWindowTitle("MyQQ");
    //更改窗口图标
    this->setWindowIcon(QIcon(":/new/prefix1/icon/qie.png"));

    //更改logo图,在当前路径下
    this->setFixedSize(700,800);
    //1.实例化一个标签,设置上面界面
    lab1 = new QLabel(this);//设置父组件
    lab1 -> resize(700,300);//设置大小
    lab1->setPixmap(QPixmap(":/new/prefix1/icon/wangzhe.jpg"));//设置内容为图片
    lab1->setScaledContents(true);//设置内容自适应

    //2.实例化一个标签,设置账号
    lab2 = new QLabel(this);//设置父组件
    lab2 -> move(150,400);//设置位置
    lab2->setPixmap(QPixmap(":/new/prefix1/icon/denglu.png"));//设置内容为图片


    //3.实例化一个标签,设置密码
    lab3 = new QLabel(this);//设置父组件
    lab3 -> move(150,lab2->y()+50);//设置位置
    lab3->setPixmap(QPixmap(":/new/prefix1/icon/mima.png"));//设置内容为图片

    //1.构造一个行编辑器,构造时给定父组件
    edit1 = new QLineEdit(this);
    edit1->setPlaceholderText("QQ/手机/邮箱");   //设置编辑器的占位文本
    edit1->resize(300,40);      //设置尺寸
    edit1->move(lab1->x()+250,400);//移动位置
    //edit1->setEnabled(false);   //设置不可用状态

    //2.构造一个行编辑器,构造时给定父组件以及文本内容
    edit2 = new QLineEdit("",this);
    edit2->setPlaceholderText("密码");   //设置编辑器的占位文本
    qDebug() << edit2->text();    //获取行文本编辑器中文本内容
    edit2->resize(edit1->size());
    edit2->move(lab1->x()+250,edit1->y()+50);
    edit2->setEchoMode(QLineEdit::Password);   //设置回显模式

    //1.构造一个按钮时,指定父组件,登录
    btn1 = new QPushButton(this);  //将当前界面设置成父组件
    btn1->setText("微信登录");
    btn1->resize(btn1->size());
    btn1->move(edit2->x(),edit2->x()+300);
    btn1->setIcon(QIcon(":/new/prefix1/icon/w.png"));


    //2.构造一个按钮时,指定父组件,登录
    btn2 = new QPushButton(this);  //将当前界面设置成父组件
    btn2->setText("qq登录");
    btn2->resize(btn1->size());
    btn2->move(btn1->x()+100,edit2->x()+300);
    btn2->setIcon(QIcon(":/new/prefix1/icon/qie.png"));

    //2.构造一个按钮时,指定父组件,取消登录
    btn3 = new QPushButton(this);  //将当前界面设置成父组件
    btn3->setText("取消登录");
    btn3->resize(btn1->size());
    btn3->move(btn1->x()+200,edit2->x()+300);
    btn3->setIcon(QIcon(":/new/prefix1/icon/cuowu.png"));

    //QT5链接微信登录到自定义的槽函数中
    connect(this->btn1,&QPushButton::clicked,this,&Widget::my_slot);

    //QT4链接取消到自定义的槽函数中
    connect(btn3,SIGNAL(pressed()),this,SLOT(on_btn3_clicked()));


}

Widget::~Widget()
{
    delete ui;
}
void Widget::my_slot()
{
    //获取文本内容
    QString username = edit1->text();
    QString pwd = edit2->text();
    if(username == "admin" && pwd == "123456")
    {
        qDebug()<< "登陆成功";

        //1.使用QMessagebox实例化一个对象
        QMessageBox box(QMessageBox::Information,     //图标
                        "信息对话框",                        //对话框标题
                        "登陆成功",                   //对话框提示信息
                        QMessageBox::Ok|QMessageBox::Cancel, //对话框提供的按钮
                        this);                        //父组件
        box.setDefaultButton(QMessageBox::Cancel);        //将NO设置成默认按钮

        //2.执行对话框
        int ret = box.exec();

        //3.对用户点击的按钮进行判断
        if(ret == QMessageBox::Ok)
        {
            emit jump();
            this->close();
        }

    }
    else
    {
        qDebug()<< "登陆失败";
        int ret = QMessageBox::critical(this,
                                        "错误对话框",
                                        "账号密码不匹配,是否重新登陆",
                                        QMessageBox::Ok|QMessageBox::Cancel,
                                        QMessageBox::Ok);
        //对用户点击的按钮进行判断
        if(ret == QMessageBox::Ok)
        {
            edit2->clear();
        }

    }
}
void Widget::on_btn1_clicked()
{

}
//取消按钮对应的槽函数,问题对话框
void Widget::on_btn3_clicked()
{
    //1.使用QMessagebox实例化一个对象
    QMessageBox box(QMessageBox::Question,     //图标
                    "问题对话框",                        //对话框标题
                    "是否确定要退出登录",                   //对话框提示信息
                    QMessageBox::Yes|QMessageBox::No, //对话框提供的按钮
                    this);                        //父组件
    box.setDefaultButton(QMessageBox::No);        //将NO设置成默认按钮

    //2.执行对话框
    int ret1 = box.exec();

    //3.对用户点击的按钮进行判断
    if(ret1 == QMessageBox::Yes)
    {
        this->close();
    }

}

三.对话框

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QFont>   //字体类
#include <QFontDialog>  //字体对话框类
#include <QDebug>
#include <QMessageBox>
#include <QColor>       //颜色类
#include <QColorDialog> //颜色对话框类
#include <QFileDialog>   //文件对话框
#include <QFile>    //文件类

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_fontbtn_clicked();

    void on_colorbtn_clicked();

    void on_openbtn_clicked();

    void on_savebtn_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#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_fontbtn_clicked()
{
    //调用QFontDialog类中的静态成员函数,getFont函数来调取系统提供的字体对话框
    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_colorbtn_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_openbtn_clicked()
{
    //调用QFileDialog的静态成员函数getopenfilename来获取选中的文件的路径
    QString fileName = QFileDialog::getOpenFileName(this,        //父组件
                                                    "选择文件",   //对话框标题
                                                    "./",        //起始路径
                                                    "Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)");//过滤器
   //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","您取消了选择文件");
        return;
    }
    //输出文件路径
    qDebug() << fileName;


    //1.实例化一个文件对象
    QFile file(fileName);  //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象

    //2.判断文件是否存在
    if(!file.exists())
    {
        return;
    }

    //3.打开方式
    if(!file.open(QFile::ReadWrite))
    {
        return;
    }

    //4.读取文件中的内容
    QByteArray msg = file.readAll();

    //5.将内容展示到ui界面
    //ui->textEdit->setText(msg);
    ui->textEdit->setText(QString::fromLocal8Bit(msg));

    //获取文本编辑器中的内容  作业
    //ui->textEdit->toPlainText();

    //6.关闭文件
    file.close();
}
//保存文件按钮对应的槽函数,对文件操作的是文件对象
void Widget::on_savebtn_clicked()
{
    //调用Qfiledialog的静态成员函数getsavefilename来保存文本编译器中的文件
    QString fileName = QFileDialog::getSaveFileName(this,                    //父组件
                                                    "选择文件",               //对话框标题
                                                   "./",                     //起始路径
                                                   "Image File(*.png *.jpg *bmp);;Text File(*.txt);;All(*.*)" ); //过滤器
    //判断是否选中文件
    if(fileName.isNull())
    {
        QMessageBox::information(this,"提示","您取消了选择文件");
        return;
    }
    //1.实例化一个文件对象
    QFile file(fileName);  //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象


    //3.打开方式
    if(!file.open(QFile::ReadWrite))
    {
        return;
    }

    //获取文本编辑器中的内容  作业
    QString ret = ui->textEdit->toPlainText();

    //将获取到的内容写到实例化的文件对象里面
    file.write(ret.toLocal8Bit());

    //6.关闭文件
    file.close();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值