当前位置: 首页 > news >正文

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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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();
}

【运行结果】

http://www.lryc.cn/news/483132.html

相关文章:

  • nvm 切换 Node.js 版本
  • AI绘图最强软件stable diffusion,一文带你迅速了解!
  • VMware重磅官宣!Workstation和Fusion彻底全部免费:支持商用
  • CCS 学习记录
  • 241112.学习日志——[CSDIY] Cpp零基础速成 [01]
  • 94.【C语言】数据结构之双向链表的初始化,尾插,打印和尾删
  • learn-F12 Performance(性能)前端性能分析(LCP,CLS,INP)
  • XCZU47DR-2FSVE1156
  • 物联网低功耗广域网LoRa开发(一):LoRa物联网行业解决方案
  • 【LeetCode】【算法】23. 合并K个升序链表
  • python3的基本数据类型:Dictionary(字典)的创建
  • 【C++】string模拟实现
  • Springboot 使用EasyExcel导出含图片并设置样式的Excel文件
  • 技术分享:《越南语翻译通》App高效学习越南语的智能助手,是怎么实现高精度语音识别翻译功能的呢?
  • 工业互联网实验实训解决方案核心优势
  • Ceph client 写入osd 数据的两种方式librbd 和kernel rbd
  • 相机光学(四十二)——sony的HDR技术
  • 文件上传漏洞--理论
  • 快速入门Selenium自动化测试
  • C++指针使用指南
  • 一文学会,利用LLaMA 3.2打造能“识图断字”的个人AI助理
  • idea的mapper.xml文件里写sql语句出现Tag name expected错误提示
  • EasyExcel 使用多线程按顺序导出数据
  • 数据驱动的投资分析:民锋科技的量化模型探索
  • cesium 设置相机视角 flyTo 参数destination,orientation
  • vue+Leaflet.PM插件实现创建和编辑几何图形(点、线、面、圆等)
  • Rust语言在系统编程中的应用
  • test 是 JavaScript 中正则表达式对象 (RegExp) 的一种方法,用于测试一个字符串是否匹配某个正则表达式
  • 大厂社招3年-力扣热点高频刷题记录(已更新100+道热点题)
  • 6.2 对角化矩阵(2)