#include <QtGui>
public:
protected:
i->setSelected(false);
m_path.setFillRule(Qt::WindingFill);
m_path.moveTo(event->posF());
}
m_path.lineTo(event->posF());
viewport()->update();
}
m_path.closeSubpath();
QList<QGraphicsItem*> it = scene()->items(mapToScene(m_path));
i->setSelected(true);
viewport()->update();
}
if(m_path.isEmpty())
return;
pen.setWidth(4);
p.setPen(pen);
p.
setRenderHint(QPainter::Antialiasing);
p.
setBrush(QColor(255,
0,
0,
100));
p.drawPath(m_path);
}
private:
};
int main(int argc, char **argv) {
qsrand
(QDateTime::currentDateTime().
toTime_t());
for(int i=0;i<50;i++) {
QColor c
(qrand
() % 255, qrand
() % 255, qrand
() % 255);
scene.
addEllipse(qrand
() % 500, qrand
() % 500, qrand
() % 100, qrand
() % 100, c, c
)->setFlag
(QGraphicsItem::ItemIsSelectable);
}
View view;
view.
setRenderHint(QPainter::Antialiasing);
view.setScene(&scene);
view.show();
return app.exec();
}
#include <QtGui>
class View : public QGraphicsView {
public:
View() : QGraphicsView(){}
protected:
void mousePressEvent(QMouseEvent *event) {
foreach(QGraphicsItem *i, scene()->items())
i->setSelected(false);
m_path.setFillRule(Qt::WindingFill);
m_path.moveTo(event->posF());
}
void mouseMoveEvent(QMouseEvent *event) {
m_path.lineTo(event->posF());
viewport()->update();
}
void mouseReleaseEvent(QMouseEvent *event) {
m_path.closeSubpath();
QList<QGraphicsItem*> it = scene()->items(mapToScene(m_path));
foreach(QGraphicsItem *i, it)
i->setSelected(true);
viewport()->update();
m_path = QPainterPath();
}
void paintEvent(QPaintEvent *event) {
QGraphicsView::paintEvent(event);
if(m_path.isEmpty())
return;
QPainter p(viewport());
QPen pen(QColor(Qt::red));
pen.setWidth(4);
p.setPen(pen);
p.setRenderHint(QPainter::Antialiasing);
p.setBrush(QColor(255,0,0,100));
p.drawPath(m_path);
}
private:
QPainterPath m_path;
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
QGraphicsScene scene;
qsrand(QDateTime::currentDateTime().toTime_t());
for(int i=0;i<50;i++) {
QColor c(qrand() % 255, qrand() % 255, qrand() % 255);
scene.addEllipse(qrand() % 500, qrand() % 500, qrand() % 100, qrand() % 100, c, c)->setFlag(QGraphicsItem::ItemIsSelectable);
}
View view;
view.setRenderHint(QPainter::Antialiasing);
view.setScene(&scene);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks