Results 1 to 4 of 4

Thread: QTableWidget subclassing, populating and scrolling problem

  1. #1
    Join Date
    Dec 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget subclassing, populating and scrolling problem

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget subclassing, populating and scrolling problem

    The problem is, I can not get the scroll bar work independantly of the QTableWidget's contents.
    Did you try to disable the scroll bar for the table, and add your own scroll bar?
    This way, you can set any range you want to your scroll bar, and populate your table in a custom way with the data you want.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget subclassing, populating and scrolling problem

    Well, I actualy tried that :/ The thing is, that if I add a custom scrollbar with its minimum and maximum set appropriately, it is somehow managed by the QTableWidget to disable scrolling if the number of shown elements is less than the "the view size" ( visible area ) :/ I suppose it is because populating the table causes emition of some sort of signal or updates some QTableWidget's properties.

    I tried to do it like that:

    Qt Code:
    1. vbar = new QScrollBar(this); // this = QTableWidget
    2. vbar->setMinimum(0);
    3. vbar->setMaximum(results.size()); // results is a vector of string*
    4. vbar->setEnabled(true);
    5. vbar->setValue(0);
    6.  
    7.  
    8. this->setVerticalScrollBar(vbar);
    9. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget subclassing, populating and scrolling problem

    Ok, I did some terrible mistake in my code, that is why it didn't work for me. Sorry for you trouble. :/

Tags for this Thread

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.