PDA

View Full Version : QTableView showing blank rows



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.

ChrisW67
6th May 2014, 21:40
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.

rawfool
7th May 2014, 08:55
In my model, I'm doing this -


void CTableModel::append(CTableData * tableData)
{
int row = m_data.count();
beginInsertRows(QModelIndex(), row, row);
m_data.append(tableData);
endInsertRows();

QModelIndex begin = index(row, 0);
QModelIndex end = index(row, 2);
emit dataChanged(begin, end); // emitting dataChanged signal --> blank rows still getting added
}

But the behavior is still the same.

ChrisW67
8th May 2014, 01:17
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.