PDA

View Full Version : Problem with scrollbar. How to set it properly at the bottom?



robgeek
13th December 2015, 22:09
Hello!

I have a QTableWidget in my program and i would like the scrollbar stay on the bottom of it at the first time that she load the data. To do that i tried as follows:

DataLoader::DataLoader(QWidget *parent) : QDialog( parent ), ui(new Ui::DataLoader) {
ui->setupUi( this );
//...
showHistTable(ui->tableHist, matrix);
//...
}

//...

void showHistTable(QTableWidget *table, Historic historic) {

// Clear all data.
for(int i = 0; i < historic.size; i++)
table->removeRow( 0 );

for(int i = 0; i < historic.size; i++) {
table->insertRow( i );

for(int j = 0; j < historic.win; j++) {
QTableWidgetItem *val = setNumberCell( historic.historic[i][j] );
table->setItem(i, j, val);
}
}

// Scrollbar in the bottom.
table->verticalScrollBar( )->setValue( table->verticalScrollBar( )->maximum( ) );
}

Why this code is not working?
How can i fix it?

anda_skoa
14th December 2015, 06:44
The window does not have its final size yet when you run the constructor, that is determined when it is shown.
So the table view's size might also still change and it might get a different maximum value.

Try moving the scroll bar adjustment to the showEvent() method.

Cheers,
_

robgeek
14th December 2015, 17:34
Try moving the scroll bar adjustment to the showEvent() method.

Sorry, but i don't understand what do you mean with that. I don't have this method implemented.

My code with one more method i implemented:

DataLoader::DataLoader(QWidget *parent) : QDialog( parent ), ui(new Ui::DataLoader) {
ui->setupUi( this );
//...
configHistTable(ui->TableHist, matrix->length);
showHistTable(ui->tableHist, matrix);
//...
}

//...

void configHistTable(QTableWidget *table, int cols) {
int width = 100;

for(int i = 0; i < (cols + 1); i++)
table->setColumnWidth(i, width);
}

void showHistTable(QTableWidget *table, Historic historic) {

// Clear all data.
for(int i = 0; i < historic.size; i++)
table->removeRow( 0 );

for(int i = 0; i < historic.size; i++) {
table->insertRow( i );

for(int j = 0; j < historic.win; j++) {
QTableWidgetItem *val = setNumberCell( historic.historic[i][j] );
table->setItem(i, j, val);
}
}

// Scrollbar in the bottom.
table->verticalScrollBar( )->setValue( table->verticalScrollBar( )->maximum( ) );
}

anda_skoa
15th December 2015, 10:01
Sorry, but i don't understand what do you mean with that. I don't have this method implemented.

Currently not, no.
But that is what I would recommend you try.

Cheers,
_

ChrisW67
15th December 2015, 20:18
You may find QAbstractItemView::scrollToBottom() useful. An alternate approach would be to use a zero length QTimer to trigger the scrollToBottom() slot after control returns to the event loop.