2012-01-10 4 views
0

Я ученик программиста и строил GUI в Qt для своей компании. Я пишу функцию-член SetData, чтобы в основном выступать в качестве посредника между моими элементами Ui и переменными-членами. В этом конкретном Ui я использую QTableWidge. Я не могу точно узнать, как установить каждый столбец в переменную.Адресация QTableWidget Colums

Например, если у меня есть имя столбца в моем QTableWidget, а Name - это первый столбец, я не могу получить к нему доступ с использованием традиционных параметров массива. documentation от Qt действительно туманно относится к тому, как решить эту проблему. Возможно также, что я все еще слишком любитель, чтобы понять, как использовать функции-члены класса для достижения результатов, которые я хочу здесь.

Чтобы уточнить, я пытаюсь определить тип данных для всего столбца. Например, моя первая колонка; Имя, будет переменной, которую я создал, которая является типом данных 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; если да, то я ценю любой совет относительно того, как правильно обрабатывать этот Виджет.

+0

QTableWidgetItem * newItem = новый QTableWidgetItem (тр ("% 1") Arg ( (строка + 1) * (колонка + 1)).); tableWidget-> setItem (строка, столбец, newItem); Есть ли что-нибудь, что этот кусок кода из этого документа не может сделать для вас? – ksming

ответ

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); 
     } 

спасибо за чтение

Смежные вопросы