我想在我的QOpenGLWidget中使用QPainter。但它只適用於3.0 OpenGL版本。問題是什麼?如何在OpenGL 3.3格式中使用QPainter?
這是我的代碼。
#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();
}
如果我會評論「format.setVersion(3,3);」一切都會正常工作。但是我的着色器不會啓動。
而且QOpenGLWidget子
#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
和simpliest例子。
#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());
}
你有什麼硬件和驅動程序?他們是否支持OpenGL 3.3? – ybungalobill