码迷,mamicode.com
首页 > 其他好文 > 详细

QGis(二)添加缩放漫游工具栏

时间:2014-08-13 13:07:56      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   2014   cti   log   

QGis添加图层后可以用鼠标滚动缩放,如果想添加图标到工具栏实现相同的效果,如图:

bubuko.com,布布扣

(1)在QMainWindow中添加如下变量:

QToolBar *mpMapToolBar;	///<地图操作相关的工具栏
QgsMapTool *mpPanTool;	///<漫游
QgsMapTool *mpZoomInTool;///<放大
QgsMapTool *mpZoomOutTool;///<缩小
QgsMapTool *mpZoomFull;	///<全图显示

在Qt设计师里面添加Action:

bubuko.com,布布扣

(2)然后在初始化函数里添加:

//添加工具栏上按钮
mpMapToolBar = addToolBar(tr("Tools"));
mpMapToolBar->addSeparator();
mpMapToolBar->addAction(ui.mpActionPan);
mpMapToolBar->addAction(ui.mpActionZoomIn);
mpMapToolBar->addAction(ui.mpActionZoomOut);
mpMapToolBar->addAction(ui.mpActionZoomFull);

mpPanTool= new QgsMapToolPan(mainMapCanvas);  
mpPanTool->setAction(ui.mpActionPan);  
mpZoomInTool = new QgsMapToolZoom(mainMapCanvas,false);  
mpZoomInTool->setAction(ui.mpActionZoomIn);  
mpZoomOutTool = new QgsMapToolZoom(mainMapCanvas,true);  
mpZoomOutTool->setAction(ui.mpActionZoomOut);  
(3)添加信号槽函数连接:

connect(ui.mpActionPan,SIGNAL(triggered()),this,SLOT(panMode()));  
connect(ui.mpActionZoomIn,SIGNAL(triggered()),this,SLOT(zoomInMode()));  
connect(ui.mpActionZoomOut,SIGNAL(triggered()),this,SLOT(zoomOutMode()));  
connect(ui.mpActionZoomFull, SIGNAL(triggered()), this, SLOT(zoomFull()));
(4)槽函数实现:

void MainWindow::zoomInMode()
{
	mainMapCanvas->setMapTool(mpZoomInTool);
	if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer)
	{
		return;
	}
	QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);
	pLayer->removeSelection(true);
}

void MainWindow::zoomOutMode()
{
	mainMapCanvas->setMapTool(mpZoomOutTool);
	if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer)
	{
		return;
	}
	QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);
	pLayer->removeSelection(true);
}

void MainWindow::panMode()
{
	mainMapCanvas->setMapTool(mpPanTool);
	ui.mpActionPan->setCheckable(true);
	ui.mpActionPan->setChecked(true);
	if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer)
	{
		return;
	}
	QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);
	pLayer->removeSelection(true);
}

void MainWindow::zoomFull()
{
	mainMapCanvas->zoomToFullExtent();
	if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer)
	{
		return;
	}
	QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);
	pLayer->removeSelection(true);
}
上面的mainMapCanvas就是已经加载了矢量图层的。




QGis(二)添加缩放漫游工具栏,布布扣,bubuko.com

QGis(二)添加缩放漫游工具栏

标签:style   blog   http   io   ar   2014   cti   log   

原文地址:http://blog.csdn.net/tszhangjunqiao/article/details/38518291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!