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

如何创建个人的网站郑州网络推广哪家口碑好

如何创建个人的网站,郑州网络推广哪家口碑好,做网站嘉兴,互联网营销专业自定义QGraphicsItem和QGraphicsView 说明示例myitem.hmyitem.cppmyview.hmyview.cpp调用main.cpp 效果 说明 在使用Qt的图形视图框架实现功能时,一般会在其基础上进行自定义功能实现。 如:滚轮对场景的缩放,鼠标拖动场景中的项,…

自定义QGraphicsItem和QGraphicsView

      • 说明
      • 示例
        • `myitem.h`
        • `myitem.cpp`
        • `myview.h`
        • `myview.cpp`
        • 调用
          • `main.cpp`
      • 效果

说明

在使用Qt的图形视图框架实现功能时,一般会在其基础上进行自定义功能实现。
如:滚轮对场景的缩放,鼠标拖动场景中的项,以及可以在场景中进行右键操作等。

示例

myitem为自定义QGraphicsItem,实现了边框、重绘事件、鼠标悬停、按键、右键菜单等功能。

myitem.h
#ifndef MYITEM_H
#define MYITEM_H#include <QGraphicsItem>class MyItem : public QGraphicsItem
{
public:MyItem();// 边框virtual QRectF boundingRect() const override;// 重绘事件virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;// 设置笔刷inline void setColor(const QColor &color) {brushColor = color;}protected:// 鼠标按下函数,设置被点击的图形项得到焦点,并改变光标外观virtual void keyPressEvent(QKeyEvent *event) override;// 键盘按下函数,判断是不是向下方向键,若是是,则向下移动图形项virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;// 悬停事件函数,设置光标外观和提示virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;// 右键菜单函数,为图形项添加一个右键菜单virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;private:QColor brushColor; // 笔刷颜色
};
#endif // MYITEM_H
myitem.cpp
#include "myitem.h"
#include <QPainter>
#include <QCursor>
#include <QKeyEvent>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>#define WIDTH 40
#define HEIGHT 40#define POS 20MyItem::MyItem()
{brushColor = Qt::black;setFlag(QGraphicsItem::ItemIsFocusable);setFlag(QGraphicsItem::ItemIsMovable);setAcceptHoverEvents(true);}QRectF MyItem::boundingRect() const
{qreal adjust = 0.5; // 返回上下左右+0.5个像素return QRectF(-POS - adjust, -POS - adjust,WIDTH + adjust, HEIGHT + adjust);
}void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget *)
{if (hasFocus()) {painter->setPen(QPen(QColor(255, 255, 255)));} else {painter->setPen(QPen(QColor(100, 100, 100)));}painter->setBrush(brushColor);painter->drawRect(-POS, -POS, WIDTH, HEIGHT);
}void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
{setFocus();// 设置光标为手握下的形状setCursor(Qt::ClosedHandCursor);QGraphicsItem::mousePressEvent(e);
}void MyItem::keyPressEvent(QKeyEvent *event)
{if (Qt::Key_Down == event->key())moveBy(0, 10);else if(Qt::Key_Up == event->key())moveBy(0, -10);else if(Qt::Key_Left == event->key())moveBy(-10, 0);else if(Qt::Key_Right == event->key())moveBy(10, 0);else{}QGraphicsItem::keyPressEvent(event);
}void MyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{// 设置光标为手张开的形状setCursor(Qt::OpenHandCursor);setToolTip("click me");
}void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{QMenu menu;QAction *moveAction = menu.addAction("move origin");QAction *selectedAction = menu.exec(event->screenPos());if (selectedAction == moveAction) {setPos(0, 0);}
}
myview.h

myview继承QGraphicsView,重新实现了滚轮事件,可对场景进行缩放操作

#ifndef MYVIEW_H
#define MYVIEW_H#include <QObject>
#include <QGraphicsView>class MyView : public QGraphicsView
{Q_OBJECT
public:explicit MyView(QWidget *parent = 0);protected:// 滚轮事件:缩放virtual void wheelEvent(QWheelEvent *event) override;
};
#endif // MYVIEW_H
myview.cpp
#include "myview.h"#include "myview.h"
#include <QKeyEvent>MyView::MyView(QWidget *parent) :QGraphicsView(parent)
{
}void MyView::wheelEvent(QWheelEvent *event)
{if(event->delta() > 0){scale(1.1, 1.1);}else{scale(0.9, 0.9);}// 加上这个,否则在场景和图形项中就没法再接收到该事件了QGraphicsView::wheelEvent(event);
}
调用
main.cpp
#include <QApplication>#include "myitem.h"
#include "myview.h"
#include <QTime>
#include <QtMath>#include <QDebug>int main(int argc, char* argv[ ])
{QApplication app(argc, argv);qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));QGraphicsScene scene;// 设置场景尺寸,减少动态渲染scene.setSceneRect(-300, -225, 600, 450);// 创建项for (int i = 0; i < 5; ++i) {MyItem *item = new MyItem;item->setColor(QColor(qrand() % 256, qrand() % 256, qrand() % 256));item->setPos(i * 50 - 100, -50);scene.addItem(item);}// 声明视图MyView view;view.setScene(&scene);// 设置背景刷view.setBackgroundBrush(QBrush(QColor(220, 220, 220)));view.show();return app.exec();
}

效果

显示效果如下:

自定义图形视图

http://www.yayakq.cn/news/888801/

相关文章:

  • 网站的空间什么意思iis建设的网站无法访问
  • 做电商网站价格表天津原筑展览展示有限公司
  • 2核4g 1m做网站中国互联网巨头排名
  • 网站做指向是什么意思金山集团网站建设
  • 做游戏网站教程广西南宁建设厅网站首页
  • 网站开发会遇到的问题做网站推销产品效果怎么样
  • 广元专业高端网站建设互动平台罗马复兴
  • 手机网站 手机app用台电脑做网站
  • 自己怎么样做网站网站建设策划报价单
  • 织梦珠宝网站模板建设一个会员积分网站
  • 网站建设优化服务资讯官方网站查询 优帮云
  • 永城市专业做网站wordpress架站
  • 无锡做网站的企业网站建设系统分析
  • 锦州市网站建设wordpress搭建方案
  • 上海外贸网站推广公司百度竞价推广有哪些
  • 个体工商网站备案wordpress分类目录表
  • 丰台建设公司网站淮安汽车集团网站建设
  • 做阿里巴巴网站费用吗化妆品网站模板下载
  • 网站开发技术架构wordpress模板+免费
  • 什么是网站域名简单的个人摄影网页制作源码
  • 中国建设银行潍坊市分行官方网站网站说说模板.
  • 上传宝贝网站建设属于什么类目有什么知名网站是用织梦做的
  • 网站开发于制作总结极乐宝盒网站建设
  • 网页设计网站开发需要什么あかねさす少女免费
  • 目前哪个网站建设的最好wordpress反弹攻击
  • 电子商务网站建设优势如何防止网站被攻击
  • 在哪个网站可做网络夫妻网站建设 信息化程度
  • 仓库网站开发武城网站建设价格
  • 邳州城乡建设局网站网站怎样做优惠卷
  • 凌云县城乡建设局网站台前网站建设费用