Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Performance of QTreeView

Performance of QTreeView

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.8k Views
  • 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.
  • M Offline
    M Offline
    mbnoimi
    wrote on last edited by
    #1

    Hi,

    I want to use QTreeView with database so I created a model then called QSqlQuery for passing it to QStandardItemModel. It worked fine but I'm afraid from its performance because it loads all the data at once which may form a problem in case of slow connection or big data (millions of records).

    On other hand I've another solution for performance issue which is using QTreeWidget and fetching the seen items only instead of loading all the data of the tree. But I'm not sure if this is a right solution because I trust in Qt model performance rather than writing the code from scratch by myself.

    Which solution is better using QTreeView with model and fetching all the data at once or using QTreeWidget and fetching the data for seen items only?

    Snippet of QTreeView soluition (I didn't write QTreeWidget solution yet because I didn't decide which one is better):
    @void AccountingTree::createModel()
    {
    QSqlQuery q("SELECT acc_id, acc_parent_id, acc_number, acc_name, acc_level_index "
    "FROM fin_accounts "
    "ORDER BY acc_level1, acc_level2, acc_level3, acc_level4, acc_level5, acc_level6, acc_level7",
    pDB);
    q.first();

    QList <QStandardItem *> rowItems;
    QStandardItemModel *m = new QStandardItemModel(this);
    m->setColumnCount(2);
    m->setHeaderData(0, Qt::Horizontal, "Numbers");
    m->setHeaderData(1, Qt::Horizontal, "Accounts");
    
    QString name = q.value("acc_name").toString();
    QString id = q.value("acc_id").toString();
    QString parentId = q.value("acc_parent_id").toString();
    int levleIndex = q.value("acc_level_index").toInt();
    QString number = q.value("acc_number").toString();
    
    QString lastParentId = parentId;
    int lastLevel = levleIndex;
    
    QStandardItem *parent = new QStandardItem(name);
    parent->setData(id);
    rowItems<<parent<<new QStandardItem(number+"Hi");
    m->appendRow(rowItems);
    
    int counter = 0;
    int rowIndex = 0;
    while (q.next()){
        name = q.value("acc_name").toString();
        id = q.value("acc_id").toString();
        parentId = q.value("acc_parent_id").toString();
        levleIndex = q.value("acc_level_index").toInt();
        number = q.value("acc_number").toString();
    
        QStandardItem *itemColumn1 = new QStandardItem(name);
        itemColumn1->setData(id);
    
        if (parentId.isNull()){
            parent = itemColumn1;
            m->appendRow(parent);
        }
        else if ((parentId != lastParentId)&&(levleIndex >= lastLevel)){
            parent->setChild(parent->rowCount(), itemColumn1);
            parent = itemColumn1;
        }
        else if ((parentId != lastParentId)&&(levleIndex < lastLevel)){
            int count = (lastLevel - levleIndex) + 1;
            while (count > 0){
                parent = parent->parent();
                --count;
            }
            parent->setChild(parent->rowCount(), itemColumn1);
            parent = itemColumn1;
        }
        else if (parentId == lastParentId){
            parent = parent->parent();
            parent->setChild(parent->rowCount(), itemColumn1);
            parent = itemColumn1;
        }
        lastParentId = parentId;
        lastLevel = levleIndex;
        ++counter;
    }
    qDebug()<<counter;
    ui->treeView->setModel(m);
    ui->treeView->resizeColumnToContents(0);
    

    }@

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      Hi,

      You have already a post refering that. Please go to this link and check pros and cons.
      "http://www.qtcentre.org/threads/10040-QTreeWidget-or-QTreeView":http://www.qtcentre.org/threads/10040-QTreeWidget-or-QTreeView

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #3

        [quote author="bjanuario" date="1370381908"]Hi,

        You have already a post refering that. Please go to this link and check pros and cons.
        "http://www.qtcentre.org/threads/10040-QTreeWidget-or-QTreeView":http://www.qtcentre.org/threads/10040-QTreeWidget-or-QTreeView[/quote]

        The link above didn't prove anything practical it's just an opinion without any certain fact!!!

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bjanuario
          wrote on last edited by
          #4

          Hi,
          Maybe is not quit clear, but if u need to use QStandardItemModel so the way you are doing is the best way to do it :)

          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