子类化QWidget iconEditor实现<三>
来源:互联网 发布:数据段字节变量 编辑:程序博客网 时间:2024/06/07 01:33
接上一章<三>
这里主要讲述鼠标左键和右键产生相应像素点变黑或者变白的效果即用来设置或者清空一个像素.主要一个是单击和移动时产生的效果
void iconeditor::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
setImagePixel(event->pos(), true);
} else if(event->button() == Qt::RightButton) {
setImagePixel(event->pos(), false);
}
}
void iconeditor::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton) {
setImagePixel(event->pos(),true);
} else if(event->buttons() & Qt::RightButton) {
setImagePixel(event->pos(), false);
}
}
在mousePressEvent()中首先用QMouseEvent创建一个event对象,event->button()返回鼠标按钮事件,通常在鼠标没有按下或者移动时返回的是:Qt::NoButton ,这里我们列出所有的鼠标事件:
This enum type describes the different mouse buttons.
void iconeditor::setImagePixel(const QPoint &pos, bool opaque)
{
int i = pos.x() / zoom;
int j = pos.y() / zoom;
if(image.rect().contains(i, j)) {
if(opaque) {
image.setPixel(i, j, penColor().rgba());
} else {
image.setPixel(i, j, qRgba(0, 0, 0, 0));
}
update(pixelRect(i, j));
}
}
const QPoint &pos这里使用引用传参,引用传参是C++里面用的非常多的传参方式,QT也不例外.
使用pos.x() .y()来返回光标的x,y坐标,再除上缩放因子得到图像坐标。rect中构造一个方形的范围,用contains来判断是否在这个构造的范围附近,如果是则返回true.
最好判断opaque来确定是否需要设置像素点。
- 子类化QWidget iconEditor实现<三>
- 子类化QWidget IconEditor实现
- 子类化QWidget IconEditor实现<二>
- class IconEditor : public QWidget
- 钩子实现子类化
- Qwidget实现文本拖放
- 窗口子类化三:SubClassWindow详解
- 学习重要的几个QWidget和QFrame子类
- 继承自QWidget的子类如何直接使用QSS样式
- 父类中调用子类构造函数实例化子类的实现
- 父类中调用子类构造函数实例化子类的实现
- 父类中调用子类构造函数实例化子类的实现
- Qwidget实现list互相拖动
- QWidget
- 漫谈QWidget及其派生类(三)
- QWidget QMainWindow QDialog 三者区别
- 漫谈QWidget及其派生类(三)
- 漫谈QWidget及其派生类(三)
- dispatch_once函数创建单例
- 值得开发者关注的8个HTML5 API
- iOS编译ffmpeg
- ios下的蓝牙4.0
- 学电脑,就得知道电脑每个键的用处,赶快保存起来
- 子类化QWidget iconEditor实现<三>
- C++ 按关键字搜索文件夹中的文件 & 全盘符搜索文件
- WCF中通过配置文件来创建启动服务
- linux tar 加压、压缩命令详解
- Linux常用命令功能集
- Oracle分页查询中排序与效率问题
- 多继承时的构造函数
- 第十四周项目三:CSolid类
- 软件开发中的“宫心斗”