Results 1 to 7 of 7

Thread: how to synchronize 2 view's (Table & tree) scrolls.

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default how to synchronize 2 view's (Table & tree) scrolls.

    I wanted to synchronize 2 views scrolling. I have TableView & TreeView, where I set Table view as header to the Tree view. now I want to move header (table view) in sync with tree view when I scroll tree view horizontally.

    I have written below code,, But when I move tree view horizontally table view (header) is not moving same amount as tree moved Instead it is jumping to half of the table.
    Below is the code, Please let me know If I am missing something here.


    Main:
    Qt Code:
    1. #include "treemodel.h"
    2. #include "treeview.h"
    3.  
    4. #include <QApplication>
    5. #include <QFile>
    6. #include <QTreeView>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QFile file(":/default.txt");
    13. file.open(QIODevice::ReadOnly);
    14. TreeModel model(file.readAll());
    15. file.close();
    16.  
    17. treeView view1;
    18. view1.setModel(&model);
    19. view1.show();
    20. view1.setHeaderSettings();
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 


    view:
    Qt Code:
    1. #ifndef TREEVIEW_H
    2. #define TREEVIEW_H
    3.  
    4. #include <QTableView>
    5. #include <QTreeView>
    6. #include <QDebug>
    7. #include <QHeaderView>
    8. #include <QScrollBar>
    9. #include "treeitem.h"
    10.  
    11.  
    12. class treeView : public QTreeView
    13. {
    14. Q_OBJECT
    15. public:
    16. treeView(QWidget *parent = 0);
    17. ~treeView();
    18.  
    19. void setHeaderSettings();
    20.  
    21.  
    22. protected:
    23. void resizeEvent(QResizeEvent *event) {
    24. setViewportMargins(0,60, 0, 0);
    25. m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
    26. }
    27. void showEvent(QShowEvent *) {
    28. setViewportMargins(0, m_header->sizeHint().height(), 0, 0);
    29. m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
    30. }
    31.  
    32. protected slots:
    33. void headerSectionResized(int,int,int);
    34. void SliderMoved(const int& val);
    35. void rangeChanged(int,int);
    36.  
    37. private:
    38. //CustomHeader *m_header;
    39. QTableView *m_header;
    40. };
    41.  
    42. #endif // TREEVIEW_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "treeview.h"
    2.  
    3.  
    4. treeView::treeView(QWidget *parent) : QTreeView(parent)
    5. {
    6. header()->hide();
    7. m_header = new QTableView(this);
    8. m_header->setFixedHeight(60);
    9. setGeometry(50, 50, 400, 400);
    10.  
    11. connect(m_header->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(headerSectionResized(int,int,int)));
    12. connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(SliderMoved(int)));
    13.  
    14. connect(m_header->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(rangeChanged(int,int)));
    15.  
    16. }
    17.  
    18. treeView::~treeView()
    19. {
    20.  
    21. }
    22.  
    23. void treeView::setHeaderSettings()
    24. {
    25. m_header->setModel(this->model());
    26. rangeChanged(horizontalScrollBar()->minimum(), horizontalScrollBar()->maximum());
    27. }
    28.  
    29. void treeView::headerSectionResized(int logIndex, int oldSize, int newSize)
    30. {
    31. setColumnWidth(logIndex, newSize);
    32. }
    33.  
    34. void treeView::SliderMoved(const int &val)
    35. {
    36. //qDebug() << "pos = " << horizontalScrollBar()->sliderPosition() << " val = " << val;
    37. m_header->horizontalScrollBar()->setValue(val);
    38.  
    39.  
    40. QScrollBar* s1 = m_header->horizontalScrollBar();
    41. QScrollBar* s2 = horizontalScrollBar();
    42.  
    43. qDebug() << "Header scroll bar: min= " << s1->minimum() << " max = " << s1->maximum() << " val = " << s1->value();
    44. qDebug() << "view scroll bar: min= " << s2->minimum() << " max = " << s2->maximum() << " val = " << s2->value();
    45. }
    46.  
    47. void treeView::rangeChanged(int min, int max)
    48. {
    49. //Eevn If I don't do this also, I could see same behavior
    50. QScrollBar* s2 = horizontalScrollBar();
    51. m_header->horizontalScrollBar()->setRange(s2->minimum(), s2->maximum());
    52.  
    53. qDebug() << "min = " << min << " max = " << max;
    54. }
    To copy to clipboard, switch view to plain text mode 



    Model I have taken from simple tree model & please let me know in case you need more information.


    Thanks in Advance.
    Last edited by prasad_N; 31st March 2016 at 11:40.
    Thanks :-)

  2. #2
    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: how to synchronize 2 view's (Table & tree) scrolls.

    Do the two scrollbars have the same range?

    If not, you might need to calculate the relative position and apply this to the other's range.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to synchronize 2 view's (Table & tree) scrolls.

    Quote Originally Posted by anda_skoa View Post
    Do the two scrollbars have the same range?_
    No, they are different. that is the reason I am just trying to adjust the header range to actual views range in below way, Now this range is getting changed but the behavior is same.

    Qt Code:
    1. connect(m_header->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(rangeChanged(int,int)));
    2. void treeView::rangeChanged(int min, int max)
    3. {
    4. //Eevn If I don't do this also, I could see same behavior
    5. QScrollBar* s2 = horizontalScrollBar();
    6. m_header->horizontalScrollBar()->setRange(s2->minimum(), s2->maximum());
    7.  
    8. qDebug() << "min = " << min << " max = " << max;
    9. }
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by anda_skoa View Post
    If not, you might need to calculate the relative position and apply this to the other's range._
    could you elaborate this little more.
    Thanks :-)

  4. #4
    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: how to synchronize 2 view's (Table & tree) scrolls.

    Quote Originally Posted by prasad_N View Post
    could you elaborate this little more.
    Let say one range is 0-100 and the other is 0-200, then a value of 20 in range 1 would be 40 in range 2

    Something like
    Qt Code:
    1. int range1 = r1Max - r1Min;
    2. int value1 = value - r1Min;
    3. double relValue = value1/double(range1);
    4.  
    5. int range2 = r2Max - r2Min;
    6. int value2 = range2 * relValue;
    7. value = value2 + r2Min;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to synchronize 2 view's (Table & tree) scrolls.

    This is some how change the behavior But not exactly the way I wanted. Because of the (int/double) header is moving step by step instead in-sync with actual view.
    Actual view is moving smoothly but header is moving step by step, this looks so odd to user, Even If I change both the header ranges to 0 to 100 I could see same behavior.

    I am attaching more code here, Please have a look. simpletreemodel.zip
    Qt5.4.0 & Compiled on Windows7.
    Thanks :-)

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to synchronize 2 view's (Table & tree) scrolls.

    Any help..?? Still struggling with this issue..!
    Thanks :-)

  7. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to synchronize 2 view's (Table & tree) scrolls.

    Using Table view/Table widget with QTreeview and trying to sync is not working as expected, But after bit experiments using two tree views is working as expect (being both the ranges are same its working as expected. so suggesting to use same view in case you need sync).
    Thanks :-)

Similar Threads

  1. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 1
    Last Post: 6th June 2015, 17:26
  2. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 6
    Last Post: 18th May 2015, 08:18
  3. table view and tree view
    By MKSPulok in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2012, 22:48
  4. Display sql table menu in a tree view
    By mat.brighi in forum Newbie
    Replies: 2
    Last Post: 19th October 2011, 00:49
  5. Replies: 33
    Last Post: 7th December 2009, 11:11

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.