QT中使用rubberband橡皮筋等方法进行选中多个物体
生活随笔
收集整理的這篇文章主要介紹了
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学习之动画框架