After removing rows in QTableView, gridlines still show.
-
I am using the removeRows() function below for removing rows in a QTableView.
It works, however, the problem is that although the function completes successfully and quickly, it might take a few seconds for the actual rows to be removed visually. Also, after the rows do visually disappear, the grid lines in the tableview still show for the exact number of items in the tableview. And when I add new rows to the table, the grid lines disappear as if it's being redrawn and looks correct.
Here is my table model.
class MyModel : public QAbstractTableModel { QList<MyItem> items; public: MyModel(QObject *parent = nullptr); int rowCount(const QModelIndex &index = QModelIndex()) const override; int columnCount(const QModelIndex &index = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; void append(const MyItem &item); bool removeRow(int row, const QModelIndex &parent = QModelIndex()); bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); }; bool MyModel::removeRows(int row, int count, const QModelIndex &parent) { beginRemoveRows(parent, row, count); QList<MyItems>::iterator it; for (it = items.begin(); it != items.end(); ++it) items.erase(it); endRemoveRows(); return true; }
Is there some sort of update function that I should be calling to clear the QTableView after removing?
Update:
Found reset(), which I call after removeRows() and the view now clears fast, but gridlines still appear. Also tried update() to no avail. -
I am using the removeRows() function below for removing rows in a QTableView.
It works, however, the problem is that although the function completes successfully and quickly, it might take a few seconds for the actual rows to be removed visually. Also, after the rows do visually disappear, the grid lines in the tableview still show for the exact number of items in the tableview. And when I add new rows to the table, the grid lines disappear as if it's being redrawn and looks correct.
Here is my table model.
class MyModel : public QAbstractTableModel { QList<MyItem> items; public: MyModel(QObject *parent = nullptr); int rowCount(const QModelIndex &index = QModelIndex()) const override; int columnCount(const QModelIndex &index = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; void append(const MyItem &item); bool removeRow(int row, const QModelIndex &parent = QModelIndex()); bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); }; bool MyModel::removeRows(int row, int count, const QModelIndex &parent) { beginRemoveRows(parent, row, count); QList<MyItems>::iterator it; for (it = items.begin(); it != items.end(); ++it) items.erase(it); endRemoveRows(); return true; }
Is there some sort of update function that I should be calling to clear the QTableView after removing?
Update:
Found reset(), which I call after removeRows() and the view now clears fast, but gridlines still appear. Also tried update() to no avail.if You want to display Rows itemwise Or Data Are The Itembase Then use
QTableWidget
instead ofQTableView
QTableView
is ModelBased Widget AndQTableWidget
is ItemBased Widget -
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
-
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
@Christian-Ehrlicher
Damn! Good spot! :) -
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
@Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the
last
arg. No? -
@Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the
last
arg. No?@johnby said in After removing rows in QTableView, gridlines still show.:
I see no endRow
Read the documentation in the link I gave you: https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveColumns
-
@Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:
@johnby said in After removing rows in QTableView, gridlines still show.:
beginRemoveRows(parent, row, count);
This is wrong. It's not count but endRow.
Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the
last
arg. No? -
Ok, I'm assuming you meant to send me the link to https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows
Anyways, I think I see what you were referring to, however, I was just trying to delete all rows using iters and ignoring the first and last, but I changed it using first and last.
bool MyModel::removeRows(int first, int last, const QModelIndex &parent) { QList<MyItems>::iterator it; beginRemoveRows(parent, first, last); for (int row = last - 1; row >= 0; --row) items.removeAt(row); endRemoveRows(); }
So even though this does remove all items in the list, I still have the issue of gridlines staying in the view. Thoughts?
-
@johnby said in After removing rows in QTableView, gridlines still show.:
Thoughts?
I would guess
first
is not 0.
If you want to remove all rows use begin/endResetModel() and clear your complete container - it's faster. -
Ok, I'm assuming you meant to send me the link to https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows
Anyways, I think I see what you were referring to, however, I was just trying to delete all rows using iters and ignoring the first and last, but I changed it using first and last.
bool MyModel::removeRows(int first, int last, const QModelIndex &parent) { QList<MyItems>::iterator it; beginRemoveRows(parent, first, last); for (int row = last - 1; row >= 0; --row) items.removeAt(row); endRemoveRows(); }
So even though this does remove all items in the list, I still have the issue of gridlines staying in the view. Thoughts?
@johnby said in After removing rows in QTableView, gridlines still show.:
for (int row = last - 1; row >= 0; --row)
Again, you cannot/should not do this. You must respect the parameters passed in, both
first
&last
. Otherwise what you remove here does not correspond to thebeginRemoveRows()
call you make, and that will upset any view attached to the model.