Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. French
  4. Des item vide s'ajoute dans QTreeView
Qt 6.11 is out! See what's new in the release blog

Des item vide s'ajoute dans QTreeView

Scheduled Pinned Locked Moved Unsolved French
3 Posts 2 Posters 1.9k Views 1 Watching
  • 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 Offline
    S Offline
    Sabri
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      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
      0
      • M mpergand

        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 Offline
        S Offline
        Sabri
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved