PDA

View Full Version : QTreeView memory consumption



eyeofhell
18th May 2010, 16:01
Hello.

I'm testing QTreeView functionality right now, and i was amazed by one thing. It seems that QTreeView memory consumption depends on items count O_O. This is highly unusual, since model-view containers of such type only keeps track for items being displayed, and rest of items are in the model. I have written a following code with a simple model that holds no data and just reports that it has 10 millions items. With MFC, Windows API or .NET tree / list with such model will take no memory, since it will display only 10-20 visible elements and will request model for more upon scrolling / expanding items. But with Qt, such simple model results in ~300Mb memory consumtion. Increasing number of items will increase memory consumption. Maybe anyone can hint me what i'm doing wrong? :)



#include <QtGui/QApplication>
#include <QTreeView>
#include <QAbstractItemModel>

class CModel : public QAbstractItemModel
{
public: QModelIndex index
(
int i_nRow,
int i_nCol,
const QModelIndex& i_oParent = QModelIndex()
) const
{
return createIndex( i_nRow, i_nCol, 0 );
}

public: QModelIndex parent
(
const QModelIndex& i_oInex
) const
{
return QModelIndex();
}

public: int rowCount
(
const QModelIndex& i_oParent = QModelIndex()
) const
{
return i_oParent.isValid() ? 0 : 1000 * 1000 * 10;
}

public: int columnCount
(
const QModelIndex& i_oParent = QModelIndex()
) const
{
return 1;
}

public: QVariant data
(
const QModelIndex& i_oIndex,
int i_nRole = Qt::DisplayRole
) const
{
return Qt::DisplayRole == i_nRole ? QVariant( "1" ) : QVariant();
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView oWnd;
CModel oModel;
oWnd.setUniformRowHeights( true );
oWnd.setModel( & oModel );
oWnd.show();
return a.exec();
}

eyeofhell
24th May 2010, 10:29
Bump. Still can't find out how to correctly use QAbstractItemModel + QTreeView in order to prevent gigabytes of memory consumption. Will appreciate any help.

tbscope
24th May 2010, 11:10
Can you explain how you measured the memory consumption?
Did you try the QStandardItemModel too?

eyeofhell
24th May 2010, 15:28
Memory consumption is measured via task manager :). Run app without connected model = 20mb, run app with connected model = 320mb O_O.
As far as i understand, using QSTandardItemModel will effectively reduce QTreeView to QTreeWidget - i will not be able to supply data dynamically as needed. For SQL queries that can be millions of records this will be a bit slow :(.

tbscope
24th May 2010, 19:38
Memory measurement via the task manager is like asking a baby the yield of a certain nuclear bomb :-)
It doesn't tell you the real information you want.

Nevertheless I believe you when your program starts using more and more memory. I also enjoyed using the list and tree widgets of windows.

Your understanding that using QStandardItemModel will reduce QTreeView to QTreeWidget isn't correct. QTreeWidget manages the items itself. Using a QTreeView together with QStandardItemModel changes nothing. It's a standard view and a standard model.

For SQL queries I suggest you look into one of the QSql...Model classes. Try it first with one of these and see if you still get big memory consumptions.

For a personal project I need to display thousands of items from a database in a treeview, and the memory consumption is minimal.

eyeofhell
25th May 2010, 08:52
It seems that if i replace QTreeView with QTableView in my example code memory is not consumed anymore O_O. So it seems that QTreeView is just not intended to be used for very huge amounts of data and QTableView must be used instead.

dw
19th November 2010, 02:04
For a personal project I need to display thousands of items from a database in a treeview, and the memory consumption is minimal.

Then maybe you could help me with some ideas.

I also have a big QTreeView and its model gets the display data from an sqlite database. So when the tree is drawed 2 queries are executed per row to get the data. This gives the view an annoying delay (for scrolling, expanding a branch etc.). Also when I have the database in the memory. If I use any styling on it that doubles the delay.

Didn't you have this problem? Should I initialize the model by reading everything from the database and building a hierarchical structure to the memory? Any other idea to improve performance?