Qt 学习第十六天:文件和事件

一、创建widget对象(文件)

二、设计ui界面

放一个label标签上去,设置成box就可以显示边框了

三、新建Mylabel类

四、提升ui界面的label标签为Mylabel

五、修改mylabel.h,mylabel.cpp

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class Mylabel : public QLabel
{
    Q_OBJECT
public:
    explicit Mylabel(QWidget *parent = nullptr);

signals:

};

#endif // MYLABEL_H
#include "mylabel.h"

Mylabel::Mylabel(QWidget *parent)
    : QLabel{parent}
{}

六、实现鼠标进入事件,鼠标移动事件

// 鼠标进入事件函数
void Mylabel::enterEvent(QEnterEvent *e){
    qDebug() << "鼠标进入标签!";

}

// 鼠标移动事件
void Mylabel::mousePressEvent(QMouseEvent *e){
    if(e->button() == Qt::LeftButton)
    {
        QString s = QString("鼠标左键按下了! x = %1, y = %2").arg(e->x()).arg(e->y());
        qDebug() << s.toUtf8().data() ; //去掉双引号
    }
    if(e->button() == Qt::RightButton)
    {
        QString s = QString("鼠标右键按下了! x = %1, y = %2").arg(e->x()).arg(e->y());
        qDebug() << s.toUtf8().data() ; //去掉双引号
    }
}

七、widget中实现键盘按下按键事件

//键盘按下按键
void Widget::keyPressEvent(QKeyEvent *e){
    if(e->key() == Qt::Key_Any){
        qDebug() << "键盘空格键被按下!" ;
    }
    if(e->key() == Qt::Key_Enter){
        qDebug() << "键盘回车键被按下!" ;
    }
}

八、widget中实现事件分发器和事件过滤器

//事件分发器
bool Widget::event(QEvent *e){
    if(e->type() == QEvent::MouseButtonPress){
        qDebug() << "鼠标在窗口被按下!";
        return true; //返回true,代表不向下分发
    }
    //其他事件交给父类处理
    return QWidget::event(e);
}

//事件过滤器
bool Widget::eventFilter(QObject *obj, QEvent *e){
    if(obj == ui->label) //判断控件
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = static_cast<QMouseEvent *>(e);
            QString str = QString("事件过滤器中鼠标按下:x = %1, y = %2").arg(me->x()).arg(me->y());
            qDebug() << str.toUtf8().data();

            return true;
        }
    }
    //其他的交给父类处理
    return QWidget::eventFilter(obj, e);
}

完整代码

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>
#include <QEvent>
#include <QMouseEvent>

class Mylabel : public QLabel
{
    Q_OBJECT
public:
    explicit Mylabel(QWidget *parent = nullptr);

    // 鼠标进入事件函数
    void enterEvent(QEnterEvent *e);

    // 鼠标移动事件
    void mousePressEvent(QMouseEvent* e);

signals:

};

#endif // MYLABEL_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

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 *e);

    //事件分发器
    bool event(QEvent *e);

    //事件过滤器
    bool eventFilter(QObject *odj, QEvent *e);

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

mylabel.cpp

#include "mylabel.h"
#include <QDebug>

Mylabel::Mylabel(QWidget *parent)
    : QLabel{parent}
{}

// 鼠标进入事件函数
void Mylabel::enterEvent(QEnterEvent *e){
    qDebug() << "鼠标进入标签!";

}

// 鼠标移动事件
void Mylabel::mousePressEvent(QMouseEvent *e){
    if(e->button() == Qt::LeftButton)
    {
        QString s = QString("鼠标左键按下了! x = %1, y = %2").arg(e->x()).arg(e->y());
        qDebug() << s.toUtf8().data() ; //去掉双引号
    }
    if(e->button() == Qt::RightButton)
    {
        QString s = QString("鼠标右键按下了! x = %1, y = %2").arg(e->x()).arg(e->y());
        qDebug() << s.toUtf8().data() ; //去掉双引号
    }
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

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

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

//键盘按下按键
void Widget::keyPressEvent(QKeyEvent *e){
    if(e->key() == Qt::Key_Any){
        qDebug() << "键盘空格键被按下!" ;
    }
    if(e->key() == Qt::Key_Enter){
        qDebug() << "键盘回车键被按下!" ;
    }
}

//事件分发器
bool Widget::event(QEvent *e){
    if(e->type() == QEvent::MouseButtonPress){
        qDebug() << "鼠标在窗口被按下!";
        return true; //返回true,代表不向下分发
    }
    //其他事件交给父类处理
    return QWidget::event(e);
}

//事件过滤器
bool Widget::eventFilter(QObject *obj, QEvent *e){
    if(obj == ui->label) //判断控件
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = static_cast<QMouseEvent *>(e);
            QString str = QString("事件过滤器中鼠标按下:x = %1, y = %2").arg(me->x()).arg(me->y());
            qDebug() << str.toUtf8().data();

            return true;
        }
    }
    //其他的交给父类处理
    return QWidget::eventFilter(obj, e);
}

