Qt工作笔记-图形视图框架中的分组,以及添加平行拖动图元
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-图形视图框架中的分组,以及添加平行拖动图元
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
啥意思,感覺本人語文學得菜,不好描述。
直接看程序運行截圖:
?
1.隱藏鼠標;
2.圖元分組;
3.視圖與場景坐標轉換
程序源碼如下:
?
mygraphicsitem.h
#ifndef MYGRAPHICSITEM_H #define MYGRAPHICSITEM_H#include <QGraphicsItem>class MyGraphicsItem:public QGraphicsItem { public:MyGraphicsItem(QGraphicsItem *parent=0);QRectF boundingRect()const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); };#endif // MYGRAPHICSITEM_Hmygraphicsview.h
#ifndef MYGRAPHICSVIEW_H #define MYGRAPHICSVIEW_H#include <QGraphicsView> class MyGraphicsItem;class MyGraphicsView:public QGraphicsView {Q_OBJECT public:MyGraphicsView(QWidget *parent=0);~MyGraphicsView();void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void setStartAndEndItem(MyGraphicsItem *start, MyGraphicsItem *end);private:qreal m_itemPosX;qreal m_itemPosY;qreal m_changedPosX;MyGraphicsItem *m_item;MyGraphicsItem *m_startItem;MyGraphicsItem *m_endItem;};#endif // MYGRAPHICSVIEW_Hwidget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> class QGraphicsScene;namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;QGraphicsScene *m_scene; };#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }mygraphicsitem.cpp
#include "mygraphicsitem.h" #include <QPainter>MyGraphicsItem::MyGraphicsItem(QGraphicsItem *parent):QGraphicsItem(parent) {}QRectF MyGraphicsItem::boundingRect() const {qreal penWidth=1;return QRectF(0-penWidth/2,0-penWidth/2,50+penWidth,50+penWidth); }void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {Q_UNUSED(option)Q_UNUSED(widget)painter->setPen(QPen(Qt::red,2));painter->drawRect(0,0,50,50); }mygrpahicsview.cpp
#include "mygraphicsview.h" #include <QDebug> #include <QGraphicsScene> #include "mygraphicsitem.h" #include <QMouseEvent>MyGraphicsView::MyGraphicsView(QWidget *parent):QGraphicsView(parent) {}MyGraphicsView::~MyGraphicsView() {}void MyGraphicsView::mousePressEvent(QMouseEvent *event) {m_changedPosX=event->x();m_item=new MyGraphicsItem;m_item->setPos(m_startItem->pos());scene()->addItem(m_item);setCursor(Qt::BlankCursor);QGraphicsView::mousePressEvent(event); }void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event) {setCursor(Qt::ArrowCursor);delete m_item;QGraphicsView::mouseReleaseEvent(event); }void MyGraphicsView::mouseMoveEvent(QMouseEvent *event) {qDebug()<<"最右端:"<<m_itemPosX+event->pos().x()-m_changedPosX<<" "<<m_endItem->pos().x();qDebug()<<"最左端:"<<m_itemPosX+event->pos().x()-m_changedPosX<<" "<<m_startItem->pos().x();if(m_itemPosX+event->pos().x()-m_changedPosX>m_endItem->pos().x()||m_itemPosX+event->pos().x()-m_changedPosX<m_startItem->pos().x()){return;}m_item->setPos(m_itemPosX+event->pos().x()-m_changedPosX,m_itemPosY); }void MyGraphicsView::setStartAndEndItem(MyGraphicsItem *start,MyGraphicsItem *end) {m_startItem=start;m_endItem=end;m_itemPosX=m_startItem->pos().x();m_itemPosY=m_startItem->pos().y(); }widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QGraphicsScene> #include <QMouseEvent> #include <QDebug> #include <QGraphicsItemGroup> #include "mygraphicsitem.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_scene=new QGraphicsScene;ui->graphicsView->setScene(m_scene);MyGraphicsItem *startItem=new MyGraphicsItem;startItem->setPos(-300,0);MyGraphicsItem *endItem=new MyGraphicsItem;endItem->setPos(300,0);QGraphicsItemGroup *group=new QGraphicsItemGroup;group->addToGroup(startItem);group->addToGroup(endItem);group->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);m_scene->addItem(group);ui->graphicsView->setStartAndEndItem(startItem,endItem); }Widget::~Widget() {delete ui; }?
總結
以上是生活随笔為你收集整理的Qt工作笔记-图形视图框架中的分组,以及添加平行拖动图元的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记-Object Tree
- 下一篇: C++ opengl 绘制三角形扇