在使用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&)));
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 }
此處為加載小區數據文件的槽函數。里面包括了數據文件的讀取和處理(widget的寫入)