使用自定义QGLWidget调用Qt的QGraphicsView :: setViewport [英] calling Qt's QGraphicsView::setViewport with a custom QGLWidget
问题描述
我以前是从QGLWidget派生的,就像这样:
I've derived from QGLWidget before, like so:
class MyGLWidget : public QGLWidget
{
public:
// stuff...
virtual void initializeGL() { /* my custom OpenGL initialization routine */ }
// more stuff...
};
但是,我发现如果尝试使用自定义QGLWidget作为视口初始化QGraphicsView,则不会调用initializeGL(在Qt库中设置断点,创建纯文本时QGLWidget :: initializeGL()也不会)
However, I find that if I try to initialize a QGraphicsView with my custom QGLWidget as the viewport, initializeGL doesn't get called (setting a breakpoint within the Qt library, neither does QGLWidget::initializeGL() when created plain).
// initializeGL, resizeGL, paintGL not called
ui.graphicsView->setViewport(new MyGLWidget(QGLFormat(QGL::DoubleBuffer)));
// initializeGL, resizeGL, paintGL *still* not called
ui.graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer)));
当前将代码驻留在MyGLWidget :: initializeGL()中的正确位置在哪里?
Where is the correct location to place the code that currently resides in MyGLWidget::initializeGL()?
推荐答案
我将继续回答我自己的问题.这不是最佳选择,但这就是我解决问题的方法.
I'm going to go ahead and answer my own question. This isn't optimal, but this is how I've gotten around the problem.
代替
ui.graphicsView->setViewport(new MyGLWidget(QGLFormat(QGL::DoubleBuffer)));
我得到了这个
ui.graphicsView->setViewport(new QGLWidget(new CustomContext(QGLFormat(QGL::SampleBuffers))));
CustomContext是从QGLContext派生的类.我已经覆盖了create成员,就像这样:
CustomContext is a class that derives from QGLContext. I've overridden the create member, like so:
virtual bool create(const QGLContext *shareContext = 0)
{
if(QGLContext::create(shareContext))
{
makeCurrent();
/* do my initialization here */
doneCurrent();
return true;
}
return false;
}
我不认为这是实现此目的的最佳方法,但是它比完全没有特定初始化步骤的替代方法要好.我仍然很高兴有人能留下更好的答案!
I don't think this is the optimal way to do this, but it's better than the alternative of not having a specific initialization step at all. I'd still be happy to have someone leave a better answer!
这篇关于使用自定义QGLWidget调用Qt的QGraphicsView :: setViewport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!