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:
Code:
#include "treemodel.h"
#include "treeview.h"
#include <QApplication>
#include <QFile>
#include <QTreeView>
int main(int argc, char *argv[])
{
QFile file(":/default.txt");
TreeModel model(file.readAll());
file.close();
treeView view1;
view1.setModel(&model);
view1.show();
view1.setHeaderSettings();
return app.exec();
}
view:
Code:
#ifndef TREEVIEW_H
#define TREEVIEW_H
#include <QTableView>
#include <QTreeView>
#include <QDebug>
#include <QHeaderView>
#include <QScrollBar>
#include "treeitem.h"
{
Q_OBJECT
public:
~treeView();
void setHeaderSettings();
protected:
setViewportMargins(0,60, 0, 0);
m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
}
setViewportMargins(0, m_header->sizeHint().height(), 0, 0);
m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
}
protected slots:
void headerSectionResized(int,int,int);
void SliderMoved(const int& val);
void rangeChanged(int,int);
private:
//CustomHeader *m_header;
};
#endif // TREEVIEW_H
Code:
#include "treeview.h"
{
header()->hide();
m_header->setFixedHeight(60);
setGeometry(50, 50, 400, 400);
connect(m_header->horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(headerSectionResized(int,int,int)));
connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(SliderMoved(int)));
connect(m_header->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(rangeChanged(int,int)));
}
treeView::~treeView()
{
}
void treeView::setHeaderSettings()
{
m_header->setModel(this->model());
rangeChanged(horizontalScrollBar()->minimum(), horizontalScrollBar()->maximum());
}
void treeView::headerSectionResized(int logIndex, int oldSize, int newSize)
{
setColumnWidth(logIndex, newSize);
}
void treeView::SliderMoved(const int &val)
{
//qDebug() << "pos = " << horizontalScrollBar()->sliderPosition() << " val = " << val;
m_header->horizontalScrollBar()->setValue(val);
qDebug() << "Header scroll bar: min= " << s1->minimum() << " max = " << s1->maximum() << " val = " << s1->value();
qDebug() << "view scroll bar: min= " << s2->minimum() << " max = " << s2->maximum() << " val = " << s2->value();
}
void treeView::rangeChanged(int min, int max)
{
//Eevn If I don't do this also, I could see same behavior
m_header->horizontalScrollBar()->setRange(s2->minimum(), s2->maximum());
qDebug() << "min = " << min << " max = " << max;
}
Model I have taken from simple tree model & please let me know in case you need more information.
Thanks in Advance.
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,
_
Re: how to synchronize 2 view's (Table & tree) scrolls.
Quote:
Originally Posted by
anda_skoa
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.
Code:
connect(m_header->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(rangeChanged(int,int)));
void treeView::rangeChanged(int min, int max)
{
//Eevn If I don't do this also, I could see same behavior
m_header->horizontalScrollBar()->setRange(s2->minimum(), s2->maximum());
qDebug() << "min = " << min << " max = " << max;
}
Quote:
Originally Posted by
anda_skoa
If not, you might need to calculate the relative position and apply this to the other's range._
could you elaborate this little more.
Re: how to synchronize 2 view's (Table & tree) scrolls.
Quote:
Originally Posted by
prasad_N
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
Code:
int range1 = r1Max - r1Min;
int value1 = value - r1Min;
double relValue = value1/double(range1);
int range2 = r2Max - r2Min;
int value2 = range2 * relValue;
value = value2 + r2Min;
Cheers,
_
1 Attachment(s)
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. Attachment 11843
Qt5.4.0 & Compiled on Windows7.
Re: how to synchronize 2 view's (Table & tree) scrolls.
Any help..?? Still struggling with this issue..!
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).