Qt图形视图框架(三) 自定义QGraphicsItem
来源:互联网 发布:淘宝店铺流量来源查询 编辑:程序博客网 时间:2024/05/18 19:42
自定义QGraphicsItem
目的:通过按空格或点击鼠标左键实现两张图片之间的切换
头文件:
#ifndef CHECKBOX_H#define CHECKBOX_H#include <QtWidgets>class CheckBox : public QGraphicsItem {private: int w, h; QPixmap a, b; bool is_checked;public: CheckBox(int, int, const QString &, const QString &); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);protected: void mousePressEvent(QGraphicsSceneMouseEvent *); void mouseReleaseEvent(QGraphicsSceneMouseEvent *); void keyPressEvent(QKeyEvent *);};#endif // CHECKBOX_H
其源文件:
#include "checkbox.h"#include <QDebug>CheckBox::CheckBox(int w, int h, const QString &a, const QString &b) : w(w), h(h), a(a), b(b), is_checked(true) {}QRectF CheckBox::boundingRect() const { return QRectF(0, 0, 50, 50);}void CheckBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); QPixmap temp; if (is_checked) { qDebug() << "a"; temp = a.scaled(50, 50, Qt::KeepAspectRatioByExpanding); } else { qDebug() << "b"; temp = b.scaled(50, 50, Qt::KeepAspectRatioByExpanding); } painter->drawPixmap(0, 0, temp);}void CheckBox::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { event->accept(); }}void CheckBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "mouse"; event->accept(); is_checked = !is_checked; update(); }}void CheckBox::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Space) { qDebug() << "key"; event->accept(); is_checked = !is_checked; update(); }}main:
#include <QtWidgets>#include <QApplication>#include "checkbox.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); QGraphicsScene *scene = new QGraphicsScene; QGraphicsView *view = new QGraphicsView; view->setScene(scene); CheckBox cb(50, 50, ":/image/a.jpg", ":/image/timg.jpg"); cb.setFlag(QGraphicsItem::ItemIsFocusable, true); cb.setFocus(); scene->addItem(&cb); view->show(); return app.exec();}
阅读全文
1 0
- Qt图形视图框架(三) 自定义QGraphicsItem
- 图形视图框架中自定义QGraphicsItem
- Qt图形框架资料07 -- Qt图形视图QGraphicsItem类
- Qt图形视图QGraphicsItem类
- QT图形视图框架
- QT图形视图框架
- Qt图形视图框架
- QT 图形视图框架
- QT图形视图框架
- Qt修炼手册7_图形:用户自定义QGraphicsItem
- (转)Qt图形视图框架(一)
- QT图形视图框架(待完成)
- Qt图形视图框架(一)
- Qt图形视图框架(一)
- Qt图形视图框架(一)
- qt之图形视图框架(上)
- qt之图形视图框架(下)
- Qt图形视图框架(五) 变换
- MyEclipse中创建maven web项目
- docker+svn+maven+tomcat一键部署项目
- HttpServeltResponse对象介绍,http://www.cnblogs.com/xdp-gacl/p/3789624.html
- Oracle 创建DBLink (转)
- XML
- Qt图形视图框架(三) 自定义QGraphicsItem
- 有限元 和 FEA
- u-boot中的text、bss、data和rodata区别和联系
- java List中对象多属性排序
- codeforces 41A(Translation) Java
- 产品新人必备的10本书
- iOS 在自定义tabbar上显示购物车的数量
- PHP学习建议
- linux符号表简介