#include <QtGui>
{
public:
GridScene(qreal x, qreal y, qreal w, qreal h)
{ }
protected:
{
const int gridSize = 25;
painter
->setPen
(QPen(Qt
::darkGreen,
0));
qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
QVarLengthArray<QLineF, 100> lines;
for (qreal x = left; x < rect.right(); x += gridSize)
lines.
append(QLineF(x, rect.
top(), x, rect.
bottom()));
for (qreal y = top; y < rect.bottom(); y += gridSize)
lines.
append(QLineF(rect.
left(), y, rect.
right(), y
));
painter->drawLines(lines.data(), lines.size());
}
};
{
public:
{
setPos(pos);
setFlags(0);
setAcceptedMouseButtons(0);
}
{
p->setPen(Qt::darkRed);
p
->drawEllipse
(QRectF(-3,
-3,
2*3,
2*3));
}
{
path.
addEllipse(QRectF(-3,
-3,
2*3,
2*3));
return path;
}
{
return QRectF(-3,
-3,
2*3,
2*3);
}
};
{
public:
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
// comment the following 4 lines to see the performance difference
t->setPos(0,-35);
new Node
(QPointF(-30,
0),
this,scene
);
new Node
(QPointF(30,
0),
this,scene
);
}
{
if(!(o
->state
& QStyle::State_Open)) p->setPen(Qt::darkBlue);
if(o
->state
& QStyle::State_Selected) p
->setPen
(QPen(Qt
::darkGray,
1));
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
}
{
qreal pw = 0.5;
return QRectF(-27,
-9,
54,
18).
adjusted(-pw,
-pw,pw,pw
);
}
};
int main(int argc,char *argv[])
{
GridScene scene(0,0,1024,768);
for(int j = 2; j < 4; ++j)
for(int i = 1; i <11; ++i)
{
Resistor *r = new Resistor(0,&scene);
r->setPos(j*100, i * 50);
}
view->show();
return app.exec();
}
#include <QtGui>
class GridScene : public QGraphicsScene
{
public:
GridScene(qreal x, qreal y, qreal w, qreal h)
: QGraphicsScene(x, y, w, h)
{ }
protected:
void drawBackground(QPainter *painter, const QRectF &rect)
{
const int gridSize = 25;
painter->setPen(QPen(Qt::darkGreen,0));
qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
QVarLengthArray<QLineF, 100> lines;
for (qreal x = left; x < rect.right(); x += gridSize)
lines.append(QLineF(x, rect.top(), x, rect.bottom()));
for (qreal y = top; y < rect.bottom(); y += gridSize)
lines.append(QLineF(rect.left(), y, rect.right(), y));
painter->drawLines(lines.data(), lines.size());
}
};
class Node : public QGraphicsItem
{
public:
Node(QPointF pos ,QGraphicsItem *par = 0, QGraphicsScene *sc = 0) : QGraphicsItem(par,sc)
{
setPos(pos);
setFlags(0);
setAcceptedMouseButtons(0);
}
void paint(QPainter* p,const QStyleOptionGraphicsItem *, QWidget *)
{
p->setPen(Qt::darkRed);
p->drawEllipse(QRectF(-3, -3, 2*3, 2*3));
}
QPainterPath shape() const
{
QPainterPath path;
path.addEllipse(QRectF(-3, -3, 2*3, 2*3));
return path;
}
QRectF boundingRect() const
{
return QRectF(-3, -3, 2*3, 2*3);
}
};
class Resistor : public QGraphicsItem
{
public:
Resistor(QGraphicsItem *par = 0, QGraphicsScene *scene = 0) : QGraphicsItem(par,scene)
{
setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
// comment the following 4 lines to see the performance difference
QGraphicsTextItem *t = new QGraphicsTextItem("R1 = 100k",this,scene);
t->setPos(0,-35);
new Node(QPointF(-30,0),this,scene);
new Node(QPointF(30,0),this,scene);
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *)
{
if(!(o->state & QStyle::State_Open))
p->setPen(Qt::darkBlue);
if(o->state & QStyle::State_Selected)
p->setPen(QPen(Qt::darkGray,1));
p->drawLine(-18, -9, 18, -9);
p->drawLine( 18, -9, 18, 9);
p->drawLine( 18, 9,-18, 9);
p->drawLine(-18, 9,-18, -9);
p->drawLine(-27, 0,-18, 0);
p->drawLine( 18, 0, 27, 0);
}
QRectF boundingRect() const
{
qreal pw = 0.5;
return QRectF(-27,-9,54,18).adjusted(-pw,-pw,pw,pw);
}
};
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
GridScene scene(0,0,1024,768);
for(int j = 2; j < 4; ++j)
for(int i = 1; i <11; ++i)
{
Resistor *r = new Resistor(0,&scene);
r->setPos(j*100, i * 50);
}
QGraphicsView *view = new QGraphicsView(&scene);
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks