Solved QTableView subclass - Call setRowHidden from paintEvent
-
I have written a QTableView which should be able to hide rows based on a certain condition as well as a flag, that the user can set in the handleCollapse slot.
void OrderBookView::paintEvent(QPaintEvent* event) { for (int row = 0; row < model()->rowCount(); row++) { setRowHidden(row, isCollapsed && !rowHasOrder(row)); } QTableView::paintEvent(event); } void OrderBookView::handleCollapse() { isCollapsed = !isCollapsed; } bool OrderBookView::rowHasOrder(size_t row) { QStandardItemModel* m = dynamic_cast<QStandardItemModel*>(model()); if (m) { return !(m->item(row, 0)->text().isEmpty() && m->item(row, 1)->text().isEmpty() && m->item(row, 3)->text().isEmpty() && m->item(row, 4)->text().isEmpty()); } return false; }
Is it safe to call setRowHidden from paintEvent? The reason why I call it before every repaint is, that the user can add more rows to the model which have to be hidden right away based on the rowHasOrder condition, so just hiding all currently existing rows in handleCollapse would not be enough.
Is there maybe a better way to achieve what I am looking for? -
@Herman-Nordberg
I wouldn't, sounds dodgily-late to me, but just opinion. "the user can add more rows to the model which have to be hidden right away" --- that seems to me what you should recognize and act upon. -
This is for sure no task which should be done inside paintEvent().
-
Ok, thank you. Is there a way for the view to detect new rows in the model? I geuss I could just send a signal from the model to a slot in the view, but I didn't want to add too many signals that tightly couple my model with the view.
-
@Herman-Nordberg said in QTableView subclass - Call setRowHidden from paintEvent:
I geuss I could just send a signal from the model to a slot in the view,
Why? See https://doc.qt.io/qt-5/qabstractitemmodel.html#signals and please read https://doc.qt.io/qt-5/model-view-programming.html
-
@Christian-Ehrlicher Ok thank you, I already had a feeling my solution was too hacky, hence the thread. I will work on the basics a bit, my question is answered so far.