Skip to content

Instantly share code, notes, and snippets.

@Atsushi4
Created September 13, 2012 13:49
Show Gist options
  • Save Atsushi4/3714416 to your computer and use it in GitHub Desktop.
Save Atsushi4/3714416 to your computer and use it in GitHub Desktop.
QGraphicsViewの表面にテキストを表示するサンプル
#include <QtGui>
class Window : public QGraphicsView
{
Q_OBJECT
public:
Window() {
setScene(new QGraphicsScene(-200, -200, 400, 400));
setDragMode(ScrollHandDrag);
drawMesh();
resize(200, 200);
setViewportUpdateMode(FullViewportUpdate);
}
protected:
void paintEvent(QPaintEvent *event) {
QPainter p(this);
p.drawText(QRect(0, 0, width() - 20, p.font().pointSize()), "hogehoge", QTextOption(Qt::AlignTop | Qt::AlignRight));
QGraphicsView::paintEvent(event);
}
private:
void drawMesh() {
const QRectF rect = scene()->sceneRect();
for (int i = rect.left(); i <= rect.right(); i += 10)
scene()->addLine(i, rect.top(), i, rect.bottom()), scene()->addLine(rect.left(), i, rect.right(), i);
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window w;
w.show();
return app.exec();
}
#include "text_on_graphicsview.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment