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:
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:
Code:
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
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.
Re: QGraphicsView and QTreeView synchronous scrolling
Why not just connect valueChanged() signals of vertical (or horizontal, where appropriate) scrollbars of both widgets to their setValue() counterparts?
Re: QGraphicsView and QTreeView synchronous scrolling
1.
"
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:
Code:
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 ?
2.
+
when I changed
Code:
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);
}
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":https://www.box.com/s/irv4qqgmxmewvydc8ual
The question is how to correct that, what widgets properties to set to what value ? How to code the scrolling ?
Thanks.
Re: QGraphicsView and QTreeView synchronous scrolling
I told you exactly what to do -- connect to the setValue() slot, not to a custom slot that does a dozen random things.
Code:
#include <QtGui>
int main(int argc, char **argv) {
for(int i
=0;i<
100;
++i
) { list <<
QString::number(i
+1);
} model.setStringList(list);
l1->setModel(&model);
l2->setModel(&model);
QObject::connect(l1
->verticalScrollBar
(),
SIGNAL(valueChanged
(int)), l2
->verticalScrollBar
(),
SLOT(setValue
(int)));
QObject::connect(l2
->verticalScrollBar
(),
SIGNAL(valueChanged
(int)), l1
->verticalScrollBar
(),
SLOT(setValue
(int)));
l->addWidget(l1);
l->addWidget(l2);
w.show();
return app.exec();
}
Re: QGraphicsView and QTreeView synchronous scrolling
Hi,
Thank you Mr.Witold for the help. I connected them the way you adviced:
Code:
connect(ui->treeView->verticalScrollBar(),SIGNAL(valueChanged(int)),ui->graphicsView->verticalScrollBar(),SLOT(setValue(int)));
connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)),ui->treeView->verticalScrollBar(),SLOT(setValue(int)));
I have such problem: on custom QGraphicsView I add QGraphicsScene and put to the screen multiple custom items such that I expect my QGraphicsView scolls to see all the items, but they does not. I set ScrollBarAlwaysOn. I see scrollbars, but they doesn't updated. The same set QTreeView does update on scroll, but QGraphicsView doesn't. Maybe to do something with viewport() ? How to call setWidgetResizable(true) on QGraphicsView?.See video example attached:
https://www.box.com/s/mhumghw4ad3nlj5ttl83
for example, in this video it is depicted how the QGraphicsView moves horizontally, but it doesn’t move vertically, disregards, that there are several items downmost.
https://www.box.com/s/o35b0p5pwyl5upn02z8z
What are the reasons ?