Results 1 to 5 of 5

Thread: Problem with scrollbar. How to set it properly at the bottom?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Problem with scrollbar. How to set it properly at the bottom?

    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:
    Qt Code:
    1. DataLoader::DataLoader(QWidget *parent) : QDialog( parent ), ui(new Ui::DataLoader) {
    2. ui->setupUi( this );
    3. //...
    4. showHistTable(ui->tableHist, matrix);
    5. //...
    6. }
    7.  
    8. //...
    9.  
    10. void showHistTable(QTableWidget *table, Historic historic) {
    11.  
    12. // Clear all data.
    13. for(int i = 0; i < historic.size; i++)
    14. table->removeRow( 0 );
    15.  
    16. for(int i = 0; i < historic.size; i++) {
    17. table->insertRow( i );
    18.  
    19. for(int j = 0; j < historic.win; j++) {
    20. QTableWidgetItem *val = setNumberCell( historic.historic[i][j] );
    21. table->setItem(i, j, val);
    22. }
    23. }
    24.  
    25. // Scrollbar in the bottom.
    26. table->verticalScrollBar( )->setValue( table->verticalScrollBar( )->maximum( ) );
    27. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with scrollbar. How to set it properly at the bottom?

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    robgeek (9th February 2016)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Problem with scrollbar. How to set it properly at the bottom?

    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:
    Qt Code:
    1. DataLoader::DataLoader(QWidget *parent) : QDialog( parent ), ui(new Ui::DataLoader) {
    2. ui->setupUi( this );
    3. //...
    4. configHistTable(ui->TableHist, matrix->length);
    5. showHistTable(ui->tableHist, matrix);
    6. //...
    7. }
    8.  
    9. //...
    10.  
    11. void configHistTable(QTableWidget *table, int cols) {
    12. int width = 100;
    13.  
    14. for(int i = 0; i < (cols + 1); i++)
    15. table->setColumnWidth(i, width);
    16. }
    17.  
    18. void showHistTable(QTableWidget *table, Historic historic) {
    19.  
    20. // Clear all data.
    21. for(int i = 0; i < historic.size; i++)
    22. table->removeRow( 0 );
    23.  
    24. for(int i = 0; i < historic.size; i++) {
    25. table->insertRow( i );
    26.  
    27. for(int j = 0; j < historic.win; j++) {
    28. QTableWidgetItem *val = setNumberCell( historic.historic[i][j] );
    29. table->setItem(i, j, val);
    30. }
    31. }
    32.  
    33. // Scrollbar in the bottom.
    34. table->verticalScrollBar( )->setValue( table->verticalScrollBar( )->maximum( ) );
    35. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with scrollbar. How to set it properly at the bottom?

    Quote Originally Posted by robgeek View Post
    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,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    robgeek (9th February 2016)

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with scrollbar. How to set it properly at the bottom?

    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    robgeek (9th February 2016)

Similar Threads

  1. QScrollbar bottom problem
    By Alundra in forum Qt Programming
    Replies: 4
    Last Post: 6th June 2014, 15:35
  2. Problem with scrolling to the bottom of a QListView
    By JohnAndy in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 22nd September 2010, 14:59
  3. Problem with the scrollbar
    By mani_cbz in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 14th July 2009, 10:24
  4. Replies: 7
    Last Post: 15th November 2007, 18:19
  5. ScrollBar is not moving properly when I use TAB
    By chikkireddi in forum Qt Programming
    Replies: 15
    Last Post: 25th May 2007, 23:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.