Results 1 to 3 of 3

Thread: Takin control of QTableView Scrollbars

  1. #1
    Join Date
    Jul 2012
    Posts
    228
    Thanks
    19
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Takin control of QTableView Scrollbars

    Hello,


    I have a QTableView widget with only one row, that is "scrolled" by a custom scrollbar of mine. This works fine when i have widget that contains both the tableview and the scrollbar, as in:

    Qt Code:
    1. QWidget* wid = new QWidget();
    2. QLayout* l = new QHBoxLayout();
    3. l->addWidget(tableview);
    4. l->addWidget(scrollbar);
    5. wid->setLayout(l);
    To copy to clipboard, switch view to plain text mode 

    Since QTableView inherits QAbstractScrollArea, i though i could make my scrollbar part of the QTableView item. Not so. When i call setScrollBar(scrollbar) it only shows an empty scrollbar. I am guessing this is because the scrollbar is associated with the scroll logic of the tableview.


    Any way to disassociate it with the tableview/abstractscrollarea scrolling logic and make it behave as totally independent component?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,595
    Thanks
    13
    Thanked 1,588 Times in 1,517 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Takin control of QTableView Scrollbars

    This works for me:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTableView>
    3. #include <QStandardItemModel>
    4. #include <QScrollBar>
    5.  
    6. class WideScrollBar: public QScrollBar
    7. {
    8. public:
    9. WideScrollBar(QWidget *p = 0): QScrollBar(p)
    10. {
    11. setStyleSheet( "QScrollBar:vertical { width: 25px; } ");
    12. }
    13. };
    14.  
    15.  
    16. int main(int argc, char *argv[])
    17. {
    18. QApplication app(argc, argv);
    19.  
    20. QStandardItemModel model(50, 3);
    21. for (int i = 0; i < model.rowCount(); ++i) {
    22. for (int j = 0; j < model.columnCount(); ++j) {
    23. QStandardItem *item = new QStandardItem(QString("%1, %2").arg(i).arg(j));
    24. model.setItem(i, j, item);
    25. }
    26. }
    27.  
    28. QTableView view;
    29. view.setModel(&model);
    30. view.setVerticalScrollBar(new WideScrollBar(&view));
    31. view.show();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    and produces a customised scroll bar that works as expected. If the table had only one row I would not expect to see a vertical scroll bar at all.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Takin control of QTableView Scrollbars

    Quote Originally Posted by tuli View Post
    Any way to disassociate it with the tableview/abstractscrollarea scrolling logic and make it behave as totally independent component?
    Isn't that what you already have?

    Why associate it just to disassociate it again?

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 31st July 2014, 08:55
  2. Replies: 3
    Last Post: 29th July 2014, 18:18
  3. scrollbars
    By giugio in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2012, 18:33
  4. Replies: 0
    Last Post: 16th December 2009, 09:45
  5. How to get scrollbars in QTreeWidget
    By Arthur in forum Qt Programming
    Replies: 14
    Last Post: 5th May 2006, 21:51

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.