Results 1 to 4 of 4

Thread: QTableView showing blank rows

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QTableView showing blank rows

    I'm using QTableView with QAbstractTableModel and I'm adding rows on a signal. When I get a new signal, I delete the exiting rows and add the rows obtained newly.
    The problem I'm facing is, the data to the new row is getting added properly, but every time, some blank rows are getting added, whose count is getting increased.
    Qt Code:
    1. class CTableModel : public QAbstractTableModel
    2. {
    3. private:
    4. QList<CTableData *> m_data;
    5. QFileIconProvider iconProvider;
    6. public:
    7. ...
    8. ...
    9.  
    10. int rowCount(const QModelIndex &) const
    11. {
    12. qDebug() << "Row Count - " << m_data.count();
    13. return m_data.count();
    14. }
    15. int columnCount(const QModelIndex &) const
    16. {
    17. return 3;
    18. }
    19. // So before new signal, I call this function to delete all the rows data.
    20. void clearData(void)
    21. {
    22. qDeleteAll(m_data);
    23. m_data.clear();
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 

    And my data class goes like this
    Qt Code:
    1. class CTableData
    2. {
    3. public:
    4. enum Type { File, Folder };
    5. CTableData()
    6. {
    7. ;
    8. }
    9. CTableData(Type & type, QString & path, QString size)
    10. {
    11. m_type = type;
    12. m_path = path;
    13. m_size = size;
    14. }
    15. ~CTableData() { }
    16.  
    17. void addData(Type & type, QString & path, QString size)
    18. {
    19. m_type = type;
    20. m_path = path;
    21. m_size = size;
    22. }
    23.  
    24. Type type(void) const
    25. {
    26. return m_type;
    27. }
    28.  
    29. QString path(void) const
    30. {
    31. return m_path;
    32. }
    33. QString size(void) const
    34. {
    35. return m_size;
    36. }
    37. private:
    38. Type m_type;
    39. QString m_path;
    40. QString m_size;
    41. };
    To copy to clipboard, switch view to plain text mode 

    And this is how I'm adding rows
    Qt Code:
    1. // This slot gets called on an update signal.
    2. void MainWidget::selectedItemsChanged()
    3. {
    4. QModelIndexList selectedFiles;
    5. QModelIndexList selectedDirectories;
    6. QModelIndexList unselectedFiles;
    7. QModelIndexList unselectedDirectories;
    8.  
    9. m_checkProxy->checkedState()
    10. .checkedLeafSourceModelIndexes(selectedFiles)
    11. .checkedBranchSourceModelIndexes(selectedDirectories)
    12. .uncheckedLeafSourceModelIndexes(unselectedFiles)
    13. .uncheckedBranchSourceModelIndexes(unselectedDirectories);
    14.  
    15. QStringList listOfSelectedItems;
    16. QString list = QString("Selected files: %1").arg(selectedFiles.count());
    17.  
    18. model->clearData();
    19. CTableData::Type type;
    20.  
    21. foreach (const QModelIndex index, selectedFiles)
    22. {
    23. list = index.data(QFileSystemModel::FilePathRole).toString();
    24. QModelIndex sizeIndex = index.model()->index(index.row(), 1, index.parent());
    25. type = CTableData::File;
    26. model->append(new CTableData(type, list, sizeIndex.data().toString()));
    27. }
    28.  
    29. foreach (const QModelIndex index, selectedDirectories)
    30. {
    31. list = index.data(QFileSystemModel::FilePathRole).toString();
    32. type = CTableData::Folder;
    33. model->append(new CTableData(type, list, QString()));
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    Please help me delete extra blank rows that I get on call of this void MainWidget::selectedItemsChanged() slot.
    Thank you.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView showing blank rows

    Your model needs to emit a range of signals to advise views that rows are being added, removed or modified. You are not doing that.
    See QAbstractItemModel and model/view programming.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableView showing blank rows

    In my model, I'm doing this -
    Qt Code:
    1. void CTableModel::append(CTableData * tableData)
    2. {
    3. int row = m_data.count();
    4. beginInsertRows(QModelIndex(), row, row);
    5. m_data.append(tableData);
    6. endInsertRows();
    7.  
    8. QModelIndex begin = index(row, 0);
    9. QModelIndex end = index(row, 2);
    10. emit dataChanged(begin, end); // emitting dataChanged signal --> blank rows still getting added
    11. }
    To copy to clipboard, switch view to plain text mode 

    But the behavior is still the same.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView showing blank rows

    Your model needs to emit a range of signals to advise views that rows are being added, removed or modified.
    Your clearData() is not announcing the removal of rows.

  5. The following user says thank you to ChrisW67 for this useful post:

    rawfool (8th May 2014)

Similar Threads

  1. How can I add blank space to a QTableView?
    By Paladin12 in forum Qt Programming
    Replies: 0
    Last Post: 15th August 2011, 11:39
  2. How to add blank space to a QTableView?
    By Paladin12 in forum Qt Programming
    Replies: 0
    Last Post: 3rd August 2011, 09:54
  3. QWebView showing blank pages.
    By gontham in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 10:02
  4. customize QTableView to add blank rows
    By rosenth in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2010, 11:47
  5. Showing selected rows in a separate table
    By dnnc in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2007, 16:35

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.