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.
{
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();
}
};
class CTableModel : public QAbstractTableModel
{
private:
QList<CTableData *> m_data;
QFileIconProvider iconProvider;
public:
...
...
int rowCount(const QModelIndex &) const
{
qDebug() << "Row Count - " << m_data.count();
return m_data.count();
}
int columnCount(const QModelIndex &) const
{
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();
}
};
To copy to clipboard, switch view to plain text mode
And my data class goes like this
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;
};
class CTableData
{
public:
enum Type { File, Folder };
CTableData()
{
;
}
CTableData(Type & type, QString & path, QString size)
{
m_type = type;
m_path = path;
m_size = size;
}
~CTableData() { }
void addData(Type & type, QString & path, QString size)
{
m_type = type;
m_path = path;
m_size = size;
}
Type type(void) const
{
return m_type;
}
QString path(void) const
{
return m_path;
}
QString size(void) const
{
return m_size;
}
private:
Type m_type;
QString m_path;
QString m_size;
};
To copy to clipboard, switch view to plain text mode
And this is how I'm adding rows
// 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()));
}
}
// 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);
QStringList listOfSelectedItems;
QString list = QString("Selected files: %1").arg(selectedFiles.count());
model->clearData();
CTableData::Type type;
foreach (const QModelIndex index, selectedFiles)
{
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()));
}
foreach (const QModelIndex index, selectedDirectories)
{
list = index.data(QFileSystemModel::FilePathRole).toString();
type = CTableData::Folder;
model->append(new CTableData(type, list, QString()));
}
}
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.
Bookmarks