Performance of QTreeView



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

    }@



  • 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 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!!!



  • 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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.