Results 1 to 5 of 5

Thread: QGraphicsView and QTreeView synchronous scrolling

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:

    Qt Code:
    1. connect(ui->treeView,SIGNAL(scrolled(int,int)),this,SLOT(scroll_gv(int,int)));
    2.  
    3. connect(ui->graphicsView,SIGNAL(scrolled(int,int)),this,SLOT(scroll_tv(int,int)));
    4.  
    5.  
    6.  
    7. ...
    8.  
    9.  
    10.  
    11.  
    12.  
    13. void MainWindow::scroll_gv(int dx,int dy)
    14.  
    15. {
    16.  
    17. ui->graphicsView->scroll(dx,dy);
    18.  
    19. }
    20.  
    21.  
    22.  
    23. void MainWindow::scroll_tv(int dx,int dy)
    24.  
    25. {
    26.  
    27. ui->treeView->scroll(dx,dy);
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    where the scroll is added to custom classes inherited from QGraphicsView and QTreeView and virtual function reimplemented:

    Qt Code:
    1. void QCTreeView::scrollContentsBy ( int dx, int dy )
    2.  
    3. {
    4.  
    5. emit scrolled(dx,dy);
    6.  
    7. }
    8.  
    9.  
    10.  
    11. void qcgraphicsview::scrollContentsBy ( int dx, int dy )
    12.  
    13. {
    14.  
    15. emit scrolled(dx,dy);
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. The following user says thank you to freely for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:


  5. #3
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:

    Qt Code:
    1. connect(ui->treeView->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(scroll_gv_v(int)));
    2. connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(scroll_tv_v(int)));
    3.  
    4. //...
    5.  
    6.  
    7. void MainWindow::scroll_gv_v(int dy)
    8. {
    9. qDebug()<<"*** in scroll_gv_v, dy = "<<dy<<endl;
    10.  
    11.  
    12. ui->graphicsView->verticalScrollBar()->setValue(dy);
    13.  
    14. ui->graphicsView->verticalScrollBar()->setSliderPosition(dy);
    15.  
    16. ui->graphicsView->verticalScrollBar()->update();
    17.  
    18. ui->graphicsView->verticalScrollBar()->repaint();
    19.  
    20. ui->graphicsView->update();
    21.  
    22. ui->graphicsView->repaint();
    23.  
    24. qDebug()<<"*** in scroll_gv_v, tree view scroll bar value = "<<ui->treeView->verticalScrollBar()->value()<<endl;
    25.  
    26. qDebug()<<"*** in scroll_gv_v, graphics view scroll bar value = "<<ui->graphicsView->verticalScrollBar()->value()<<endl;
    27.  
    28. }
    29.  
    30. void MainWindow::scroll_tv_v(int dy)
    31. {
    32. qDebug()<<"*** in scroll_tv_v, dy = "<<dy<<endl;
    33.  
    34.  
    35. ui->treeView->verticalScrollBar()->setValue(dy);
    36.  
    37. ui->treeView->verticalScrollBar()->setSliderPosition(dy);
    38.  
    39. ui->treeView->verticalScrollBar()->update();
    40.  
    41. ui->treeView->verticalScrollBar()->repaint();
    42.  
    43. ui->treeView->update();
    44.  
    45. ui->treeView->repaint();
    46.  
    47. qDebug()<<"*** in scroll_tv_v, tree view scroll bar value = "<<ui->treeView->verticalScrollBar()->value()<<endl;
    48.  
    49. qDebug()<<"*** in scroll_tv_v, graphics view scroll bar value = "<<ui->graphicsView->verticalScrollBar()->value()<<endl;
    50.  
    51. }
    To copy to clipboard, switch view to plain text mode 


    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
    Qt Code:
    1. void MainWindow::scroll_gv(int dx,int dy)
    2. {
    3. ui->graphicsView->scroll(dx,dy);
    4. }
    5. void MainWindow::scroll_tv(int dx,int dy)
    6. {
    7. ui->treeView->scroll(dx,dy);
    8. }
    To copy to clipboard, switch view to plain text mode 

    to:
    Qt Code:
    1. void MainWindow::scroll_gv(int dx,int dy)
    2. {
    3. ui->graphicsView->viewport()->scroll(dx,dy);
    4. }
    5.  
    6. void MainWindow::scroll_tv(int dx,int dy)
    7. {
    8. ui->treeView->viewport()->scroll(dx,dy);
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by freely; 11th January 2013 at 12:59.

  6. The following user says thank you to freely for this useful post:


  7. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. for(int i=0;i<100;++i) { list << QString::number(i+1); }
    6. model.setStringList(list);
    7. QListView *l1 = new QListView;
    8. QListView *l2 = new QListView;
    9. l1->setModel(&model);
    10. l2->setModel(&model);
    11. QObject::connect(l1->verticalScrollBar(), SIGNAL(valueChanged(int)), l2->verticalScrollBar(), SLOT(setValue(int)));
    12. QObject::connect(l2->verticalScrollBar(), SIGNAL(valueChanged(int)), l1->verticalScrollBar(), SLOT(setValue(int)));
    13. QHBoxLayout *l = new QHBoxLayout(&w);
    14. l->addWidget(l1);
    15. l->addWidget(l2);
    16. w.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:


  9. #5
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsView and QTreeView synchronous scrolling

    Hi,
    Thank you Mr.Witold for the help. I connected them the way you adviced:
    Qt Code:
    1. connect(ui->treeView->verticalScrollBar(),SIGNAL(valueChanged(int)),ui->graphicsView->verticalScrollBar(),SLOT(setValue(int)));
    2. connect(ui->graphicsView->verticalScrollBar(),SIGNAL(valueChanged(int)),ui->treeView->verticalScrollBar(),SLOT(setValue(int)));
    To copy to clipboard, switch view to plain text mode 
    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 ?
    Last edited by freely; 16th January 2013 at 14:29.

  10. The following user says thank you to freely for this useful post:


Similar Threads

  1. QGraphicsView and Scrolling
    By validator in forum Qt Programming
    Replies: 5
    Last Post: 8th September 2017, 00:27
  2. QTreeView problem scrolling to end.
    By seneca in forum Qt Programming
    Replies: 7
    Last Post: 22nd December 2015, 12:08
  3. Replies: 3
    Last Post: 25th May 2010, 10:29
  4. Custom scrolling of QGraphicsView
    By hb in forum Qt Programming
    Replies: 0
    Last Post: 12th August 2008, 10:10
  5. QGraphicsView scrolling problem with 4.3.0
    By hb in forum Qt Programming
    Replies: 8
    Last Post: 30th August 2007, 22:18

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.