Results 1 to 5 of 5

Thread: QScrollArea -- Scroll to widget problem

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QScrollArea -- Scroll to widget problem

    Hi,

    I have a scrollarea that contains buttons. When I add a button to the area and immediately call ensureWidgetVisible for that button, the scrollarea does not moves to the widget however, if I call ensureWidgetVisible from another button it works. Seem to me that it something related to call this function immediately after adding a button.

    Here is a code the mimics the error:

    main.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7.  
    8. QWidget window;
    9. window.resize(320, 240);
    10. QScrollArea *scrollArea;
    11. QWidget *scrollAreaWidgetContents_2;
    12. QVBoxLayout *verticalLayout;
    13. QHBoxLayout *horizontalLayout_2;
    14.  
    15. scrollArea = new QScrollArea(&window);
    16. scrollArea->setGeometry(QRect(1, 1, 231, 61));
    17. scrollArea->setWidgetResizable(true);
    18. scrollAreaWidgetContents_2 = new QWidget();
    19. scrollAreaWidgetContents_2->setGeometry(QRect(0, 0, 238, 36));
    20. verticalLayout = new QVBoxLayout(scrollAreaWidgetContents_2);
    21. horizontalLayout_2 = new QHBoxLayout();
    22. verticalLayout->addLayout(horizontalLayout_2);
    23. scrollArea->setWidget(scrollAreaWidgetContents_2);
    24.  
    25. window.show(); //Will not work
    26.  
    27. QList<QPushButton *> buttons;
    28. buttons.append(new QPushButton("Button 1",scrollAreaWidgetContents_2));
    29. buttons.append(new QPushButton("Button 2",scrollAreaWidgetContents_2));
    30. buttons.append(new QPushButton("Button 3",scrollAreaWidgetContents_2));
    31. buttons.append(new QPushButton("Button 4",scrollAreaWidgetContents_2));
    32. buttons.append(new QPushButton("Button 5",scrollAreaWidgetContents_2));
    33. int pos;
    34. for (pos = 0; pos<=buttons.count()-1;pos++)
    35. {
    36. buttons[pos]->setGeometry(QRect(80, 330, 112, 26));
    37. }
    38. for (pos = 0; pos<=buttons.count()-1;pos++)
    39. {
    40. horizontalLayout_2->addWidget(buttons[pos]);
    41. }
    42.  
    43. //window.show(); //Will work
    44.  
    45. scrollArea->ensureWidgetVisible(buttons[3],0,0);
    46.  
    47. return a.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    In this example the ensureWidgetVisible fuction will work depending on where I have the show function.

    Any ideas what I am missing? Some refresh before calling ensureWidgetVisible?

    Many thanks.

  2. #2
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea -- Scroll to widget problem

    First - I recommend to read QScrollArea::setWidget. There are some dependencies according to order in which objects are added.
    After show() there are some changes and recreation of QScrollArea internal layouts. After that - he can says where selected widgets are and move to that position.
    So - create enitre internal widget and then add it as main widget for scrollarea.

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea -- Scroll to widget problem

    To create the entire internal widget beforehand will work in the given example however, it it will not work for example: if a user clicks on a "Add new" button to add a new element to the scrollview because this changes internal layouts. For example:

    Add_new_clicked()
    Qt Code:
    1. buttons.append(new QPushButton("New button",scrollAreaWidgetContents_2));
    2. buttons[buttons.count()-1]->setGeometry(QRect(80, 330, 112, 26));
    3. horizontalLayout_2->addWidget(buttons[buttons.count()-1]); //Add the new element and it is show in the screen
    4. //How to move to this new element?
    5. scrollArea->ensureWidgetVisible(buttons[buttons.count()-1],0,0); //This does not work.
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea -- Scroll to widget problem --Solved

    I'm replying to my post.

    Indeed this happens because the scroll area's widget changes it geometry with every button that is added. Thus, ensureWidgetVisible cannot be called immediately because the scroll area does not know its final size yet. A way around is to subclass a QWidget and emit a signal after the widget size changes (by reimplementing the resizeEvent) + setting this widget as the scroll areas's widget. After connecting the signal, ensureWidgetVisible can properly scroll to the added widget.

  5. #5
    Join Date
    Sep 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea -- Scroll to widget problem

    Hello I had the same problem and managed to fix it quite easy: Call qApp->processEvents(); before calling the ensureWidgetVisible() method.

    The idea is that when the geometry changes, events for doing that are pushed on the event queue... So you execute the events to make the geometry changes and then you can ensure the widget is visible. Of course, these things are happening in the UI thread...

    I hope this helps someone out there. Cheers!

Similar Threads

  1. Scroll Bar not appearing in QScrollArea
    By coolkaps in forum Newbie
    Replies: 1
    Last Post: 2nd May 2010, 16:12
  2. QScrollArea inner widget problem
    By agaf in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2010, 18:06
  3. Force a Scroll in a QScrollArea
    By nmuntz in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2009, 13:49
  4. Auto scroll down in QScrollArea
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2008, 12:51
  5. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27

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.