QAbstractItemModel::rowsInserted notworking
-
QList<QStandardItem*> items; { // [Sample Name] { items.append(new usrStandardItem(strName)); } // [Conc] items.append(new usrStandardItem(fConc)); // [Abs] items.append(new usrStandardItem(fAbs)); // [Factor] items.append(new usrStandardItem(this->m_pQttInfo->fFactor)); // [Cell] items.append(new usrStandardItem(strCell)); // [Time] items.append(new usrStandardItem(QDateTime::currentDateTime().toString("yy-MM-dd HH:mm:ss"))); } m_pDataModel->appendRow(items); this->connect(this->m_pDataModel, &QAbstractItemModel::rowsInserted, this->m_pDataModel, [this] (const QModelIndex& index, int iStart, int iEnd) -> void { qDebug() << "QAbstractItemModel::rowsInserted: " << index << iStart << iEnd; this->ui.tableWidget->scrollTo(m_pDataModel->index(iStart, 0), QAbstractItemView::PositionAtBottom); });
It doesn't mean it can't, but it doesn't go down to the very end. And if you take the index value of the (const QModelIndex& index, int iStart, int iEnd) parameter, QModelIndex(-1,-1,0x0,QObject(0x0)) comes out.
When I do a data insertrow, I want to move to the last row.
-
Then please provide a minimal, compilable example to reproduce your problem. Works fine in a similar situation for me. You can also set the vertical slider to max.
this->connect(this->m_pDataModel, &QAbstractItemModel::rowsInserted, this->m_pDataModel, [this] (const QModelIndex& index, int iStart, int iEnd) -> void { QTimer::singleShot(0, [=]() { this->ui.tableWidget->scrollToBottom(); }); });
Strangely, if 10 data is added, it moves to the last row up to 9, but does not move to the 10th row.
I solved this problem with the code I posted. -
QList<QStandardItem*> items; { // [Sample Name] { items.append(new usrStandardItem(strName)); } // [Conc] items.append(new usrStandardItem(fConc)); // [Abs] items.append(new usrStandardItem(fAbs)); // [Factor] items.append(new usrStandardItem(this->m_pQttInfo->fFactor)); // [Cell] items.append(new usrStandardItem(strCell)); // [Time] items.append(new usrStandardItem(QDateTime::currentDateTime().toString("yy-MM-dd HH:mm:ss"))); } m_pDataModel->appendRow(items); this->connect(this->m_pDataModel, &QAbstractItemModel::rowsInserted, this->m_pDataModel, [this] (const QModelIndex& index, int iStart, int iEnd) -> void { qDebug() << "QAbstractItemModel::rowsInserted: " << index << iStart << iEnd; this->ui.tableWidget->scrollTo(m_pDataModel->index(iStart, 0), QAbstractItemView::PositionAtBottom); });
It doesn't mean it can't, but it doesn't go down to the very end. And if you take the index value of the (const QModelIndex& index, int iStart, int iEnd) parameter, QModelIndex(-1,-1,0x0,QObject(0x0)) comes out.
When I do a data insertrow, I want to move to the last row.
@IknowQT said in QAbstractItemModel::rowsInserted notworking:
QModelIndex(-1,-1,0x0,QObject(0x0)) comes out.
Which is correct since you're adding your items to the root item.
When I do a data insertrow, I want to move to the last row.
Why do you use
iStart
then instead therowCount() - 1
? -
@IknowQT said in QAbstractItemModel::rowsInserted notworking:
QModelIndex(-1,-1,0x0,QObject(0x0)) comes out.
Which is correct since you're adding your items to the root item.
When I do a data insertrow, I want to move to the last row.
Why do you use
iStart
then instead therowCount() - 1
?It's not that I haven't used it. I used row()-1, but it didn't go to the last row.
From 9 to 8 rows work normally, but it does not move to the last row.
-
Then please provide a minimal, compilable example to reproduce your problem. Works fine in a similar situation for me. You can also set the vertical slider to max.
-
Then please provide a minimal, compilable example to reproduce your problem. Works fine in a similar situation for me. You can also set the vertical slider to max.
this->connect(this->m_pDataModel, &QAbstractItemModel::rowsInserted, this->m_pDataModel, [this] (const QModelIndex& index, int iStart, int iEnd) -> void { QTimer::singleShot(0, [=]() { this->ui.tableWidget->scrollToBottom(); }); });
Strangely, if 10 data is added, it moves to the last row up to 9, but does not move to the 10th row.
I solved this problem with the code I posted.