Results 1 to 8 of 8

Thread: QTableView memory consumption

  1. #1
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView memory consumption

    Hello friends,

    I try a little routine to fill up a tableview from a textfile.

    Qt Code:
    1. //**************************************************************************
    2. void Window::openFileInTable(const QModelIndex & index )
    3. //**************************************************************************
    4. {
    5.  
    6. int digits[6];
    7. int iSegCount(0);
    8. int iCopyFLines(0);
    9. QString selectedFile;
    10. if ( index.isValid())
    11. {
    12. selectedFile = model->filePath( index );
    13. selectedFile.replace(QString("/"),QString("\\"));
    14. bodyEdit->clear();
    15. bodyEdit->append(selectedFile);
    16.  
    17. QFile file(selectedFile);
    18. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    19. return;
    20. int fileLines(1);
    21. QTextStream InputDataFile(&file);
    22. while (!InputDataFile.atEnd()) {
    23. InputDataFile.readLine();
    24. fileLines++;
    25. }
    26. file.close();
    27. QTextStream InputDataFile(&file);
    28. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    29. return;
    30. QStringList buffer_list;
    31. QString Delimiter(";");
    32. tableModel = new QStandardItemModel;
    33. tableModel->setRowCount(0) ;
    34. int Max_Number_of_Columns(36);
    35. int Max_Number_of_Lines(0);
    36. tableModel->setColumnCount(Max_Number_of_Columns + 3) ;
    37. progressDialog->setMaximum(fileLines);
    38. for(iSegCount=0;iSegCount<6;iSegCount++){digits[iSegCount]=0;}
    39. QString buf;
    40. while (!InputDataFile.atEnd())
    41. {
    42. buf = InputDataFile.readLine();
    43. buffer_list = buf.split(Delimiter);
    44.  
    45. for (int column = 0; column <= Max_Number_of_Columns; column ++)
    46. {
    47. QStandardItem * item = new QStandardItem(buffer_list[column]);
    48. tableModel->setItem(Max_Number_of_Lines, column, item) ;
    49. }
    50. Max_Number_of_Lines++;
    51. progressDialog->setLabelText(tr("Transfer %1...").arg(selectedFile));
    52. progressDialog->setWindowModality(Qt::WindowModal);
    53. progressDialog->setValue(Max_Number_of_Lines);
    54.  
    55. }
    56. iCopyFLines=Max_Number_of_Lines;
    57. iSegCount = 0;
    58. do {
    59. digits[iSegCount++] = iCopyFLines % 10;
    60. } while ((iCopyFLines /= 10) > 0);
    61. progressCountEiner->setNumber(digits[0]);
    62. progressCountZehner->setNumber(digits[1]);
    63. progressCountHunderter->setNumber(digits[2]);
    64. progressCountTausender->setNumber(digits[3]);
    65. progressCountZehnTausender->setNumber(digits[4]);
    66. progressCountHundertTausender->setNumber(digits[5]);
    67.  
    68. progressDialog->setValue(fileLines);
    69. trackView->setAutoFillBackground(true);
    70. trackView->setAlternatingRowColors(true);
    71. trackView->resizeColumnsToContents();
    72. trackView->setModel(tableModel);
    73. trackView->show() ;
    74. }
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 


    What I detect is that the bigger the textfile is the greater the memory consumption is.

    The problem I think is the for loop:
    Qt Code:
    1. for (int column = 0; column <= Max_Number_of_Columns; column ++)
    2. {
    3. QStandardItem * item = new QStandardItem(buffer_list[column]);
    4. tableModel->setItem(Max_Number_of_Lines, column, item) ;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Have somebody a tip to filling up my tableview with better performance or lower memory consumption ..???

  2. #2
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView memory consumption

    What I detect is that the bigger the textfile is the greater the memory consumption is.
    I think what you detect is reasonable,doesn't if?
    Jerry

  3. #3
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView memory consumption

    What do you mean???

    When I parse a textfile with for example 150.000 lines the Applications memory usage grows up to 500-600 mb. When the tableview is filled it goes to 400 mb and still point there.

    There must be a better solution for handling big textfiles, or not???

  4. #4
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView memory consumption

    Quote Originally Posted by LordQt View Post
    What do you mean???

    When I parse a textfile with for example 150.000 lines the Applications memory usage grows up to 500-600 mb. When the tableview is filled it goes to 400 mb and still point there.

    There must be a better solution for handling big textfiles, or not???
    QString is Unicode, so 2 bytes per character. It's not the lines, but the number of characters you have.

    As you store the content of the file in the StandardItem you have it temporarily twice in memory.
    After closing the file "only" once, all in QStandardItems.

    I'm not surprised by your numbers.

    The only way is to handle the file like a "database" - read only chunks until you find the needed part - and your model is then looking temporarily for each line into the file. But with a text file this is really a timing issue.

    You may first read the file junk by junk and store it in a SQL database on disk, here the access times are faster. Still I don't think we will get wonders in performance.

  5. #5
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView memory consumption

    Thank you for yours suggestions ;O)

    but I need to check content of the File before inserting the file into Database.

    When I understand you right
    I must insert the hole file in DB then visualize the content in QTreeView then reinserts the hole content to my real DB ... hmm

    Thats inefficient isn´t it?

  6. #6
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView memory consumption

    As you don't want to start with a database, then you should do it similar like a database.

    Databases don't keep the data itself in memory, just the index to the data on a harddrive.

    In your case, you don't store the text itself, but an index to the position in the file. The index is then fast enough and not that memory consuming.

    So I would first parse the file and at each position of a new line, you store the position in an index, so later you can do:

    pos = index (row)
    data = readData(file, pos, length)

    Maybe you also store the length for faster processing.

    Then your model takes for each line through this index the data when displaying from the harddisk.

    I think this is then reasonable fast and the index takes much less memory.

  7. #7
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView memory consumption

    Is there any example out there .....

  8. #8
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView memory consumption

    QFile has no member that returns an index of a row.......????

Similar Threads

  1. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  2. Tracking memory consumption of plugins.
    By spud in forum General Programming
    Replies: 3
    Last Post: 7th September 2007, 13:14
  3. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.