2012-01-10 89 views
0

我是一名學生程序員,並且在Qt中爲我的公司構建了一個GUI。我正在寫一個成員函數SetData來充當我的Ui元素和成員變量之間的中介。在這個特殊的用戶界面中,我使用了QTableWidge。我似乎無法確切知道如何將每列設置爲一個變量。尋址QTableWidget Colums

例如,如果我的QTableWidget中有Name列,並且Name是我無法使用傳統數組參數訪問它的第一列。來自Qt的documentation對於如何解決這個問題真的很尷尬。也可能是我仍然太業餘瞭解如何使用該類的成員函數來實現我想要的結果。

澄清我想爲整個列定義數據類型。例如我的第一列;名稱,將是我創建的一個變量,它是一個QString數據類型。 SetData會將它傳遞給QVector或QList。下面是我輸入的代碼,以便給出我想我可以做什麼的想法。

void InjectionDialog::setData(InjectionData &setDataStruct) 
    { 
     /*The following setData functions assists in creating a new instance of 
      the Injection dialog with whatever values are passed to setDataStruct*/ 
     QString str;//created str to make datatype conversion 
     ui->lineEditFluidVelocity->setText(str.setNum(setDataStruct.lineEditFluidVelocity)); 
     ui->lineEditFluidMassFlow->setText(str.setNum(setDataStruct.lineEditFluidMassFlow)); 
    ui->lineEditFluidArea->setText(str.setNum(setDataStruct.lineEditFluidArea)); 
     ui->lineEditFluidTemperature->setText(str.setNum(setDataStruct.lineEditFluidTemperture)); 
     ui->lineEditFluidPressure->setText(str.setNum(setDataStruct.lineEditPressure)); 
     ui->lineEditParticleManual->setText(str.setNum(setDataStruct.lineEditManualParticlesPerCell)); 
     ui->lineEditParticleVelocity->setText(str.setNum(setDataStruct.lineEditParitcleVelocity)); 
     ui->lineEditParticleMassFlow->setText(str.setNum(setDataStruct.lineEditParticleMassFlow)); 
     ui->lineEditParticleArea->setText(str.setNum(setDataStruct.lineEditParticleArea)); 
     ui->lineEditParticleTemperature->setText(str.setNum(setDataStruct.lineEditParticleTemperture)); 
     ui->tableWidgetInjectionLocations //this is where I got stuck 
} 

我知道QTreeView可以通過委派列來設置項目,但我需要這些字段才能被編輯。我可能會在處理我的QTableWidget時遇到這個問題;如果是這樣,我很欣賞任何有關如何正確處理這個Widget的建議。

+0

QTableWidgetItem *的newitem =新QTableWidgetItem(TR( 「%1」)的Arg( (行+ 1)*(列+ 1))); tableWidget-> setItem(row,column,newItem); 這段文件中的這段代碼不能爲你做什麼嗎? – ksming 2012-01-11 03:45:16

回答

0

經過QTableWidgetItem的一些研究和試驗和錯誤;我找到了我正在尋找的東西。正如我前面所說,我需要編寫一個setData函數來提供一種方法,使用QTableWidget將每個單元設置爲某些指定的數據。 QTableWidget使用setItem將每個項目設置爲QTableWidgetItem。知道這一點我只是填補了目的。這就是我所做的;這是我的主要代碼後。

for(int i=0; i<setDataStruct.qTableWidegetlocations.size(); i++) 
      { 
       QTableWidgetItem *qTableWidgetItemInjectionName = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsInjectionName); 
       ui->tableWidgetInjectionLocations->setItem(i,0, qTableWidgetItemInjectionName); 
       QTableWidgetItem *qTableWidgetItemInjectionOnOff= new QTableWidgetItem((setDataStruct.qTableWidegetlocations[i].locationsInjectionOnOff)); 
       ui->tableWidgetInjectionLocations->setItem(i,1, qTableWidgetItemInjectionOnOff); 
       QTableWidgetItem *qTableWidgetItemInjectionX = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsX); 
       ui->tableWidgetInjectionLocations->setItem(i,2, qTableWidgetItemInjectionX); 
       QTableWidgetItem *qTableWidgetItemInjectionY = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsY); 
       ui->tableWidgetInjectionLocations->setItem(i,3, qTableWidgetItemInjectionY); 
       QTableWidgetItem *qTableWidgetItemInjectionZ = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsZ); 
       ui->tableWidgetInjectionLocations->setItem(i,4,qTableWidgetItemInjectionZ); 
       QTableWidgetItem *qTableWidgetItemInjectionnx = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsnx); 
       ui->tableWidgetInjectionLocations->setItem(i,5,qTableWidgetItemInjectionnx); 
       QTableWidgetItem *qTableWidgetItemInjectionny = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsny); 
       ui->tableWidgetInjectionLocations->setItem(i,6,qTableWidgetItemInjectionny); 
       QTableWidgetItem *qTableWidgetItemInjectionnz = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsnz); 
       ui->tableWidgetInjectionLocations->setItem(i,7,qTableWidgetItemInjectionnz); 
       QTableWidgetItem *qTableWidgetItemInjectionTemperature = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsTemperature); 
       ui->tableWidgetInjectionLocations->setItem(i,8,qTableWidgetItemInjectionTemperature); 
       QTableWidgetItem *qTableWidgetItemInjectionWeight = new QTableWidgetItem(setDataStruct.qTableWidegetlocations[i].locationsWeight); 
     } 

感謝閱讀