Solved how to do about Windows drawing software dash rectangle?
-
like the Windows drawing software, i want to finished one function like the following images show.
user can adjust the rectangle size with cursor drag the small dash rectangles. The note is, when user zoom the viewport. the rectangle's line width is not zoomed and small dash rectangle's size also not zoomed
now, I have the QGraphicsView,Scene,Item...
about the line width. I think, maybe like the
QPen::setCosmetic
Cosmetic pens are used to draw strokes that have a constant width regardless of any transformations applied to the QPainter they are used with. Drawing a shape with a cosmetic pen ensures that its outline will have the same thickness at different scale factors.
Using the
Cosmetic
, we can get the same thickness line at different scale factors inQGraphicsView
.But, if i want to get the same size of shape at different scale factors in
QGraphicsView
. How to do it? is there has theCosmetic
for size of shape like pen?I also know
QGraphicsItem
has the flagItemIgnoresTransformations
.But, it is not a good way to my problem i think.is there has the example or demo about it? or some solution?
-
I saved my problem. I am thinking for 2 days. Shared for people who has the same problem.
#ifndef CONTROLHANDLE_H #define CONTROLHANDLE_H #include <QGraphicsItem> QT_BEGIN_NAMESPACE class QGraphicsView; QT_END_NAMESPACE class ControlHandles : public QGraphicsItem { public: explicit ControlHandles(QRect parentRect,QGraphicsView* pView,QGraphicsItem* parent); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: QRect _parentRect; QGraphicsView* _pView = NULL; QSize _HandleSize = QSize(6,6); }; #endif // CONTROLHANDLE_H
#include "controlhandles.h" #include <QPainter> #include <QGraphicsView> ControlHandles::ControlHandles(QRect parentRect, QGraphicsView *pView, QGraphicsItem *parent): QGraphicsItem(parent) { Q_ASSERT(NULL!=pView && NULL!=parent); _parentRect = parentRect; _pView = pView; setPos(parentRect.topLeft()); setFlag(QGraphicsItem::ItemIgnoresTransformations); } QRectF ControlHandles::boundingRect() const { qreal x = 0 - _HandleSize.width(); qreal y = 0 - _HandleSize.height(); qreal w = _parentRect.width() * _pView->matrix().m11(); qreal h = _parentRect.height() * _pView->matrix().m22(); w += _HandleSize.width(); h += _HandleSize.height(); return QRectF(x,y,w,h); } void ControlHandles::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); qreal w = _HandleSize.width(); qreal h = _HandleSize.height(); qreal x = 0 - w/2; qreal y = 0 - h/2; qreal xOffset = _parentRect.width() * _pView->matrix().m11(); qreal yOffset = _parentRect.height() * _pView->matrix().m22(); /** Top Left */ painter->drawRect(QRectF(x, y, w,h)); /** Left Center */ painter->drawRect(QRectF(x, y+yOffset/2, w,h)); /** Bottom Left */ painter->drawRect(QRectF(x, y+yOffset, w,h)); /** Boottom Center */ painter->drawRect(QRectF(x+xOffset/2, y+yOffset, w,h)); /** Bottom Right */ painter->drawRect(QRectF(x+xOffset, y+yOffset, w,h)); /** Right Center */ painter->drawRect(QRectF(x+xOffset, y+yOffset/2, w,h)); /** Top Right */ painter->drawRect(QRectF(x+xOffset, y, w,h)); /** Top Center */ painter->drawRect(QRectF(x+xOffset/2, y, w,h)); }