rawfool
6th May 2014, 14:26
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.
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();
}
};
And my data class goes like this
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;
};
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(selectedDirectori es)
.uncheckedLeafSourceModelIndexes(unselectedFiles)
.uncheckedBranchSourceModelIndexes(unselectedDirec tories);
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).toStrin g();
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).toStrin g();
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.
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.
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();
}
};
And my data class goes like this
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;
};
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(selectedDirectori es)
.uncheckedLeafSourceModelIndexes(unselectedFiles)
.uncheckedBranchSourceModelIndexes(unselectedDirec tories);
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).toStrin g();
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).toStrin g();
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.