在QGraphicsView上显示3D模型的正确方法是什么?

时间:2011-10-19 09:46:10

标签: opengl qt4 qgraphicsview

我想在该图像的顶部显示图像和3D模型。到目前为止,我已使用QGraphicsView显示QGraphicsScene,其中我将图片添加为QGraphicsPixmapItem

QGraphicsView设置在:

ImageResourceWidget::ImageResourceWidget(ImageResource* resource, QWidget* parent) :
    QWidget(parent),
//QGraphicsScene m_scene;
//QGraphicsView m_view;
    m_view(&m_scene),
    m_pixmapItem(QPixmap::fromImage(resource->qimage())),
    m_zoom(1.0)
{
    QVBoxLayout* layout = new QVBoxLayout;

    layout->addWidget(&m_view);
    setLayout(layout);

    QGLWidget* qgl=new QGLWidget(new QGLContext(QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::AlphaChannel | QGL::HasOverlay)));
    m_view.setViewport(qgl);

    _updateScene();

    connect(m_resource, SIGNAL(si_updateImage()), SLOT(updateImage()));
}

我尝试通过覆盖QGraphicsView中的paintEvent来绘制ImageResourceWidget之上:

void ImageResourceWidget::paintEvent(QPaintEvent* event)
{
    std::cout << __PRETTY_FUNCTION__ << " -- paintEvent called." << std::endl;
    QPainter painter(this);
    painter.fillRect(0, 0, 128, 128, Qt::green);

//    from: http://doc.qt.nokia.com/4.7-snapshot/qpainter.html#beginNativePainting
//    painter.beginNativePainting();

//    glEnable(GL_SCISSOR_TEST);
//    glScissor(0, 0, 64, 64);

//    glClearColor(1, 0, 0, 1);
//    glClear(GL_COLOR_BUFFER_BIT);

//    glDisable(GL_SCISSOR_TEST);
//    painter.endNativePainting();

      // For testing purposes:
      QPaintEngine::Type pType = m_view.paintEngine()->type(); // pType = QPaintEngine::X11, was expecting QPaintEngine::OpenGL(2)
      QPainter* p = m_view.paintEngine()->painter();           // p = 0x0, was expecting some other value
}

这将在QGraphicsView 后面绘制一个绿色矩形

This website建议继承QGraphicsScene,但是,我不希望3d模型成为场景的一部分。换句话说,3d模型不应受场景或视图中的变换的影响。它应该只悬停在QGraphicsView顶部

我也试过子类QGraphicsView(我认为这似乎是理想的方式),但是当我覆盖该类中的paintEvent时,不再显示任何内容,我收到了警告:QPainter::begin: Widget painting can only begin as a result of a paintEvent

void MyGraphicsView::paintEvent(QPaintEvent* event) // inherits QGraphicsView
{
    this->QGraphicsView::paintEvent(event);
    std::cout << __PRETTY_FUNCTION__ << " -- paintEvent called." << std::endl;

    QPainter painter(this);
    painter.fillRect(0, 0, 128, 128, Qt::green);

    // For testing purposes:
    QPaintEngine::Type pType = paintEngine()->type(); // pType = QPaintEngine::X11, was expecting QPaintEngine::OpenGL(2)
    QPainter* p = paintEngine()->painter();           // p = 0x0, was expecting some other value
}

更新

  • 我注意到paintEngine()->type()返回QPaintEngine::X11,在QPaintEngine::OpenGL之后期待m_view.setViewport(new QGlWidget(...))
  • paintEngine()->painter()返回0x0,我希望能够接触画家,所以我可以在同一个表面上画画。

1 个答案:

答案 0 :(得分:0)

您可能希望直接在视口而不是QGraphicsView上绘制。我想视口是包含实际场景表示的内容。 在任何情况下,我都无法弄清楚为什么你会得到油漆事件错误。