I'm trying to use QPainter in my QOpenGLWidget. But it only works with 3.0 OpenGL version. What is the problem?
Here is my code.
#include <QApplication>
#include <QMainWindow>
#include "window.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(3,3);
// Set widget up
Window *widget = new Window;
widget->setFormat(format);
// Set the window up
QMainWindow window;
window.setCentralWidget(widget);
window.resize(QSize(800, 600));
window.show();
return app.exec();
}
If i'll comment "format.setVersion(3,3);" everything will work fine. But my shaders won't start up.
And QOpenGLWidget subclass
#ifndef WINDOW_H
#define WINDOW_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class QOpenGLShaderProgram;
class Window : public QOpenGLWidget,
protected QOpenGLFunctions
{
Q_OBJECT
// OpenGL Events
public:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
};
#endif // WINDOW_H
and simpliest example.
#include "window.h"
#include <QDebug>
#include <QString>
#include <QOpenGLShaderProgram>
#include "vertex.h"
#include <QPainter>
void Window::initializeGL()
{}
void Window::resizeGL(int width, int height)
{}
void Window::paintGL()
{
QPainter p(this);
p.setPen(Qt::red);
p.drawLine(rect().topLeft(), rect().bottomRight());
}
See Question&Answers more detail:os