Skip to content

Instantly share code, notes, and snippets.

@Atsushi4
Last active December 30, 2015 10:49
Show Gist options
  • Save Atsushi4/7818810 to your computer and use it in GitHub Desktop.
Save Atsushi4/7818810 to your computer and use it in GitHub Desktop.
テキスト書けたよありがとう!
#include <QApplication>
#include <QtWidgets>
#include <QtCore>
#include <QtGui>
class MyView : public QGraphicsView
{
Q_OBJECT
public:
MyView() {
setScene(new QGraphicsScene(-2000, -2000, 4000, 4000));
}
protected:
void drawBackground(QPainter *painter, const QRectF &rect) {
for (int x = static_cast<int>(rect.left() / 10 + 1) * 10; x < rect.right(); x += 10) {
for (int y = static_cast<int>(rect.top() / 10 + 1) * 10; y < rect.bottom(); y += 10) {
painter->drawLine(rect.left(), y, rect.right(), y);
painter->drawLine(x, rect.top(), x, rect.bottom());
if (y % 50 == 0) {
painter->drawText(0, y, QString::number(y));
if (x == y) {
painter->save();
painter->setPen(QPen(Qt::red));
painter->setBrush(QBrush(qRgba(128, 0, 0, 50)));
painter->drawEllipse(x - 20, y - 20, 40, 40);
painter->restore();
}
}
}
if (x % 50 == 0)
painter->drawText(x, 0, QString::number(x));
}
QGraphicsView::drawBackground(painter, rect);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyView v;
v.resize(200, 200);
v.show();
return a.exec();
}
#include "graphicsview.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment