Hi,

I'm trying to subclass QTableWidget in order to alter its functionality/behavior. What I need is a function/method to populate the widget's table with data read from a text file but not all the data. The idea is to create a "fully-scrollable fake-view for the user", a range of rows, for example 10, while having thousands of rows in the data file, and give him a tool ( an independent* scrollbar; minimum 0, maximum results.size() ) to browse through all the data available.


Use case:

So for example, I read a hudge text file ( 15 MB! ; max case ) with a custom method, save the results in a "vector of strings" and populate only a subset of data rows ( like first 10 for the start ). "I scroll down the table" to see the rest of the data ( in fact, i want to scroll only the scrollbar and change the contents of the fake-table to present the appropriate/relevant data, like row names/indexes and row data ).

Why do I do it like that? Becase of my benchmark tests. When I tried to populate the table with all the data, the whole process was endless ( 1MB file presented in over 120 sec. ). The problem was on the Qt side. The data reading funtion worked pretty well ( 15 MB of raw text data saved to vector of strings in less then 4.5 sec ), so I figured out this idea of a "small table working like pixels in a TV".

The problem is, I can not get the scroll bar work independantly of the QTableWidget's contents. I mean, I can not set it free to be always enabled to scroll through <0; results.size()> range. Is there any way to make it possible? Or, if not, is there any efficient way of populating the QTableWidget's contents with some kind of a vector/list/something?

The file's structure is:

Qt Code:
  1. TITLE|VARIABLE1:NAME1:VALUE1|VARIABLE2:NAME2:VALUE2|
To copy to clipboard, switch view to plain text mode 

(as you can see the looping pattern is: "VARIABLE(N):NAME(N):VALUE(N)|VARIABLE(N+1):NAME(N +1):VALUE(N+1)|" )

The function/method is:

Qt Code:
  1. class MyQTableWidget { //..// private: std::vector<std::string*> results; //...// };
  2.  
  3. int MyQTableWidget::getDataFromFile(const char* v_szText, const char v_szDelimiter)
  4. {
  5. std::ifstream inf(v_szText);
  6. std::string buf;
  7.  
  8. std::string name;
  9. std::string label;
  10. std::string value;
  11.  
  12.  
  13. while(!std::getline(inf, buf, v_szDelimiter).eof()) {
  14. std::stringstream str(buf, std::stringstream::in);
  15.  
  16. std::getline(str, name, ':');
  17. std::getline(str, label, ':');
  18. std::getline(str, value);
  19.  
  20. results.push_back(new std::string(name));
  21. results.push_back(new std::string(label));
  22. results.push_back(new std::string(value));
  23. } // while
  24.  
  25. int range = results.size() < 20 ? results.size() : 20; // show only 20 items
  26.  
  27. for(int i = 0; i < range; i++) {
  28. iLabel = new QTableWidgetItem(tr((*(results[2*i])).c_str()));
  29. iValue = new QTableWidgetItem(tr((*(results[2*i+1])).c_str()));
  30.  
  31. this->setRowCount(i + 1);
  32. this->setItem(i, 0, iLabel);
  33. this->setItem(i, 1, iValue);
  34. }
  35.  
  36. vbar = new QScrollBar(this);
  37. vbar->setMinimum(0);
  38. vbar->setMaximum(results.size());
  39. vbar->setEnabled(true);
  40. vbar->setValue(0);
  41.  
  42. this->setVerticalScrollBar(vbar);
  43. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  44.  
  45. // ... //
  46. } // getDataFromFile
To copy to clipboard, switch view to plain text mode 

Shall I use some type of lazy loading ( like in Ajax ) or perhaps use some type of special technics for populating large sets of data?

Any help please?