QGraphicsView and QTreeView synchronous scrolling
-
Hi,
I have coded a GUI where there are QGraphicsView and QTreeView placed, loaded, activated and used.
There is a need to move QGraphicsScene, that is "assigned" ( set ) to the QGraphicsView downward and upward ( a.k.a to scroll vertically and horizontally ) using set scroll bars ( scroll bar policy set to both widgets as ScrollBarAlwaysOn ) synchronously with QTreeView, so that when one of the widgets is scrolled on some number of pixels, to scroll the second as well.
The widgets scrolling connected using next code:
@ connect(ui->treeView,SIGNAL(scrolled(int,int)),this,SLOT(scroll_gv(int,int)));
connect(ui->graphicsView,SIGNAL(scrolled(int,int)),this,SLOT(scroll_tv(int,int)));...
void MainWindow::scroll_gv(int dx,int dy)
{
ui->graphicsView->scroll(dx,dy);
}void MainWindow::scroll_tv(int dx,int dy)
{
ui->treeView->scroll(dx,dy);
}@where the scroll is added to custom classes inherited from QGraphicsView and QTreeView and virtual function reimplemented:
@void QCTreeView::scrollContentsBy ( int dx, int dy )
{
emit scrolled(dx,dy);
}void qcgraphicsview::scrollContentsBy ( int dx, int dy )
{
emit scrolled(dx,dy);
}@The problem to correct is next, when I scroll one of the widgets, the second is scrolled as well, but somehow not accurate - all the area of the widget is scrolled and not a widgets internals,
The scrolling behavior is recorded in next video clip:
"https://www.box.com/s/lpaon61oly45fffyu3kn":https://www.box.com/s/lpaon61oly45fffyu3kn
Will you hint why such problem, what properties of the widgets to set, how to code correctly in order the synchronous scroll will work ?
Thanks in advance for your help.
-
"
Why not just connect valueChanged() signals of vertical (or horizontal, where appropriate) scrollbars of both widgets to their setValue() counterparts?
"
-- Tnx, recently connected next way:@connect(ui->treeView->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(scroll_gv_v(int)));
connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(scroll_tv_v(int)));//...
void MainWindow::scroll_gv_v(int dy)
{
qDebug()<<"*** in scroll_gv_v, dy = "<<dy<<endl;ui->graphicsView->verticalScrollBar()->setValue(dy); ui->graphicsView->verticalScrollBar()->setSliderPosition(dy); ui->graphicsView->verticalScrollBar()->update(); ui->graphicsView->verticalScrollBar()->repaint(); ui->graphicsView->update(); ui->graphicsView->repaint(); qDebug()<<"*** in scroll_gv_v, tree view scroll bar value = "<<ui->treeView->verticalScrollBar()->value()<<endl; qDebug()<<"*** in scroll_gv_v, graphics view scroll bar value = "<<ui->graphicsView->verticalScrollBar()->value()<<endl;
}
void MainWindow::scroll_tv_v(int dy)
{
qDebug()<<"*** in scroll_tv_v, dy = "<<dy<<endl;ui->treeView->verticalScrollBar()->setValue(dy); ui->treeView->verticalScrollBar()->setSliderPosition(dy); ui->treeView->verticalScrollBar()->update(); ui->treeView->verticalScrollBar()->repaint(); ui->treeView->update(); ui->treeView->repaint(); qDebug()<<"*** in scroll_tv_v, tree view scroll bar value = "<<ui->treeView->verticalScrollBar()->value()<<endl; qDebug()<<"*** in scroll_tv_v, graphics view scroll bar value = "<<ui->graphicsView->verticalScrollBar()->value()<<endl;
}@
then qDebug() shows, that the verticalScrollBar() value of the counterparts stays the same after the signals called and finished and counterpart scrollbars of the connected widgets do not move at all then. Why the value of the counterpart verticalScrollBar() wasn't changed then in such implementation ?
when I changed
@
void MainWindow::scroll_gv(int dx,int dy)
{
ui->graphicsView->scroll(dx,dy);
}
void MainWindow::scroll_tv(int dx,int dy)
{
ui->treeView->scroll(dx,dy);
}@to:
[CODE]
void MainWindow::scroll_gv(int dx,int dy)
{
ui->graphicsView->viewport()->scroll(dx,dy);
}void MainWindow::scroll_tv(int dx,int dy)
{
ui->treeView->viewport()->scroll(dx,dy);
}[/CODE]then the internal widgets move more close fashion to the desired results, but still scroll bars frozen while moving and internals of the widgets are degraded during scrolling, see video recorded:
"https://www.box.com/s/irv4qqgmxmewvydc8ual":[url]https://www.box.com/s/irv4qqgmxmewvydc8ual[/url]
The question is how to correct that, what widgets properties to set to what value ? How to code the scrolling ?
Thanks.