Difference between revisions of "Projects/Marble/MarbleGeoPainter"

Jump to: navigation, search
(Created page with 'In the previous tutorial, you saw how easy it is to embed a MarbleWidget in any Qt application: Just create a Marble::MarbleWidget, set a map theme on it and... you're done alrea...')
 
Line 1: Line 1:
 +
{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleGeoPainter}}
 +
{{TutorialBrowser|
 +
 +
series=Marble C++ Tutorial|
 +
 +
name=Hello Marble|
 +
 +
pre=[[Projects/Marble/MarbleCPlusPlus|Tutorial 1 - Hello World]]|
 +
 +
next=[[Projects/Marble/MarbleWidgetCustomized|Tutorial 3 - Customize your Marble Widget]]|
 +
}}
 +
 
In the previous tutorial, you saw how easy it is to embed a MarbleWidget in any Qt application: Just create a Marble::MarbleWidget, set a map theme on it and... you're done already.
 
In the previous tutorial, you saw how easy it is to embed a MarbleWidget in any Qt application: Just create a Marble::MarbleWidget, set a map theme on it and... you're done already.
  

Revision as of 23:22, 28 January 2010


Editing Projects/Marble/MarbleGeoPainter

Hello Marble
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 1 - Hello World
What's Next   Tutorial 3 - Customize your Marble Widget
Further Reading   n/a

In the previous tutorial, you saw how easy it is to embed a MarbleWidget in any Qt application: Just create a Marble::MarbleWidget, set a map theme on it and... you're done already.

Next we'll extend that example a bit and write our own little paint method to add some extra content to the globe. To facilitate this, Marble provides a painting hook called customPaint. It is called in between of the normal paint operations: After the background and tiles are painted, but before the top layers like float items (info boxes).

The customPaint operation is called with a GeoPainter: An extended QPainter which not only is able to paint at certain screen (pixel) positions, but also at certain geo (lat,lon) positions. We'll make use of that feature now. To keep things simple again, we just add a little 'Hello World' message indicated by a green circle.

  1. include <QtGui/QApplication>
  2. include <marble/MarbleWidget.h>
  3. include <marble/GeoPainter.h>

using namespace Marble;

class MyMarbleWidget : public MarbleWidget { public:

   virtual void customPaint(GeoPainter* painter);

};

void MyMarbleWidget::customPaint(GeoPainter* painter) {

   GeoDataCoordinates home(8.4, 49.0, 0.0, GeoDataCoordinates::Degree);
   painter->setPen(Qt::green);
   painter->drawEllipse(home, 7, 7);
   painter->setPen(Qt::black);
   painter->drawText(home, "Hello Marble!");

}

int main(int argc, char** argv) {

   QApplication app(argc,argv);
   MyMarbleWidget *mapWidget = new MyMarbleWidget;
   mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
   mapWidget->show();
   return app.exec();

}

Save the code above as my_marble.cpp and compile it:

g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui

If things go fine, execute ./my_marble and you end up with a globe view similar to this: Marble-geopainter.png


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal