PDA

View Full Version : QTreeView problem scrolling to end.



seneca
24th January 2006, 12:02
[Qt 4.1.0, windows]

In a QTableView I want to add a new row at the end, scroll to it and open the first column for editing. However it only scrolls down to the second last row (the one that was the last one before the new row was added). The new row remains outside of visibility. I have also tried with scrollToBottom(), but exactly the same.


if (act == iActionAddValue) {
ALocalvalueModel* model = (ALocalvalueModel*)iTable->model();
model->insertRow(model->rowCount());
iTable->resizeRowsToContents();
iTable->clearSelection();
QModelIndex index(model->index(model->rowCount()-1,0));
iTable->scrollTo(index);
iTable->edit(index);
} // if

wysota
24th January 2006, 14:12
Did you try using different scroll hints? And does a proper row get edited?

seneca
24th January 2006, 14:51
When there is enough space in the visible table to add another row everything works as expected: the new row gets added and the first column of it is opened for editing as expected.

I have not yet looked at scroll hints, only tried scrollTo(index) and scrollToBottom(). So basicly it should default to QAbstractItemView::EnsureVisible, but I will give the others a try.

Could it be related to the fact that I allways need to do resizeRowsToContents() because by default the new rows are allways to high?

Edit:
I tried all availabe scroll hints now without success. It allways scrolls one row to few to show the last row, whatever hint is used.

Next I tried to add 2 rows at a time, and to my surprise the scroll halts so that non of them is shown. I now suspect that there needs to be a function call or something as an idle event loop between adding the row(s) and scrolling.

wysota
24th January 2006, 15:29
You can try setting a timer with timeout set to 0 and insert those scroll and edit commands there. The timer will go off as soon as the event queue gains control over the application.

seneca
24th January 2006, 15:59
Yes, the timer did the trick. This is how it is working now, including the new slot:


...
if (act == iActionAddValue) {
iTable->clearSelection();
QAbstractItemModel* model = iTable->model();
model->insertRow(model->rowCount());
iTable->resizeRowsToContents();
QTimer::singleShot(0, this, SLOT(scrollAndEdit()));
} // if
...

void AMainWindow::scrollAndEdit() // declared as private slot
{
QAbstractItemModel* model = iTable->model();
QModelIndex index(model->index(model->rowCount()-1,0));
iTable->scrollTo(index);
iTable->edit(index);
} // scrollAndEdit


Well surely not an elegant solution, but at least a workaround. I wonder if there is a more convenient way to let the pending events be consumed between two statements.

wysota
24th January 2006, 16:52
I think you can post it to Trolltech as a bug.

seneca
25th January 2006, 14:14
Yes it is considered a bug by TT and should get fixed. An easier workaround for the problem is to issue QApplication::sendPostedEvents() immediately before the call to scrollTo().

prasad_N
22nd December 2015, 13:08
Ran into same issue & came across that this bug has not fixed in Qt4.x from https://bugreports.qt.io/browse/QTBUG-9326 (here).
Is there a work around for this, singleShot timer did not work in my case, I tried signal slot also did not work :(
Any suggestions from experts ??


I have some modelindex (ex: l_ modelIndex);


scrollTo(l_modelIndex, , QAbstractItemView::PositionAtTop);
//Is not getting scroll to this index, I tried printing data just to make sure that index is proper or not , qDebug() << l_modelIndex.data() is printing expected data to where it needs to move.