Created
January 31, 2020 12:51
-
-
Save geotavros/446baeffc63ca353731fa9523ca642a2 to your computer and use it in GitHub Desktop.
Qt OpenGL Minimal QGLWidget Implementation for QPainter Painting, Drawing OpenGL Triangle, QPainter Text and Circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QGLWidget> | |
class GLWidget : public QGLWidget | |
{ | |
public: | |
GLWidget(QWidget *parent = 0); | |
~GLWidget(); | |
QSize sizeHint() const { return QSize(400, 400); } | |
protected: | |
void initializeGL() override; | |
void paintEvent(QPaintEvent *e) override; | |
void resizeGL(int width, int height) override; | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
GLWidget glWidget; | |
glWidget.show(); | |
return app.exec(); | |
} | |
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) | |
{ | |
// requied for QPainter operations after OpenGL drawing | |
// otherwise QPainter fills image with solid color | |
setAutoFillBackground(false); | |
} | |
GLWidget::~GLWidget() | |
{ | |
} | |
void GLWidget::initializeGL() | |
{ | |
} | |
void GLWidget::paintEvent(QPaintEvent *e) | |
{ | |
makeCurrent(); | |
QSize viewport_size = size(); | |
glViewport(0, 0, viewport_size.width(), viewport_size.height()); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance | |
glMatrixMode(GL_MODELVIEW); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
glTranslatef(-1.5f, 0.0f, -6.0f); | |
glBegin(GL_TRIANGLES); | |
glColor3f(1, 0, 0); | |
glVertex3f(0.0f, 1.0f, 0.0f); | |
glColor3f(0.0, 1, 0); | |
glVertex3f(-1.0f, -1.0f, 0.0f); | |
glColor3f(0.0, 0.0, 1); | |
glVertex3f(1.0f, -1.0f, 0.0f); | |
glEnd(); | |
QPainter painter(this); | |
painter.setRenderHint(QPainter::Antialiasing); | |
painter.setPen(Qt::white); | |
QString text = tr("Text drawn with QPainter"); | |
QFontMetrics metrics = QFontMetrics(font()); | |
int border = qMax(4, metrics.leading()); | |
QRect rect = metrics.boundingRect(0, 0, width() - 2 * border, int(height()*0.125), | |
Qt::AlignCenter | Qt::TextWordWrap, text); | |
painter.setRenderHint(QPainter::TextAntialiasing); | |
painter.fillRect(QRect(0, 0, width(), rect.height() + 2 * border), | |
QColor(0, 0, 0, 127)); | |
painter.setPen(Qt::white); | |
painter.fillRect(QRect(0, 0, width(), rect.height() + 2 * border), | |
QColor(0, 0, 0, 127)); | |
painter.drawText((width() - rect.width()) / 2, border, | |
rect.width(), rect.height(), | |
Qt::AlignCenter | Qt::TextWordWrap, text); | |
painter.setPen(Qt::red); | |
painter.drawEllipse(QPoint(width() / 2, height() / 2), 30, 30); | |
painter.end(); // calls swapBuffers(); | |
// it is necessary to call swapBuffers() manually if QPainter is not used in paintEvent() | |
} | |
void GLWidget::resizeGL(int w, int h) | |
{ | |
QGLWidget::resize(w, h); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment