2012-01-23 23 views
0

私は現時点では、職場でGUIインターフェイスを構築するためにQtを使用している学生プログラマです。現在、Qt Documentation On the QTreeWidgetItemの解決策を見つける問題に就いています。私は現在、QTreeのインスタンスを編集、削除、コピーするためのボタンを持つインターフェースを持っています。私の木に人口がどのように分布しているかを理解することが重要です。 QTreeに表示される項目は、このようにベクトルから動的に追加されます。QTreeWidgetItem:選択した項目を取得するにはどうすればよいですか?

void InjectionGUI::addInjections_Clicked() //creates a new instance of injections 
{ 
    InjectionDialog newAddInjectionDialog; //where my dialog opens for user input 
    InjectionData defaultValues; 
    newAddInjectionDialog.setData(defaultValues); 
      if(newAddInjectionDialog.exec() == QDialog::Accepted)//a check data returns either Accepted or rejected 
      { 
       qTableInjectionData.append(newAddInjectionDialog.transInjectionData); //this appends the valid data from the injection dialog to the vector qTableInjectionData 
       ui->injectionTreeWidget->clear(); 
       for (int i=0; i < qTableInjectionData.size(); i++) // here I add the data from the vector to the tree widget. 
        { 
        InjectionData temp = qTableInjectionData.at(i); 
         QString injectionType; 
         QString tmpStr; 
         int column = 0; 

         //need sorting solution(still working on this) 
         if(temp.particleInjectionActive == true) // this evaluates the injection types 
         { 
          if(temp.particleInjectionOrLiquidDroplets == true) 
          { 
           injectionType += "(LD)"; 
          } 
          else 
          { 
           injectionType += "(P)"; 
          } 
         } 
         if(temp.fluidInjectionActive == true) 
         { 
          injectionType += "(F)"; 
         } 
         QTreeWidgetItem *qTreeWidgetItemInjectionData = new QTreeWidgetItem(ui->injectionTreeWidget); //Here data is added into rows from each instance of injection dialog found in vector 
         qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(i)); 
         qTreeWidgetItemInjectionData->setText(column++, temp.lineEditInjectionName); 
         qTreeWidgetItemInjectionData->setText(column++, injectionType); 
         qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditParitcleVelocity)); 
         qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditFluidVelocity)); 
         qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditParticleMassFlow)); 
         qTreeWidgetItemInjectionData->setText(column++, tmpStr.setNum(temp.lineEditFluidMassFlow)); 
         qTreeWidgetItemInjectionData->setText(column++, temp.lineEditComment); 
        } 
      } 
} 

は今、私は本当に私はそれがベクトルから削除することができますように、ユーザーがQTreeで選択した項目を見つけるための方法が必要です。私の擬人は、どの行が選択されたのかを識別し、削除を確認し、itemAt(項目を選択)を削除し、ID列を再割当てします。各インスタンスにはこの列の数値が割り当てられているためです。私はこれを見ていたpost、3年前に投稿;それは主に私がすでにレビューしているドキュメントを参照するだけです。さらに、選択された答えは、他の答えが適切な軌道上にあるように見えるので、決して決定的ではないようです。私はこの答えが私の目の前にあるかもしれないことを理解しています。 noobsがnoobsになり、実装を理解するのに苦労しています。私はこの仕事を学び、達成することに興味があるので、生産的なフィードバックを残してください。ありがとうございました。

答えて

3

あなたは、選択したインデックス、selectionModelを取得する必要がありますし、それらを反復:

treeWidget->selectionModel()->selection(); 
    auto idx = sel.indexes(); 
    foreach(auto index, idx) { 
    camModel_->removeRow(index.row()); 
    } 
} 

selectionModelQAbstractItemModelです。 C++ 11 autoに注意してください。

+0

これを試してみましょう。多分私は他の関数のために再利用できるように、選択された項目を返すために独立したconnect文を用意するべきでしょうか? @WylieCoyoteSG。 –

+1

私を教えてください。 '独立コネクトステートメント(independent connect statement) 'とは何ですか? – pmr

+0

私は、使用、削除、コピー、編集したい機能ごとにボタンを持っています。これらのそれぞれはconnect文を持っていますが、おそらく私はツリーウィジェットのItemをクリック/選択するための別のconnect文を用意する必要がありますか?何かが選択項目を選択された項目へのポインタを返すポインタ関数に接続するようなものです。コピー、編集、および削除のconnect文を使用します。 *選択されたアイテムを取って、何かをする。 –