Results 1 to 3 of 3

Thread: QTableWidget Not Scrolling, Centering Properly

  1. #1
    Join Date
    Mar 2016
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTableWidget Not Scrolling, Centering Properly

    Hi all,

    I'm stumped trying to understand why I my QTableWidget to scroll like I think it should. I'm appending rows to a QTableWidget and I want to center the view on a particular row.

    Qt Code:
    1. ui->tableWidget->scrollToTop();
    2. ui->tableWidget->scrollToBottom();
    To copy to clipboard, switch view to plain text mode 

    Testing one at a time, both of these work great and the behavior is as expected. The scrollbars\view stay either at the top or the bottom. However when I try to center the view it doesn't work at all:

    Qt Code:
    1. ui->tableWidget->scrollToItem(ui->tableWidget->itemAt(center, 0));
    To copy to clipboard, switch view to plain text mode 

    Center is an integer representing the row I want to center the view on. I tried switching "center,0" with "0,center" it didn't seem to help. I check the return value for itemAt and it's returning a non-NULL value.

    My workaround (from: "QListWidget in a tab not scrolling on setCurrentRow()") was to do this:

    Qt Code:
    1. ui->tableWidget->setCurrentCell(center, 0);
    2. QModelIndex index = ui->tableWidget->currentIndex();
    3. ui->tableWidget->scrollTo(index, QAbstractItemView::PositionAtCenter);
    4. ui->tableWidget->setCurrentCell(-1, -1);
    To copy to clipboard, switch view to plain text mode 

    Which works but seems horrible. The last setCurrentCell() was to un-highlight the cell...

    For my own knowledge can you please help to explain what I was doing wrong? Thanks in advance.

  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: QTableWidget Not Scrolling, Centering Properly

    Did you check the documentation of the QTableWidget::itemAt() and then decided to ignored it or is reading the documentation something you delegate to others?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget Not Scrolling, Centering Properly

    Hello!
    I have the same problem in QT 5.5

    Solution:
    1) Find in the file Qt_source\qtbase\src\widgets\itemviews\qtableview. cpp function

    Qt Code:
    1. void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint)
    To copy to clipboard, switch view to plain text mode 

    2) Add string updateGeometries(); to this function

    Qt Code:
    1. void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint)
    2. {
    3.  
    4. Q_D(QTableView);
    5.  
    6. updateGeometries();
    7. .....
    8. }
    To copy to clipboard, switch view to plain text mode 

    3) Go to Qt_source\qtbase\src\widgets and recompile Qt5Widgets.dll

    It works well to me!


    By analogy with the function in Qt_source\qtbase\src\widgets\itemviews\qabstractit emview.cpp
    Qt Code:
    1. void QAbstractItemView::scrollToBottom()
    2. {
    3. if (d->delayedPendingLayout) {
    4. d->executePostedLayout();
    5. updateGeometries();
    6. }
    7. verticalScrollBar()->setValue(verticalScrollBar()->maximum());
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Properly using QTableWidget and QTableWidgetItem
    By c0dehunter in forum Newbie
    Replies: 4
    Last Post: 27th October 2012, 16:38
  2. Centering QPushButton within a QTableWidget
    By bob2oneil in forum Qt Programming
    Replies: 7
    Last Post: 11th November 2011, 11:26
  3. Replies: 3
    Last Post: 8th December 2009, 11:49
  4. QTableWidget - scrolling to bottom of the table
    By mkhoang in forum Qt Programming
    Replies: 4
    Last Post: 12th November 2008, 18:21
  5. scrolling in QTableWidget
    By Ursa in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2007, 08:22

Tags for this Thread

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.