zoukankan      html  css  js  c++  java
  • qt环境下地图数据的导入与显示

    在使用mapx控件时,需要从本地导入小区基站数据,并在地图中显示出来,另外在窗口中将导入的小区数据写入到tablewidget中。基本就是这么个流程。

    首先是mapx自定义工具消息的响应:当点击地图图元坐标时,弹出对话框,显示小区的名字

    ToolUsed 事件在用户使用地图上的定制工具时调用。此过程允许您确定如何使用工具,例如,它将告知您用户点击何处的地图坐标。在 ToolUsed 过程之内,您编写代码以执行工具的既定功能。此事件在使用标准工具时也将调用,此时事件在用户交互之后调用,但是在为标准功能执行的操作之前。

    下面是ToolUsed事件的槽函数

    1     QObject::connect(widget, SIGNAL(ToolUsed(int, double, double, double, double, double, bool, bool, bool&)),
    2                      this, SLOT(OnToolUsed(int, double, double, double, double, double, bool, bool, bool&)));
    View Code
    ToolUsed(int, double, double, double, double, double, bool, bool, bool&)

    ToolNum 整数:表示工具号。请注意如果您创建多个自定义工具,此事件将在使用任意这些工具时调用,因此,您需要检查 ToolNum 以确定正在使用的工具。
    x1, y1 双击:用户点击的地图坐标。
    x2, y2 双击:用户结束工具使用的地图坐标;不适用于点工具,后者不允许拖放。
    distance 双击:开始位置和结束位置之间的距离的地图单位。
    shift 布尔值:表示用户是否按住了 SHIFT 键。
    control 布尔值:表示用户是否按住了 CTRL 键。
    *EnableDefault 布尔值:如果使用的是标准工具,则此参数控制 MapX 是否允许工具的标准行为生效。默认值为 True。要避免工具的正常行为(例如取消用户操作),可将此参数设置为 False。

    上面是mapx自带的信号。详细可以参考网上的开发人员手册

      1 //加载文件按钮响应槽
      2 void MainWindow::OnLoadFile_Clicked(){
      3     QStringList RowLabels;
      4 
      5     //qDebug()<<"clicked";
      6     QString StrFile = QFileDialog::getOpenFileName(this,
      7                                                    tr("Open File"),
      8                                                    ".",
      9                                                    tr("Text File(*.csv)"));
     10     if(!StrFile.isEmpty()){
     11         QFile f(StrFile);
     12         if(!f.open(QIODevice::ReadOnly | QIODevice::Text)){
     13             return ;
     14         }
     15         //将文件数据写进文本流中
     16         QTextStream stream(&f);
     17         //获取表头字符串
     18         QString head = stream.readLine();
     19         //qDebug()<< head;
     20         //将表头字符串以‘,’分割,存入链表
     21         QStringList fields = head.split(",");
     22         //向tablewidget 表头中写一行数据
     23         for(int i = 0; i < fields.size(); ++i){
     24             RowLabels<<fields[i];
     25         }
     26         //设置Table的显示方式
     27         //pTableWidget->setVerticalHeaderLabels(RowLabels);//列表头
     28         pTableWidget->setHorizontalHeaderLabels(RowLabels);//行表头
     29         pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//行选中
     30         pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//双击不可编辑单元格
     31         //pTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//根据需要自动显示/*没有效果*/
     32         //pTableWidget->setAutoScroll(true);//根据需要自动显示/*没有效果*/
     33         pTableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");//设置表头颜色
     34         pTableWidget->setRowCount(fields.size());
     35         //qDebug()<< fields.size();
     36 
     37 
     38         int iCellCnName = fields.indexOf("CellNameCN");
     39         int iLongitude = fields.indexOf("Longitude");
     40         int iLatitude = fields.indexOf("Latitude");
     41         int iAzimuth = fields.indexOf("Azimuth");
     42         if(-1 == iCellCnName | -1 == iLongitude | -1 == iLatitude | -1 == iAzimuth ){
     43             f.close();
     44             return;
     45         }
     46         CMapXFeatureFactory *pFactory = pCMapX->FeatureFactory();
     47         CMapXLayers *pLayers = pCMapX->Layers();
     48         CMapXLayer *pLayer = pLayers->CreateLayer("test");
     49         //没有下面这两句不会显示label
     50         pLayer->SetAutoLabel(true);//详见手册
     51         pLayer->LabelProperties()->SetParallel(false);
     52 
     53         pLayer->BeginAccess(miAccessReadWrite);
     54         QString strRow;
     55 
     56         while (!stream.atEnd()) {
     57             pTableWidget->insertRow(1);//读一行添加一行
     58             strRow = stream.readLine();
     59             QStringList values = strRow.split(",");
     60             qDebug()<<strRow;
     61             //往WIDGET中写入item
     62             for(int icloum = 0; icloum< values.size(); ++icloum){
     63 
     64                 QTableWidgetItem *Item = new QTableWidgetItem;
     65                 Item->setText(values[icloum]);
     66                 Item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
     67                 pTableWidget->setItem(1,icloum,Item);
     68             }
     69 
     70             QString strName = values[iCellCnName];
     71             double dLongitude = values[iLongitude].toDouble();
     72             qDebug()<<"12315435634"<<dLongitude;
     73             double dLatitude = values[iLatitude].toDouble();
     74             int nAzimuth = values[iAzimuth] == "" ? 0 : values[iAzimuth].toInt();
     75 
     76             //画饼形pA坐标(dLongitude+0.001*sin(Pi*nAzimuth/360.0),dLatitude + 0.001*cos(Pi*nAzimuth/360.0))
     77             //pB坐标(dLongitude+0.001*sin(Pi*nAzimuth/120.0),dLatitude + 0.001*cos(Pi*nAzimuth/120.0))
     78             //原点坐标O(dLongitude,dLatitude)
     79 
     80             CMapXFeature *pLine = pFactory->CreateLine();//画线
     81             QAxObject *pObj = new QAxObject;
     82             pObj->setControl("MapX.Points.4");
     83             CMapXPoints *pPoints = new CMapXPoints(pObj->asVariant().value<IDispatch*>());
     84 
     85             pPoints->AddXY(dLongitude,dLatitude);
     86             double lng1 = dLongitude + 0.001 * sin(Pi * nAzimuth / 180.0);
     87             double lat1 = dLatitude + 0.001 *cos(Pi *nAzimuth / 180.0);
     88             pPoints->AddXY(lng1,lat1);
     89             pLine->SetKeyValue(strName);
     90 
     91             pLine->Style()->SetLineColor(miColorBlue);
     92             pLine->Style()->SetLineWidthUnit(miStyleUnitTenthsOfPoint);
     93             pLine->Style()->SetLineWidth(20);
     94 
     95             CMapXParts *pParts = pLine->Parts();
     96             pParts->Add(pPoints->asVariant().value<IDispatch*>());
     97             pLayer->AddFeature(pLine->asVariant().value<IDispatch*>());
     98         }
     99         pCMapX->SetBounds(pLayer->Bounds());
    100         pLayer->EndAccess();
    101         f.close();
    102     }
    103     else{
    104         return ;
    105     }
    106 
    107 }
    View Code

    此处为加载小区数据文件的槽函数。里面包括了数据文件的读取和处理(widget的写入)

    作者:first_semon
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题,欢迎交流
  • 相关阅读:
    第四次作业
    第三次
    第十次作业
    第九次作业
    第八次作业
    10.29第七次
    15
    14
    13 this
    12 电视机
  • 原文地址:https://www.cnblogs.com/first-semon/p/5949992.html
Copyright © 2011-2022 走看看