Skip to content

Instantly share code, notes, and snippets.

@jjgod
Created March 20, 2012 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjgod/2135911 to your computer and use it in GitHub Desktop.
Save jjgod/2135911 to your computer and use it in GitHub Desktop.
#include <QPainter>
#include <QGuiApplication>
#include <QTextLayout>
#include <QElapsedTimer>
#include <QWindow>
#include <QScreen>
#include <QImage>
#include <QOpenGLContext>
#include <QOpenGLPaintDevice>
#include <QDebug>
class Window : public QWindow
{
public:
Window(QScreen *screen = 0);
protected:
void exposeEvent(QExposeEvent *);
private:
void render();
QImage m_buffer;
QTextLayout m_layout;
QOpenGLContext *m_context;
};
Window::Window(QScreen *screen)
: QWindow(screen)
{
setSurfaceType(QSurface::OpenGLSurface);
setGeometry(QRect(0, 0, 400, 400));
create();
m_context = new QOpenGLContext(this);
m_context->create();
m_buffer = QImage(geometry().size(), QImage::Format_ARGB32_Premultiplied);
QFont font = QFont("Monospace");
font.setPixelSize(12);
m_layout.setFont(font);
QTextOption option;
m_layout.setTextOption(option);
m_layout.setText(QString::fromUtf8("Hello. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."));
m_layout.beginLayout();
qreal height = 0;
while (1) {
QTextLine line = m_layout.createLine();
if (!line.isValid())
break;
line.setLineWidth(geometry().size().width());
line.setPosition(QPointF(0, height));
height += line.height();
}
m_layout.endLayout();
}
void Window::exposeEvent(QExposeEvent *)
{
render();
}
void Window::render()
{
{
m_buffer.fill(Qt::white);
QPainter p(&m_buffer);
QElapsedTimer t;
t.start();
m_layout.draw(&p, QPointF(0, 0));
}
QRect rect(QPoint(), geometry().size());
m_context->makeCurrent(this);
QOpenGLPaintDevice device(rect.size());
QPainter p(&device);
p.drawImage(0, 0, m_buffer);
p.end();
m_context->swapBuffers(this);
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
Window a;
a.show();
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment