QT中使用rubberband橡皮筋等方法进行选中多个物体
文章目录
- 功能介绍
- 代码介绍
- myitem.h/cpp 图形项
- myview.h/cpp场景
- 主函数
- 结果展示
- 代码下载
- 参考
功能介绍
- 点击左键、按 Shift 键可以单选,按下 Ctrl 可进行多选。
- 选中时候点击右键,可以选择隐藏物体,归位
- 移动:点击左键,选择 item,然后移动鼠标
代码介绍
myitem.h/cpp 图形项
myitem.h
#ifndef MYITEM_H
#define MYITEM_H#include <QGraphicsItem>class MyItem : public QGraphicsItem
{
public:MyItem();~MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);void setColor(const QColor &color){brushColor = color;};private:QColor brushColor;protected:void keyPressEvent(QKeyEvent *event);void mousePressEvent(QGraphicsSceneMouseEvent *event);void hoverEnterEvent(QGraphicsSceneMouseEvent *event);void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);};#endif // MYITEM_H
myitem.cpp
#include "myitem.h"
#include<QPainter>
#include<QDebug>
#include<QCursor>
#include<QKeyEvent>
#include<QGraphicsSceneHoverEvent>
#include<QGraphicsSceneContextMenuEvent>
#include<QMenu>MyItem::MyItem()
{brushColor = Qt::red;//设置图形项可以移动setFlag(QGraphicsItem::ItemIsMovable);//设置图形项可以选择setFlag(QGraphicsItem::ItemIsSelectable);//设置接受鼠标悬停事件setAcceptHoverEvents(true);
}MyItem::~MyItem()
{}//设置包围盒,返回图形项矩形区域
//详细信息参考:http://doc.qt.io/archives/qt-4.8/qgraphicsitem.html
QRectF MyItem::boundingRect() const
{//画笔宽度qreal adjust = 0.5;//因为paint函数中的矩形是以(-10,-10)为原点画出的长宽均为20的矩形return QRectF(-10 - adjust, -10 - adjust, 20 + adjust, 20 + adjust);
}//执行实际的绘图操作
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{//判断是否被选中if (isSelected()){//设置轮廓为白色painter->setPen(QPen(QColor(255, 255, 255, 200)));}else{painter->setPen(QPen(QColor(100, 100, 100, 100)));}//设置填充颜色painter->setBrush(brushColor);//绘制矩形painter->drawRect(-10, -10, 20, 20);
}//键盘按下处理函数,判断是否向下按下方向键,是的话,则向下移动图形项
void MyItem::keyPressEvent(QKeyEvent *event)
{if (event->key() == Qt::Key_Down){moveBy(0, 10);}else if (event->key() == Qt::Key_Up){moveBy(0, -10);}else if (event->key() == Qt::Key_Right){moveBy(10, 0);}else if (event->key() == Qt::Key_Left){moveBy(-10, 0);}
}//鼠标按下事件处理函数,设置被点击的图形项是否被选中,并改变外观
//系统默认Ctrl+鼠标左键可实现多选
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{//设置鼠标形状setCursor(Qt::ClosedHandCursor);if (event->button() == Qt::LeftButton){//设置shift+鼠标左键单选if (event->modifiers() == Qt::ShiftModifier){qDebug() << "item left clicked with shift key";setSelected(true);}else{qDebug() << "item left clicked";QGraphicsItem::mousePressEvent(event);}}
// else if (event->button() == Qt::RightButton)
// {
// qDebug() << "item right clicked";
// }
}//悬停事件处理函数,设置光标外观和提示
void MyItem::hoverEnterEvent(QGraphicsSceneMouseEvent *event)
{setCursor(Qt::OpenHandCursor);
}//右键菜单事件处理函数,为图形项添加一个右键菜单
void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{QMenu menu;QAction *moveAction = menu.addAction("move back");QAction *hideAction = menu.addAction("hide");QAction *showAction = menu.addAction("show");QAction *selectedAction = menu.exec(event->screenPos());//让图形项归位到场景原点if (selectedAction == moveAction){setPos(0, 0);}else if (selectedAction == hideAction){setVisible(false);}else if (selectedAction == showAction){setVisible(true);}
}
myview.h/cpp场景
myview.h
#ifndef MYVIEW_H
#define MYVIEW_H
#include <QGraphicsView>class MyView : public QGraphicsView
{Q_OBJECT
public:explicit MyView(QWidget *parent = 0);
// MyView();~MyView();protected:void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);};#endif // MYVIEW_H
myview.cpp
#include "myview.h"
#include <QKeyEvent>
#include <QMouseEvent>
#include <QGraphicsView>MyView::MyView(QWidget *parent) :QGraphicsView(parent)
{//设置橡皮筋选择选项setDragMode(QGraphicsView::RubberBandDrag);
}MyView::~MyView()
{}void MyView::mousePressEvent(QMouseEvent *event)
{QGraphicsView::mousePressEvent(event);
}void MyView::mouseReleaseEvent(QMouseEvent *event)
{QGraphicsView::mouseReleaseEvent(event);
}
主函数
#include "myitem.h"
#include "myview.h"
#include <QApplication>
#include <QTime>
#include <QGraphicsScene>int main(int argc, char *argv[])
{QApplication a(argc, argv);//设置随机种子qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));//设置场景QGraphicsScene scene;scene.setSceneRect(-200, -150, 400, 300);//随机生成5个颜色不同的矩形for (int i = 0; i < 5; i++){MyItem *item = new MyItem;item->setColor(QColor(qrand() % 256, qrand() % 256, qrand() % 256));item->setPos(i * 50 - 90, -50);//将图形项添加到场景中scene.addItem(item);}MyView view;//为视图添加场景view.setScene(&scene);//设置场景背景view.setBackgroundBrush(Qt::gray);//显示view.show();return a.exec();
}
结果展示
代码下载
https://download.csdn.net/download/sty945/10931750
参考
https://blog.csdn.net/liang19890820/article/details/53504323
QT中使用rubberband橡皮筋等方法进行选中多个物体相关推荐
- 在Qt中设置程序图标的方法介绍
本文主要介绍在 Qt 中设置程序图标的方法. 说明: 本文中的应用程序是面向 Windows 操作系统的: 本文中使用的 Qt Creator 版本号为:7.0.0: 本文中使用的 Qt 版本号为:5 ...
- Qt中添加背景图片的方法
Qt中添加背景图片的方法 QPalette的方法 #include #include int main(int argc, char *argv[]) { QApplication app(argc, ...
- QT中生成字符串md5的方法
QT中,提供了QCryptographicHash类,很方便的实现的字符串到md5/md4/sha1的转换, 可以通过两种方法实现: [cpp] view plain copy QString pwd ...
- QT中自定义控件和插件大致方法
文章目录 前言 一.方法:右键提升为 二.方法:变为qt插件,作为ui使用 总结 前言 Qt中自带的控件有时候无法满足项目需求,这就需要在原有的控件或者重新编写代码. 一.方法:右键提升为 该方法主要 ...
- QT中QLabel的常见使用方法
QLabel标签控件在QT中算是比较常见的,它一般都是用来显示文本内容,当然它的功能不是只局限在这里,还可以用它来显示照片,gif动画等,那接下来就看看是如何操作它的吧: 首先先创建一个新项目,在ui ...
- Qt中查看ui_xxx.h文件方法
前提 1.Qt当有界面 2.构造完成 满足以上两个条件qt会生成ui_xxx.h文件. 如何查看 方法1 在cpp文件中找到UI下的一个对象 如: ui->textEdit Ui::QWDial ...
- (转载)Qt中使用cout输出的方法
(转载)http://blog.sina.com.cn/s/blog_4f183d960100sdxf.html 最近用QT写一个控制台程序,却不能将提示文本输出到屏幕. cout<<&q ...
- Ubuntu系统装了搜狗输入法,但还是不能在QT中输入中文的解决方法
在编写Linux的Qt项目时,发现虽然已经在Linux下安装好搜狗输入法了,但是,在QT里面还是无法输入中文,这是为什么呢???因为Qtcreator比较奇葩,你会发现装了搜狗输入法后不能在Qtcre ...
- qt c语言程序运行时间,c – 在Qt中计时事件的最佳方法
如果使用带有Functor(函数对象)的 overloaded method,则可以使用静态QTimer单次拍摄功能.这将允许您捕获所需的变量;哪个电机,速度,动作等.如果你不熟悉Functors,你 ...
最新文章
- my.ini修改后服务无法启动_VisualSVN Server 自助修改密码页面
- Windows Server 2008 没有磁盘清理工具的情况下使用系统提供的磁盘清理工具
- JavaWeb --第三章 HTTP协议详解
- android+内存清理+代码,最新版本:Android一键式清理,内存清理功能的实现
- 通讯可以并联吗_工业控制知识:吃透RS485通讯的连接方式,接485总线就简单多了...
- C# 编程规范 (coding standard)
- 关于学习新知识的一点想法
- 朴素贝叶斯分类算法(Naive Bayesian classification)
- poj 2157 Maze(bfs)
- 华为云NP考试题库_华为np题库
- 马哥Linux 高端运维云计算就业班
- 刚体转动惯量c语言计算程序,Tracker软件在三线摆测量刚体转动惯量实验中的应用...
- 网页版数据库管理工具
- 小程序左滑删除,可上下滑动
- JQuery设置网页全屏代码实现
- 查询攻略,将多个退回延误的快递单号物流设置分类
- 苹果犯了“围师必阙”大忌
- Webpack实用工具之webpack-server
- T3备份提示原先格式化该文件时所用扇区大小为512
- 帆软内SQL语句的使用