鼠标事件
鼠标进入事件 enterEvent
鼠标离开事件leaveEvent
鼠标按下事件 mousePressEvent
鼠标释放 mouseReleaseEvent
鼠标移动 mouseMoveEvent
ev->x() ,ev->y() 坐标
ev-> button() 可以判断左右键,Qt::LeftButton,Qt::RightButton
ev->buttons() 判断组合按键,判断move
格式化字符串
QString("%1 %2").arg(111).arg(222);
//设置鼠标追踪
setMouseTracking(true);
定时器事件
利用事件void timerEvent(QTimerEvent *ev)
启动定时器 startTimer(1000) 毫秒单位
timerEvent的返回值是唯一标识
利用类使用定时器
头文件:#include<QTimer>
创建定时器对象QTimer *timer = new QTimer(this)
启动定时器 timer->start(毫秒)
每隔一定毫秒,发送信号,timeout
暂停 timer ->stop()
绘图事件
事件:void paintEvent()
声明一个画家对象:QPainter painter(this) this指定绘图设备
设置画笔 QPen
设置画刷QBrush
绘图高级设置
开启抗锯齿能力:效率会降低
painter.drawEllipse(QPointer::Antialiasing)
画家移动位置
painter.translate(位置)
利用画家来绘制资源图片
painter.drawpixmap(x,y,QPixmap("路径"))
手动调用绘图事件,需要用update更新。
对文件进行操作
文件:#include<QFileDialog>
读文件:#include<QFile>
QFile file(path 文件路径)
file.open(打开方式)
全部读取 file.readAll()
按照行读 file.readline() atend()判断是否读到文件尾
QFile默认支持的格式为UTF-8
编码格式:#include<QTextCodec>
QTextCodec *codec = QTextCodec::codecForName("gbk")
ui->textEdit->setText(codec->toUnicode(aray));
不要忘记对文件进行关闭file.close();
文件信息类
头文件#include<QFileInfo>
QFileInfo info(路径)
大小 info.size()
后缀名 info.suffix()
文件路径 info.filePath()