Results 1 to 2 of 2

Thread: QTableView insertRow 's dynamically

  1. #1
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableView insertRow 's dynamically

    I have periodically action to insert a single rows in existing QTableList.
    Problem is that inserted row does not display items correctly in the right place.
    I think it only happens when QtableList has set.

    Qt Code:
    1. this->sortItems( 1, Qt::AscendingOrder );
    To copy to clipboard, switch view to plain text mode 

    When I remove sorting new rows appears on the bottom and all row items are displayed just find.

    This is how I do it.

    Qt Code:
    1. void ListView::insertRowFile(File *file) {
    2. DEBUG;
    3.  
    4. int row = this->rowCount();
    5. this->setRowCount(row+1);
    6.  
    7. dateItem->setText(file->FileDate().toString("MM-dd-yyyy hh:mm"));
    8. this->setItem(row, col, dateItem);
    9.  
    10. nameItem->setIcon(file->FileIcon());
    11. dateItem->setText(file->FileName());
    12. this->setItem(row, col, nameItem);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 


    I have tried also

    Qt Code:
    1. this->insertRow(row);
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. this->setRowCount(row+1);
    To copy to clipboard, switch view to plain text mode 

    I set sortItems after displaying all rows in initial list construct.

    What is a proper way to use insertRow with column sorting enabled ?

    Thank You

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView insertRow 's dynamically

    When inserting rows into QTableWidget, you need to disable sorting.

    1. Disable Sorting
    2. Insert Row(s), set all items in the row.
    3. Enable Sorting

    This is because, with sorting enabled, as soon as you insert a new item in a row, QTableWidget runs the sorting check on the new row item and relocates the item as per set sorting order, this means that the item row number is changed, and the row variable which you hold is no more valid, and when you to set the next item on the same row, it will give you spurious results, as the row variable is not valid any more

    Other alternate to disable sorting, is to get the latest row number before setting the item, like this

    1. Insert a new row (say row)
    2. Set new item at row, at column 0
    3. Get the new row number of the newly set item at column 0
    4. Set next item at new row, at column 1
    ....next columns, as needed

  3. The following user says thank you to Santosh Reddy for this useful post:

    migel (30th June 2011)

Similar Threads

  1. how to add tab dynamically
    By asish_cse in forum Newbie
    Replies: 2
    Last Post: 14th September 2010, 16:27
  2. How to InsertRow Throught QSqlQueryModel ?
    By innobleday in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 04:44
  3. Replies: 2
    Last Post: 11th January 2010, 11:39
  4. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 11:55
  5. QTableView QTablemodel & insertRow
    By pfusterschmied in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 12:43

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.