【运行结果】 

---------------------------------------------------------------------------------------------------------------------------------

一、创建mainwindow对象(事件)

二、设计ui界面

点一下水平布局即可填充全部

三、实现菜单栏

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>

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

    QAction *action1 = new QAction("打开");
    QAction *action2 = new QAction("保存");

    ui->menu->addAction(action1);
    ui->menu->addAction(action2);

    connect(action1, &QAction::triggered, this, &MainWindow::openFile);
    connect(action2, &QAction::triggered, this, &MainWindow::saveFile);
}

四、实现打开文件操作

void MainWindow::openFile(){
    //1. 先弹出 "打开文件" 对话框. 让用户选择打开哪个文件.
    QString path = QFileDialog::getOpenFileName(this);
    //2. 把文件名显示到状态栏里.
    QStatusBar *stbar = this->statusBar();
    stbar->showMessage(path);
    //3. 根据用户选择的路径, 构造一个 QFile 对象. 并打开文件
    QFile file(path);
    bool isOpen = file.open(QFile::ReadOnly);
    if(!isOpen){
        //打开文件失败!
        stbar->showMessage(path + "打开失败");
        return;
    }
    //4. 读取文件
    QString text = file.readAll();
    //读到的内容设置到输入框中.
    ui->plainTextEdit->setPlainText(text);
    //6. 关闭文件!! 千万不要忘记!!
    file.close();

}

五、实现保存文件操作

记得要开一个新的记事本,血的教训,错了不能反悔的

void MainWindow::saveFile(){
    //1. 先弹出 "保存文件" 对话框.
    QString path = QFileDialog::getOpenFileName(this);
    //2. 把文件名显示到状态栏里.
    QStatusBar *stbar = this->statusBar();
    stbar->showMessage(path);
    //3. 根据用户选择的路径, 构造一个 QFile 对象. 并打开文件
    QFile file(path);
    bool isOpen = file.open(QFile::WriteOnly);
    if(!isOpen){
        //打开文件失败!
        stbar->showMessage(path + "打开失败");
        return;
    }
    //4. 写文件
    const QString &text = ui->plainTextEdit->toPlainText();
    file.write(text.toUtf8());
    //5. 关闭文件.
    file.close();
}

完整代码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    void openFile();

    void saveFile();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>

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

    QAction *action1 = new QAction("打开");
    QAction *action2 = new QAction("保存");

    ui->menu->addAction(action1);
    ui->menu->addAction(action2);

    connect(action1, &QAction::triggered, this, &MainWindow::openFile);
    connect(action2, &QAction::triggered, this, &MainWindow::saveFile);
}

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

void MainWindow::openFile(){
    //1. 先弹出 "打开文件" 对话框. 让用户选择打开哪个文件.
    QString path = QFileDialog::getOpenFileName(this);
    //2. 把文件名显示到状态栏里.
    QStatusBar *stbar = this->statusBar();
    stbar->showMessage(path);
    //3. 根据用户选择的路径, 构造一个 QFile 对象. 并打开文件
    QFile file(path);
    bool isOpen = file.open(QFile::ReadOnly);
    if(!isOpen){
        //打开文件失败!
        stbar->showMessage(path + "打开失败");
        return;
    }
    //4. 读取文件
    QString text = file.readAll();
    //读到的内容设置到输入框中.
    ui->plainTextEdit->setPlainText(text);
    //6. 关闭文件!! 千万不要忘记!!
    file.close();

}

void MainWindow::saveFile(){
    //1. 先弹出 "保存文件" 对话框.
    QString path = QFileDialog::getOpenFileName(this);
    //2. 把文件名显示到状态栏里.
    QStatusBar *stbar = this->statusBar();
    stbar->showMessage(path);
    //3. 根据用户选择的路径, 构造一个 QFile 对象. 并打开文件
    QFile file(path);
    bool isOpen = file.open(QFile::WriteOnly);
    if(!isOpen){
        //打开文件失败!
        stbar->showMessage(path + "打开失败");
        return;
    }
    //4. 写文件
    const QString &text = ui->plainTextEdit->toPlainText();
    file.write(text.toUtf8());
    //5. 关闭文件.
    file.close();
}

【运行结果】