Adding pixels to the QGraphicsRectItem does not work.
So here is some sample code that demonstrates my problem. I have an initial scene and then "zoom" to a particular area. This is where I draw the rectangle. If the rectangle is then selected, a red "hilite" should be drawn around the rectangle . . . and currently its suppose to be penWidth pixels wide. However, it is many pixels wide in the Y direction and nowhere in the X direction.
MyRect.h
#include <QPen>
#include <QDebug>
#include <QPainter>
#include <QGraphicsRectItem>
#include <QStyleOptionGraphicsItem>
{
Q_OBJECT
public:
{
setItUp();
}
{
setItUp();
}
~MyRect() {}
void setItUp()
{
m_penWidth = 1.0;
setPen
( QPen(Qt
::green) );
setAcceptHoverEvents( true );
}
{
return this->rect().adjusted( -m_penWidth, -m_penWidth,
m_penWidth, m_penWidth );
}
protected:
{
Q_UNUSED(widget);
painter->setPen(pen());
painter->setBrush(brush());
painter->drawRect(rect());
if (option
->state
& QStyle::State_Selected) {
const QRectF murect
= painter
->transform
().
mapRect(QRectF(0,
0,
1,
1));
if (qFuzzyCompare(qMax(murect.width(), murect.height()) + 1, 1))
return;
const QRectF mbrect
= painter
->transform
().
mapRect(this
->boundingRect
());
if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
return;
const qreal pad = hilitePen.widthF() / 2;
painter->setPen(hilitePen);
painter->setBrush(Qt::NoBrush);
painter->drawRect(this->boundingRect().adjusted(pad, pad, -pad, -pad));
}
}
private:
qreal m_penWidth;
};
#include <QPen>
#include <QDebug>
#include <QPainter>
#include <QGraphicsRectItem>
#include <QStyleOptionGraphicsItem>
class MyRect : public QObject, public QGraphicsRectItem
{
Q_OBJECT
public:
MyRect( QGraphicsItem *pParent=0 )
: QGraphicsRectItem( pParent )
{
setItUp();
}
MyRect( const QRectF & rect, QGraphicsItem* pParent=0 )
: QGraphicsRectItem( rect, pParent )
{
setItUp();
}
~MyRect() {}
void setItUp()
{
m_penWidth = 1.0;
setPen( QPen(Qt::green) );
setAcceptHoverEvents( true );
setFlag( QGraphicsItem::ItemIsSelectable, true );
}
QRectF boundingRect() const
{
return this->rect().adjusted( -m_penWidth, -m_penWidth,
m_penWidth, m_penWidth );
}
protected:
void paint( QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget )
{
Q_UNUSED(widget);
painter->setPen(pen());
painter->setBrush(brush());
painter->drawRect(rect());
if (option->state & QStyle::State_Selected)
{
const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1));
if (qFuzzyCompare(qMax(murect.width(), murect.height()) + 1, 1))
return;
const QRectF mbrect = painter->transform().mapRect(this->boundingRect());
if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
return;
QPen hilitePen(QBrush(Qt::red),m_penWidth);
const qreal pad = hilitePen.widthF() / 2;
painter->setPen(hilitePen);
painter->setBrush(Qt::NoBrush);
painter->drawRect(this->boundingRect().adjusted(pad, pad, -pad, -pad));
}
}
private:
qreal m_penWidth;
};
To copy to clipboard, switch view to plain text mode
main.cpp
#include "MyRect.h"
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
int main(int argc, char **argv)
{
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
19900000.0,1536.0 );
view.setScene(scene);
QRectF seRect
(2154794311.0,
742.0,
4000.0,
5.0);
MyRect *rect = new MyRect;
scene->addItem( rect );
rect->setRect( seRect );
QRectF zmRect
(2154696311.0,
732.0,
200000.0,
20.0);
view.show();
view.fitInView( zmRect );
return app.exec();
}
#include "MyRect.h"
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsView view;
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
QGraphicsScene *scene = new QGraphicsScene( 2147583648.0,22.0,
19900000.0,1536.0 );
view.setScene(scene);
QRectF seRect(2154794311.0,742.0,4000.0,5.0);
MyRect *rect = new MyRect;
scene->addItem( rect );
rect->setRect( seRect );
QRectF zmRect(2154696311.0,732.0,200000.0,20.0);
view.show();
view.fitInView( zmRect );
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks