MapView into QGraphicsWidget

948
4
11-17-2017 08:59 AM
JonMullen
New Contributor II

The documentation says that MapView can be derived off of to create a QGraphicsWidget; however, it doesn't explain what is required to accomplish this.  Besides implementing the two pure virtual functions, how might one go about implementing this?

So this is what I have done so far.  

I created a class that derives off of QGraphicsWidget and MapView. 

In the Paint function I call MapView::Draw.  

In the resize event I resize the MapView.

I have implemented the two pure virtual functions qObjectPointer() and drawRequestedEvent.  I'm not really sure what happens in drawRequestedEvent, so it's just stubbed out.

However, when the Graphics Widget is drawn all you see is a grey background.

Tags (2)
0 Kudos
4 Replies
LucasDanzinger
Esri Frequent Contributor

we have an example of subclassing the map view as a QGraphicsView. This differs from QGraphicsWidget, but the principle about how to extend MapView remains the same.

0 Kudos
JonMullen
New Contributor II

I followed your attached example and made the changes to work with a QGraphicsWidget; however, all i see is a grey background with the esri logo on the bottom right.  I don't see any tiles.  Have I missed anything?

MyGraphicsObject::MyGraphicsObject(QGraphicsItem *parent)
    : QGraphicsWidget(parent)
    , MapView(1024,768)
{
    Basemap*basemap = Basemap::streets(this);
    mMap = new Map(basemap, this);

    setMap(mMap);
}

void MyGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPaintEngine::Type type = painter->paintEngine()->type();
    if (!painter || !painter->isActive() || (type != QPaintEngine::OpenGL2 && type != QPaintEngine::OpenGL))
        return;

    painter->beginNativePainting();

    if (isReadyToDraw())
    {
        // workaround ArcGIS Runtime Qt bug which requires blending to be enabled before
        // each draw request
        QOpenGLFunctions* functions = QOpenGLContext::currentContext()->functions();
        functions->glEnable(GL_BLEND);

        draw();
    }

    painter->endNativePainting();
}

void MyGraphicsObject::resizeEvent(QGraphicsSceneResizeEvent * event)
{
    QGraphicsWidget::resizeEvent(event);

    sendGeoViewRectChangedEvent(QRectF(0,0, event->newSize().width(), event->newSize().height()));

    resizeView(event->newSize().width(), event->newSize().height());
}
     
void MyGraphicsObject::drawRequestedEvent()
{
    if(scene())
        scene()->update();
}

QObject* MyGraphicsObject::qObjectPointer() const
{
    return const_cast<MyGraphicsObject *>(this);
}
0 Kudos
EricBader
Occasional Contributor III

Hi Jon,

Are your OpenSSL libs in your path? You won't get a map to render if the application can't find those libs. Just wondering..

0 Kudos
JonMullen
New Contributor II

Eric,

Yes the openSSl libs are in the correct path.  Without them there are all types of qDebug messages in the console window.

Jon

0 Kudos