Marble/MarbleSignalsSlots: Difference between revisions

From KDE TechBase
Line 15: Line 15:
We'd like to add other widgets to our Marble window: A zoom slider and a label that shows the current mouse position.
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 vertical layout. Once done we add the slider and the label that we created to the layout. Also we zoom the globe to the slider's default  
In order to achieve this we need to create a vertical layout. Once done we add the slider and the label that we created to the layout. Also we zoom the globe to the slider's default value using the [http://api.kde.org/4.x-api/kdeedu/marble/classMarble_1_1MarbleWidget.html#d49d8ab0be0721c579bb511bdda45829 MarbleWidget::zoomView(int)] method.
 
We want to center our globe onto a place that is different from the default. So we create a new GeoDataCoordinates object that takes the longitude and the latitude as a parameter. In the next step MarbleWidget::centerOn is called.
 


<code cppqt>
<code cppqt>
Line 47: Line 50:
     mapWidget->setMapQuality( NormalQuality, Still );
     mapWidget->setMapQuality( NormalQuality, Still );
     mapWidget->setMapQuality( LowQuality, Animation );
     mapWidget->setMapQuality( LowQuality, Animation );
    // Center the map onto a given position
    GeoDataCoordinates home(-60.0, -10.0, 0.0, GeoDataCoordinates::Degree);
    mapWidget->centerOn(home);
      
      
     // Set default zoom and create horizontal zoom slider
     // Set default zoom and create horizontal zoom slider
Line 69: Line 68:
     layout->addWidget(positionLabel);
     layout->addWidget(positionLabel);


    // Center the map onto a given position
    GeoDataCoordinates home(-60.0, -10.0, 0.0, GeoDataCoordinates::Degree);
    mapWidget->centerOn(home);
   
     // Connect the map widget to the position label.
     // Connect the map widget to the position label.
     QObject::connect( mapWidget, SIGNAL( mouseMoveGeoPosition( QString ) ),
     QObject::connect( mapWidget, SIGNAL( mouseMoveGeoPosition( QString ) ),

Revision as of 10:09, 6 July 2010


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 vertical layout. Once done we add the slider and the label that we created to the layout. Also we zoom the globe to the slider's default value using the MarbleWidget::zoomView(int) method.

We want to center our globe onto a place that is different from the default. So we create a new GeoDataCoordinates object that takes the longitude and the latitude as a parameter. In the next step MarbleWidget::centerOn is called.


  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 Plain map
   mapWidget->setMapThemeId("earth/plain/plain.dgml");
   // Hide the FloatItems: OverviewMap, ScaleBar and Compass
   mapWidget->setShowOverviewMap(false);
   mapWidget->setShowScaleBar(false);
   mapWidget->setShowCompass(false);
   // Set the map quality to gain speed
   mapWidget->setMapQuality( NormalQuality, Still );
   mapWidget->setMapQuality( LowQuality, Animation );
   
   // Set default zoom and create horizontal zoom slider
   QSlider * zoomSlider = new QSlider(Qt::Horizontal);
   zoomSlider->setMinimum( 1000 );
   zoomSlider->setMaximum( 2400 );
   
   mapWidget->zoomView( zoomSlider->value() );
   // 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;
   layout->addWidget(mapWidget);
   layout->addWidget(zoomSlider);
   layout->addWidget(positionLabel);
   // Center the map onto a given position
   GeoDataCoordinates home(-60.0, -10.0, 0.0, GeoDataCoordinates::Degree);
   mapWidget->centerOn(home);
   
   // 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->setLayout(layout);
   window->resize( 400, 300 );
   window->show();
   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: