0

This a very simple problem to which I can find no solution:

This is my code:

qint32 pos = ui->twShow->verticalScrollBar()->value();
ui->twShow->blockSignals(true);    

//Code for updating the contents QTableWidget twShow, this is done by erasing all cells and adding them again, in case it matters.

ui->twShow->blockSignals(false);
if (pos > 0){
    ui->twShow->verticalScrollBar()->setValue(pos);
}

What I want to accomplish is simply to maintain the vertical scroll position. However the setValue function ignores the value pos (I've checked by printing the value before and after the instruction and both times its cero).

I have also tried: QScrollBar *bar = ui->twShow->verticalScrollBar(); // Same code as before ui->twShow->setVerticalScrollBar(bar); //This line crashes de program

However the last line crashes the program (which I've checked by commenting it, and it works fine).

Any advice would be greatly appreciated...

Thank you very much

aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • can you pls clarify what do you mean by 'same position'. In general you could have different number of rows loaded after update. So you want to just to same absolute row or relative scroll bar position or scroll new content till same values appears? – evilruff Apr 23 '13 at 13:53
  • The number of rows is always the same. I want the exact same scroll position. – aarelovich Apr 23 '13 at 13:56

2 Answers2

0
 QTableWidget * tw;
 int            desiredRow;

 // before update

 desiredRow = tw->row(tw->itemAt(1,1));

 ...
 // update code
 ...

 tw->scrollToItem( tw->item( desiredRow, 0), 
  QAbstractItemView::EnsureVisible | QAbstractItemView::PositionAtTop );
evilruff
  • 3,947
  • 1
  • 15
  • 27
  • I don't understand. If no row has been selected, how do I know the desired row? That is why I'm trying to solve this by setting the slider position. – aarelovich Apr 23 '13 at 14:06
  • Thank you, but it didn't work. It does nothing. After the update the scroll bar still appears at the top (and I've checked the desired row contains the right row number). – aarelovich Apr 23 '13 at 14:17
  • check that desiredRow keeps correct value before update starts, and a value of tw->item(desiredRow,0).. – evilruff Apr 23 '13 at 14:19
  • And yes. The item is the correct item. It is simply not scrolling. – aarelovich Apr 23 '13 at 14:22
  • try like that (second parameter added) – evilruff Apr 23 '13 at 14:34
  • The command doesn't compile. It seems to have to do with OR on the flags, but I can't figure out how it should go. I've tried with each of the parameters independently and it still does nothing. – aarelovich Apr 23 '13 at 17:06
  • you should use them both, first instruct that item specified should became visible, second that this item should appear in the top. – evilruff Apr 23 '13 at 17:08
  • Any suggestions on why it doesn't compile, the? I get the following error: invalid conversion from 'int' to 'QAbstractItemView::ScrollHint' [-fpermissive] error: initializing argument 2 of 'void QTableWidget::scrollToItem(const QTableWidgetItem*, QAbstractItemView::ScrollHint)' [-fpermissive] – aarelovich Apr 23 '13 at 17:14
0

QAbstractItemView::EnsureVisible = 0. The 'or' flag converts the result to an integer which is not allowed as parameter of the scrollToItem method. On the other hand enums are not intended to be used as combined flags.