Select to view content in your preferred language

MapView into QGraphicsWidget

1739
6
11-17-2017 08:59 AM
JonMullen
Emerging Contributor

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
6 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
Emerging Contributor

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
Honored Contributor

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
Emerging Contributor

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
AntonAAA
New Contributor

Hi @LucasDanzinger . How do I inherit from Map View and override the methods so that the exportImageAsync() method works - ArcGIS Qt SDK 200.6?

My impl - not work - exportImageAsync() - QFuture - never finished

class MyMapView : public QObject, public Esri::ArcGISRuntime::MapView
{
Q_OBJECT
Q_INTERFACES(Esri::ArcGISRuntime::MapView Esri::ArcGISRuntime::GeoView)
 
public:
MyMapView() 
: QObject()
, Esri::ArcGISRuntime::MapView(500,500)
{
 
}
 
void drawRequestedEvent() override
{
 
}
 
QObject* qObjectPointer() const override
{
return const_cast<MyMapView*>(this);
}
};
 
//main
//portal init ....
map_ = new Esri::ArcGISRuntime::Map(new Esri::ArcGISRuntime::PortalItem("db3ec1e50d654594acdb6294b43f3bf1", this));
view_ = new MyMapView;
 
view_->setMap(map_);
 
//load data ...
 
QFuture<QImage> f = view_->exportImageAsync();
 
f.then([this](QImage img) {
//Never comes in
}).onFailed(this, [](Esri::ArcGISRuntime::ErrorException ex) {
QString h = ex.error().message();
}).onCanceled([]() {
 
});
 
With MapGraphicsView - Everything works
I just need a picture without creating and showing a GUI.
Thanks.
0 Kudos
LucasDanzinger
Esri Frequent Contributor

I believe the exportImage is going to require a MapView to draw on screen for it to work. Can you provide details on your workflow as to why the shown pattern is necessary for your use case so we could better understand?

0 Kudos