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:
#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();
}
#include "treemodel.h"
#include "treeview.h"
#include <QApplication>
#include <QFile>
#include <QTreeView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
treeView view1;
view1.setModel(&model);
view1.show();
view1.setHeaderSettings();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
view:
#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
#ifndef TREEVIEW_H
#define TREEVIEW_H
#include <QTableView>
#include <QTreeView>
#include <QDebug>
#include <QHeaderView>
#include <QScrollBar>
#include "treeitem.h"
class treeView : public QTreeView
{
Q_OBJECT
public:
treeView(QWidget *parent = 0);
~treeView();
void setHeaderSettings();
protected:
void resizeEvent(QResizeEvent *event) {
setViewportMargins(0,60, 0, 0);
m_header->setGeometry(0, 0, viewport()->width(), m_header->sizeHint().height());
}
void showEvent(QShowEvent *) {
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;
QTableView *m_header;
};
#endif // TREEVIEW_H
To copy to clipboard, switch view to plain text mode
#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;
}
#include "treeview.h"
treeView::treeView(QWidget *parent) : QTreeView(parent)
{
header()->hide();
m_header = new QTableView(this);
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);
QScrollBar* s1 = m_header->horizontalScrollBar();
QScrollBar* s2 = horizontalScrollBar();
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
QScrollBar* s2 = horizontalScrollBar();
m_header->horizontalScrollBar()->setRange(s2->minimum(), s2->maximum());
qDebug() << "min = " << min << " max = " << max;
}
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.
Bookmarks