Keyboard event override

Implement file writing and saving

Source File

#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()
{

//函数原型: 
        //[static] QFont QFontDialog::getFont(bool *ok, const QFont &initial, QWidget *parent = nullptr, const QString &title = QString(), QFontDialog::FontDialogOptions options = FontDialogOptions())

//    bool ok;
//    QFont font = QFontDialog::getFont(
//                    &ok, QFont("Helvetica [Cronyx]", 10), this);
//    if (ok) {
//        // the user clicked OK and font is set to the font the user selected
//    } else {
//        // the user canceled the dialog; font is set to the initial
//        // value, in this case Helvetica [Cronyx], 10
//    }
    //调用QFontDialog类中的静态成员函数,getFont函数来调取系统提供的字体对话框
    bool ok;          //用来接收用户是否选择了字体,ok选择返回选中的,若没ok则返回默认的
    QFont font = QFontDialog::getFont(&ok,    //返回是否选择字体
                                      QFont("幼圆", 17, 10, false),   //第二个参数初始字体:的有参构造函数QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
                                      this,         //父组件
                                      "字体选择");  //对话框标题
    //将选择的字体进行使用
    if(ok)
    {
        //选择了字体,将字体设置到文本上
        //ui->textEdit->setFont(font);
        //将选中的内容进行设置为选择的字体
        ui->textEdit->setCurrentFont(font);
    }
    else
    {
        //未选中字体
        QMessageBox::information(this, "提示", "取消了字体选择");
    }
}

//颜色按钮对应的槽函数
void Widget::on_colorBtn_clicked()
{
    //函数原型:
    //[static] QColor QColorDialog::getColor(const QColor &initial = Qt::white, QWidget *parent = nullptr, const QString &title = QString(), QColorDialog::ColorDialogOptions options = ColorDialogOptions())
    //调用静态成员函数,getcolor函数来调取系统提供的颜色对话框
    QColor color = QColorDialog::getColor(QColor("pink"),        //初始颜色的有参构造函数:inline QColor(const QString& name);
               this, "颜色选择");
    //对选择的颜色判断合法性
    if(color.isValid())
    {
        //颜色合法,直接使用即可
        //设置选中的字体颜色
        //ui->textEdit->setTextColor(color);

        //设置选择的字体的背景颜色
        ui->textEdit->setTextBackgroundColor(color);

    }
    else
    {
        //颜色不合格
        QMessageBox mesBox(QMessageBox::Information, "提示", "您已取消了颜色选择", QMessageBox::Ok, this);
        //调用exec()函数进入执行态
        mesBox.exec();

    }
}

void Widget::on_openBtn_clicked()
{
    //调用QFileDialog类中的静态成员函数, getOpenFileName来获取文件路径
    //函数原型:
   // QString
    //getOpenFileName(
    //QWidget *parent = nullptr,
    //const QString &caption = QString(),
    //const QString &dir = QString(),
    //const QString &filter = QString(),
    //QString *selectedFilter = nullptr,
    //QFileDialog::Options options = Options())

    //fileName = QFileDialog::getOpenFileName(this,
          //tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

    //调用QFileDialog静态成员函数, getOpenFileName来获取文件路径
    QString filename  =  QFileDialog::getOpenFileName(this,          //父组件
                                                      "文件选择",     //对话框标题
                                                      "./",         //起始路径
                                                      "Image File(*.png *.jpg *bmp);; Texe Tile(*.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();

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

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

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


void Widget::on_saveBtn_clicked()
{
    //调用QFileDialog静态成员函数, getOpenFileName来获取文件路径
    QString filename  =  QFileDialog::getSaveFileName(this,          //父组件
                                                      "文件选择",     //对话框标题
                                                      "fyjh",         //起始路径
                                                      "Image File(*.png *.jpg *bmp);; Texe Tile(*.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;
    }

   //获取文件编辑器
   QString buf = ui->textEdit->toPlainText();

    //将文件内容写入文件中
    //QByteArray bytes = buf.toLocal8Bit();   //Qstring类型转为C风格字符串
    file.write(buf.toLocal8Bit());

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

Implement keyboard event WSAD to move up, down, left and right

head File

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QKeyEvent>    //键盘事件类所在的头文件
#include <QtDebug>

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

Source File

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

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

    ui->lab1->resize(140, 140);
}

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

//键盘按下事件处理函数的定义
void Widget::keyPressEvent(QKeyEvent *event)
{
    qDebug() <<"键盘按下了" << event->text() << event->key();
    switch (event->key())
    {
        case 'W':
        {
            if(ui->lab1->y() <= 0-ui->lab1->height())
            {
                ui->lab1->move(ui->lab1->x(), this->height());
            }
            ui->lab1->move(ui->lab1->x(), ui->lab1->y()-5);
            break;
        }


        case 'S':
        {
            if(ui->lab1->y() >= this->height())
            {
                ui->lab1->move(ui->lab1->x(), 0 - ui->lab1->height());
            }
            ui->lab1->move(ui->lab1->x(), ui->lab1->y()+5);
            break;
        }

        case 'A':
        {
            if(ui->lab1->x() <= 0 - ui->lab1->width())
            {
                ui->lab1->move(this->width(),ui->lab1->y());
            }
            ui->lab1->move(ui->lab1->x()-5, ui->lab1->y());
            break;
        }

        case 'D':
        {
            if(ui->lab1->x() >= this->width())
            {
                ui->lab1->move(0 - ui->lab1->width(), ui->lab1->y());
            }
            ui->lab1->move(ui->lab1->x()+5, ui->lab1->y());
            break;
        }

    }

}

//键盘抬起事件处理函数的定义
void Widget::keyReleaseEvent(QKeyEvent *event)
{

}

Guess you like

Origin blog.csdn.net/qq_46766479/article/details/132631860