< Marble
Revision as of 09:55, 6 July 2010 by Tackat (talk | contribs) (Created page with '{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleCPlusPlus}} {{TutorialBrowser| series=Marble C++ Tutorial| name=Basic interaction with MarbleWidget| pre=...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Editing Projects/Marble/MarbleCPlusPlus

Basic interaction with MarbleWidget
Tutorial Series   Marble C++ Tutorial
Previous   C++, Qt
What's Next   Tutorial 4 - Marble's GeoPainter
Further Reading   n/a

Creating a window with controls

We'd like to add other widgets to our Marble window: A zoom slider and a label that shows the current mouse position.

In order to achieve this we need to create a

  1. include <QtGui/QApplication>
  1. include <QtGui/QLayout>
  2. include <QtGui/QSlider>
  3. include <QtGui/QLabel>
  1. include <marble/MarbleWidget.h>

using namespace Marble;

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

   QApplication app(argc,argv);
   QWidget *window = new QWidget;
   // Create a Marble QWidget without a parent
   MarbleWidget *mapWidget = new MarbleWidget();
   // Load the Atlas map
   // Hide the FloatItems: Compass and StatusBar
   // Set the map quality to gain speed
   mapWidget->setMapQuality( NormalQuality, Still );
   mapWidget->setMapQuality( LowQuality, Animation );
   // Center the map onto a given position
   GeoDataCoordinates home(-60.0, -10.0, 0.0, GeoDataCoordinates::Degree);
   // Set default zoom and create horizontal zoom slider
   mapWidget->zoomView( 1000 );
   QSlider * zoomSlider = new QSlider(Qt::Horizontal);
   zoomSlider->setMinimum( 1000 );
   zoomSlider->setMaximum( 2400 );
   // Create a label to show the geodetic position
   QLabel * positionLabel = new QLabel();
   positionLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
   // Add all widgets to the vertical layout.
   QVBoxLayout *layout = new QVBoxLayout;
   // Connect the map widget to the position label.
   QObject::connect( mapWidget, SIGNAL( mouseMoveGeoPosition( QString ) ),
                     positionLabel, SLOT( setText( QString ) ) );
   // Connect the zoom slider to the map widget and vice versa.
   QObject::connect( zoomSlider, SIGNAL( valueChanged(int) ),
                     mapWidget, SLOT( zoomView(int) ) );
   QObject::connect( mapWidget, SIGNAL( zoomChanged(int) ),
                     zoomSlider, SLOT( setValue(int) ) );
   window->resize( 400, 300 );
   return app.exec();


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

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

If things go fine, execute ./marble_control and you end up with a map application that displays our globe with a zoom slider below: Marble control.png

Content is available under Creative Commons License SA 4.0 unless otherwise noted.