Better way to adding Large amount's of rows & columns to QTableWidget?



  • Hi,
    I have a QTableWidget with some random no of rows & columns, & I have created QTableWidgetItem for each cell.
    Now I am asking the user for how many rows & columns must be increased.
    Now the problem is that the rows & columns are added in the middle of table & not at the end. But they are contiguously added.
    There are 2 options (just showing the logic, not the exact syntax!) :

    for(no of columns/rows to add)
    {
    table->insertColumn();
    table->insertRow();
    //in 1 more loop create QTableWidgetItem & set that for cells of corresponding row/column
    }

    delete table;
    table = new QTableWidget();
    table->setColumnCount();
    table->setRowCount();
    //create QTableWidgetItem for each cell & set that as item.

    Currently I am getting less time for 2nd method. But I am not sure as to which method is better.
    Any suggestions?
    Thank You.



  • Is it a necessity to use QTableWidget?
    Why don't you use a QtableView with a custom model? that way it should be easier for you to insert data in between and then emit the model changed events which would refresh the view automatically?



  • Are you sure the 2nd method is the faster one? Sounds strange...

    A small hint: If you want to use method 2, you don't have to delete the whole TableWidget, just call QTableWidget::clear()...
    But you should really consider using a TableView with your own model (Model/View programming isn't that difficult)



  • @prady_80 I dont know how mode/view framework works. I tried understanding once but it was too much for me to understand in one go. I think I will try understanding it again.

    @Mr. Universe the docs for QTableWidget::clear() dont say much. Does it automatically delete the QTableWidgetItem's for all the cell?
    What is the difference between ::clear() & ::clearcontents(). Both seem to do the same.



  • To get you started "this":http://doc.qt.digia.com/4.7/model-view-programming.html will give you a very clear understanding about QModelIndex which is the main object around which everything revolves. And as an example to modify and do your self stuff you can look at "Address Book example":http://qt-project.org/doc/qt-4.8/itemviews-addressbook.htmlfrom Qt examples.

    EDIT: If you wish to insert items in between
    you can create a new empty row using QTableWidget::insertRow.
    Get the inserted row using QTableWidget::item and modify it.
    The view should automatically show it as it model underlying QTableWidget will emit A signal to refresh the view


Log in to reply
 

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