Now we render the background of the scene with our own OpenGL calls..
#ifndef MAIN_H
#define MAIN_H
#include <QtCore>
#include <QtGui>
#include <QtOpenGL>
using namespace std;
{
public:
OpenGLScene() {
}
protected:
};
{
/*if (painter->paintEngine()->type() != QPaintEngine::OpenGL) {
qWarning("OpenGLScene: drawBackground needs a "
"QGLWidget to be set as viewport on the "
"graphics view");
return;
}*/
//
glClearColor(m_backgroundColor.redF(),
m_backgroundColor.greenF(),
m_backgroundColor.blueF(), 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
{
public:
private:
OpenGLScene *scene;
};
MainWindow
::MainWindow(QWidget *parent
) :{
scene = new OpenGLScene();
scene->addWidget(btn);
graphicsView->setScene(scene);
graphicsView
->setGeometry
(QRect(50,
50,
500,
300));
graphicsView->show();
this->setCentralWidget(graphicsView);
}
#endif // MAIN_H
#ifndef MAIN_H
#define MAIN_H
#include <QtCore>
#include <QtGui>
#include <QtOpenGL>
using namespace std;
class OpenGLScene : public QGraphicsScene
{
public:
OpenGLScene() {
}
protected:
void drawBackground(QPainter *painter, const QRectF &);
};
void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
{
/*if (painter->paintEngine()->type() != QPaintEngine::OpenGL) {
qWarning("OpenGLScene: drawBackground needs a "
"QGLWidget to be set as viewport on the "
"graphics view");
return;
}*/
//
QColor m_backgroundColor = QColor("yellow");
glClearColor(m_backgroundColor.redF(),
m_backgroundColor.greenF(),
m_backgroundColor.blueF(), 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget *parent = 0);
private:
OpenGLScene *scene;
QGraphicsView *graphicsView;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
scene = new OpenGLScene();
QPushButton* btn = new QPushButton("Test");
scene->addWidget(btn);
graphicsView = new QGraphicsView(this);
graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
graphicsView->setScene(scene);
graphicsView->setGeometry(QRect(50, 50, 500, 300));
graphicsView->show();
this->setCentralWidget(graphicsView);
}
#endif // MAIN_H
To copy to clipboard, switch view to plain text mode
Aah! Something is wrong with this check! Did produce the same warning here. But the OpenGL stuff obviously works!
Joh
Bookmarks