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.
Code:
{
private:
QList<CTableData *> m_data;
public:
...
...
{
qDebug() << "Row Count - " << m_data.count();
return m_data.count();
}
{
return 3;
}
// So before new signal, I call this function to delete all the rows data.
void clearData(void)
{
qDeleteAll(m_data);
m_data.clear();
}
};
And my data class goes like this
Code:
class CTableData
{
public:
enum Type { File, Folder };
CTableData()
{
;
}
{
m_type = type;
m_path = path;
m_size = size;
}
~CTableData() { }
{
m_type = type;
m_path = path;
m_size = size;
}
Type type(void) const
{
return m_type;
}
{
return m_path;
}
{
return m_size;
}
private:
Type m_type;
};
And this is how I'm adding rows
Code:
// This slot gets called on an update signal.
void MainWidget::selectedItemsChanged()
{
QModelIndexList selectedFiles;
QModelIndexList selectedDirectories;
QModelIndexList unselectedFiles;
QModelIndexList unselectedDirectories;
m_checkProxy->checkedState()
.checkedLeafSourceModelIndexes(selectedFiles)
.checkedBranchSourceModelIndexes(selectedDirectories)
.uncheckedLeafSourceModelIndexes(unselectedFiles)
.uncheckedBranchSourceModelIndexes(unselectedDirectories);
QString list
= QString("Selected files: %1").
arg(selectedFiles.
count());
model->clearData();
CTableData::Type type;
{
list = index.data(QFileSystemModel::FilePathRole).toString();
QModelIndex sizeIndex
= index.
model()->index
(index.
row(),
1, index.
parent());
type = CTableData::File;
model->append(new CTableData(type, list, sizeIndex.data().toString()));
}
{
list = index.data(QFileSystemModel::FilePathRole).toString();
type = CTableData::Folder;
model
->append
(new CTableData
(type, list,
QString()));
}
}
Please help me delete extra blank rows that I get on call of this void MainWidget::selectedItemsChanged() slot.
Thank you.
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.
Re: QTableView showing blank rows
In my model, I'm doing this -
Code:
void CTableModel::append(CTableData * tableData)
{
int row = m_data.count();
m_data.append(tableData);
endInsertRows();
emit dataChanged(begin, end); // emitting dataChanged signal --> blank rows still getting added
}
But the behavior is still the same.
Re: QTableView showing blank rows
Quote:
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.