开发者

QGLWidget + QGraphicsScene + QGraphicsView problem

I would like to create a simple thumbnail viewer using QGLWidget, QGraphicsScene and QGraphicsView. And I have a problem with placing QGLWidget on QGraphicsScene. The code is similar to this:

QGraphicsScene *testScene = new QGraphicsScene (0, 0, 400, 400,开发者_运维百科 0);

QGLWidget *testWidget1 = new QGLWidget();
testWidget1->renderText("Test text1");

QGLWidget *testWidget2 = new QGLWidget();
testWidget2->renderText("Test text2");

testScene->addWidget(testWidget1);
testScene->addWidget(testWidget2);

QGraphicsView *testView = new QGraphicsView();
testView->setScene(testScene);
testView->show()

It is possible to place few QGLWidgets on QGraphicsScene/QGraphicsView? Where I doing something wrong? Is there any other component on which I could embed QGLWidgets and show them on the screen?

Thanks for help


To make QGLWidget show in QGraphicsView, you should redirect painting for it. You can overwrite the paintGL like this:

virtual void MyGLWidget::paintGL()
{
    QGLWidget::paintGL();
    //support redirecting painting
    QPaintDevice* device = QPainter::redirected(this);
    if (device != NULL && device != this)
    {
        QImage image = grabFrameBuffer();
        QPainter painter(this);
        painter.drawImage(QPointF(0.0,0.0),image);
    }
}

It works well for most platforms, but still happens to show black widget.


The QGraphicsScene::addWidget documentation states that QGLWidget is not a supported widget type.

Parenting a QGLWidget onto the viewport of the QGraphicsView doesn't seem to work either.

Edit:

Actually parenting a QGLWidget to the viewport does work provided I put the renderText call within the paintGL method of my test GL widget.


From the QGraphicsView docs:

To render using OpenGL, simply call setViewport(new QGLWidget). QGraphicsView takes ownership of the viewport widget.

So, in order to draw text on the view, use a QGraphicsTextItem rather than trying to draw text using the QGLWidget.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