Results 1 to 7 of 7

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 10:40.
    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, 16: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, 07:18
  3. table view and tree view
    By MKSPulok in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2012, 21:48
  4. Display sql table menu in a tree view
    By mat.brighi in forum Newbie
    Replies: 2
    Last Post: 18th October 2011, 23:49
  5. Replies: 33
    Last Post: 7th December 2009, 10: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.