Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Des item vide s'ajoute dans QTreeView

    French
    2
    3
    555
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sabri last edited by

      Bonjour, j'ai essaye de crée une arbre dynamiquement j'utilise QTreeView et QStandardItemModel. j'arrive à crée mon arbre mais j'ai toujours un item vide qui s'ajoute à la fin.
      Example de l'arbre que je veux la criée :
      -A (index: 0,0)
      |
      b (index: 0,0)

      mais j'ai l'affichage suivant :
      -A (index: 0,0)
      |
      b (index: 0,0)
      |
      (index: 1,0)

      Voici un exemple des fonctions de la création son les suivants :

      void Model::update(QStandardItemModel * model)
          {
              layoutAboutToBeChanged();
                  if (model->rowCount() != 0)
                  {
                      QList<QStandardItem*> items;
                      for (int i = 0; i < model->rowCount(); ++i)
                      {
                          items.append(model->takeItem(i, 0));
                      }
      
                      QList<QStandardItem*> items2;
                      for (int i = 0; i < rowCount(); ++i)
                      {
                          items2.append(item(i, 0));
                      }
                      if (items2.size() < items.size())
                      {
                          int first = items2.size();
                          int last = items.size();
                          for (int i = items2.size(); i < items.size(); ++i)
                          {
                                  appendRow(items.at(i));
                          }
                      }
      
                      for (int i = 0; i <  items2.size(); ++i)
                      {
                          updateSection(items2.at(i), items.at(i));
                      }
              }
                  layoutChanged();
          }
      
          void Model::updateSection(QStandardItem * actualSection, QStandardItem * newSection, QStandardItem* parent)
          {
                  QList<QStandardItem*> actualItems;
                  for (int i = 0; i < actualSection->rowCount(); ++i)
                  {
                      actualItems.append(actualSection->child(i, 0));
                  }
      
                  QList<QStandardItem*> newItems;
                  int r = newSection->rowCount();
                  for (int i = 0; i < newSection->rowCount(); ++i)
                  {
                      newItems.append(newSection->takeChild(i, 0));
                  }
      
                  if (actualItems.size() < newItems.size())
                  {
                      QList<QStandardItem*> toBeInserted;
                      for (int i = actualItems.size(); i < newItems.size(); ++i)
                      {
                          toBeInserted.append(newItems.takeAt(i));
                      }
                      actualSection->appendRows(toBeInserted);
                  }
      
                  int i = 0;
                  while (i < sizeMax)
                  {
                              updateSection(actualItems.at(i), newItems.at(i));
                      ++i;
                  }
      
              }
              else {
                  if (actualSection->rowCount() != 0)
                  {
                      actualSection->removeRows(0, actualSection->rowCount());
                  }
          }
      
      

      Merci!

      1 Reply Last reply Reply Quote 0
      • M
        mpergand last edited by

        Salut,
        Sur un forum français (aujourd'hui fermé), quelqu'un avait un prob similaire, j'avais fait un petit exemple:

        int main(int argc, char *argv[])
        {
        
           QApplication app(argc, argv);
        
          QTreeView *tree = new QTreeView();
        QStandardItemModel model;
        
        for(int l=0; l<2; l++)
            {
            //QStandardItem *parentItem = model.invisibleRootItem();
            QStandardItem *item = new QStandardItem(QString("item %0").arg(l));
            QStandardItem *sub = new QStandardItem(QString("sub %0").arg(l));
            QList<QStandardItem*> cols; // list des cols
            cols<<sub;
        
            for( int c=0; c<2; c++)
                cols<< new QStandardItem(QString("%0-%1").arg(l).arg(c));
        
            item->appendRow(cols);
        
            cols.clear();
            cols<<item;
        
            for( int c=0; c<2; c++)
                cols<< new QStandardItem(QString("%0-%1").arg(l).arg(c));
        
            model.appendRow(cols);
            }
        
          tree->setModel( &model );
          tree->setGeometry(500,500,400,400);
          tree->setWindowTitle("Entrainement");
          tree->show();
        
        return app.exec();
        }
        

        Hope it helps ;)

        S 1 Reply Last reply Reply Quote 0
        • S
          Sabri @mpergand last edited by

          @mpergand Merci pour votre réponse! est ce qu'il existe une solution pour ce problème ?

          1 Reply Last reply Reply Quote 0
          • First post
            Last post