Page 1 of 2 12 LastLast
Results 1 to 20 of 40

Thread: attach more widgets to a QScrollArea

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default attach more widgets to a QScrollArea

    Hi to all,
    I'm able to attach a widget to a scrollarea. I would know if it's possible to attach more than one qwidget to the same scrollarea.
    So when I move the scrollbar, both widgets should be moved.

    I tried with this code:
    Qt Code:
    1. /* scroll area */
    2. QScrollArea* sa = new QScrollArea( this );
    3. sa->setWidgetResizable( true );
    4. sa->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    5. sa->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    6.  
    7. QScrollBar *scrollBar = sa->horizontalScrollBar();
    8.  
    9. m_WaveDisplay = new WaveDisplay; //QWidget
    10. m_TimeDisplay = new TimeWidget; //QWidget
    11.  
    12. sa->setWidget(m_TimeDisplay);
    13. sa->setWidget(m_WaveDisplay);
    14.  
    15. QVBoxLayout* layout = new QVBoxLayout();
    16. layout->addWidget( sa );
    17.  
    18. ..more code..
    To copy to clipboard, switch view to plain text mode 

    but I get a crash when the application starts.
    Maybe is not possible?

    Best
    Last edited by franco.amato; 14th January 2010 at 17:34.
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    you can do it like this:
    Qt Code:
    1. QWidget w = new QWidget(this);
    2. lo->addWidget(m_TimeDisplay);
    3. lo->addWidget(m_WaveDisplay);
    4. w->setLayout(lo);
    5. sa->setWidget(w);
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by faldżip View Post
    you can do it like this:
    Qt Code:
    1. QWidget w = new QWidget(this);
    2. lo->addWidget(m_TimeDisplay);
    3. lo->addWidget(m_WaveDisplay);
    4. w->setLayout(lo);
    5. sa->setWidget(w);
    To copy to clipboard, switch view to plain text mode 
    It works, thank you
    Franco Amato

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by faldżip View Post
    you can do it like this:
    Qt Code:
    1. QWidget w = new QWidget(this);
    2. lo->addWidget(m_TimeDisplay);
    3. lo->addWidget(m_WaveDisplay);
    4. w->setLayout(lo);
    5. sa->setWidget(w);
    To copy to clipboard, switch view to plain text mode 
    I spoken too early.
    It doesn't work.
    In the ctor of the m_TimeDisplay I have
    Qt Code:
    1. setMinimumSize( QSize(800, 20) );
    2. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // it must expand only orizzontally
    To copy to clipboard, switch view to plain text mode 
    In the ctor of the m_WaveDisplay I have

    Qt Code:
    1. setMinimumSize( QSize(800, 250) );
    2. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // it must expand only orizzontally
    To copy to clipboard, switch view to plain text mode 

    Now in the ctor of the WaveWidget that must contains both m_TimeDisplay and m_WaveDisplay
    I have the code you suggested to me:

    Qt Code:
    1. /* scroll area */
    2. QScrollArea* sa = new QScrollArea( this );
    3. sa->setWidgetResizable( true );
    4. sa->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    5. sa->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    6.  
    7. QScrollBar *scrollBar = sa->horizontalScrollBar();
    8.  
    9. QWidget* w = new QWidget();
    10. w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    11. w->setMinimumSize(800, 271); // 271 = 250(m_WaveDisplay) + 20(m_TimeDisplay) + 1
    12.  
    13. il->addWidget( m_TimeDisplay );
    14. il->addWidget( m_WaveDisplay );
    15. w->setLayout(il);
    16. sa->setWidget(w);
    17.  
    18. QVBoxLayout* layout = new QVBoxLayout();
    19.  
    20. layout->addWidget(sa);
    21. layout->addLayout( infoLayout );
    22. layout->addLayout(lo);
    23. setLayout(layout);
    To copy to clipboard, switch view to plain text mode 

    The result is in this image ( also attached )image.jpg
    You can see that there are some not desired spaces and the widget w doesn't occupies all scrollarea surface.

    I don't know where I'm wrong.

    Best Regards,
    Franco
    Franco Amato

  5. #5
    Join Date
    Oct 2008
    Location
    Boston
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    You can see that there are some not desired spaces
    check out QBoxLayout::setSpacing()

    the widget w doesn't occupies all scrollarea surface
    check out QAbstractScrollArea::setViewportMargins()

  6. #6
    Join Date
    Oct 2008
    Location
    Boston
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    nevermind on my 2nd tip - QAbstractScrollArea::setViewportMargins() is protected

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: attach more widgets to a QScrollArea

    Add a spacer after the last widget and also play with setWidgetResizable().
    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. #8
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by pezmaster31 View Post
    check out QBoxLayout::setSpacing()
    I set il->setSpacing(1) ans the above space is reduced to 1 pixel but I still have problems with the bottom space.
    I'm trying all.
    I'm sure the problem is in set the parents, in the logic.

    check out QAbstractScrollArea::setViewportMargins()
    I tried to call it but is protected and doc says that default values are 0 ( what I want, overall for the bottom space ).
    The widget doesn't fit well the scroll area.
    When I try to resize the widget ( with a zoom ) for an instant it fit all scroll area and then it come back to the previous size.
    Something as a pulse.

    Any idea?
    Franco Amato

  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by wysota View Post
    Add a spacer after the last widget and also play with setWidgetResizable().
    Hi,
    how can add a spacer? I don't use designer. I played with setWidgetResizable but if I set it to false I can not zoom,
    what I don't want.

    Thank you
    Franco Amato

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: attach more widgets to a QScrollArea

    Quote Originally Posted by franco.amato View Post
    how can add a spacer?
    QSpacerItem
    I played with setWidgetResizable but if I set it to false I can not zoom,
    I don't really know what you mean by "zoom" in this situation. In general this property should be set rather to true than to false (the latter is default).
    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.


  11. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by wysota View Post
    QSpacerItem

    I don't really know what you mean by "zoom" in this situation. In general this property should be set rather to true than to false (the latter is default).
    Dear wysota in my case the m_TimeDisplay and m_WaveDisplay are used to show an audio wave were user can zoom in/out to navigate into the sound.
    I would attach such widgets to a QScrollArea to leave to Qt manage when user zoom.

    This is what I meant
    Franco Amato

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: attach more widgets to a QScrollArea

    That's not the way to go, really... Nevertheless, I don't see the problem. Would you care to explain?
    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.


  13. #13
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by wysota View Post
    That's not the way to go, really... Nevertheless, I don't see the problem. Would you care to explain?
    Which is the way to go?

    Well the problem is that the widget doesn't fit the scroll area.
    I'm not sure the logic I used is correct ( assign the correct parent of every widget )
    Franco Amato

  14. #14
    Join Date
    Oct 2008
    Location
    Boston
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Not sure if this is the ideal way to achieve this sort of behavior... but this seems to work on my end (if I understand you correctly)

    Qt Code:
    1. // set size policy for your WaveDisplay widget to use as much vertical space on its parent as is available
    2. m_WaveDisplay->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    3.  
    4. il->setSpacing(0);
    5. il->addWidget(m_TimeDisplay);
    6. il->addWidget(m_WaveDisplay);
    7.  
    8. QWidget* w = new QWidget();
    9. // you had the widget's vertical size policy set to Fixed, so there's no way it could ever expand vertically, changed to Ignored (same as WaveDisplay)
    10. // w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    11. w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    12. w->setMinimumSize(800, 271); // 271 = 250(m_WaveDisplay) + 20(m_TimeDisplay) + 1
    13. w->setLayout(il);
    14.  
    15. sa->setWidgetResizable(true);
    16. sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    17. sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    18. sa->setWidget(w);
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: attach more widgets to a QScrollArea

    Quote Originally Posted by franco.amato View Post
    Which is the way to go?
    Either a completely custom widget derived from QAbstractScrollArea or Graphics View.

    Well the problem is that the widget doesn't fit the scroll area.
    setWidgetResizable(true) would fix the problem.
    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.


  16. #16
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by wysota View Post
    Either a completely custom widget derived from QAbstractScrollArea or Graphics View.


    setWidgetResizable(true) would fix the problem.
    Hi, I already set it to true. It doesn't solve the problem. I posted above the most important code. If you can give a look you can see that the setWidgetResizable(true) is set to true
    Franco Amato

  17. #17
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by wysota View Post
    Either a completely custom widget derived from QAbstractScrollArea or Graphics View.
    I can rewrite it if it's the case. Can you give me some hints on how to do it? As now I inherited from QWidget.
    I wrote WaveWidget : QWidget.
    WaveWidget contains vertically aligned
    1) TimeDisplay : QWidget
    2) WaveDisplay : QWidget

    I would attach a QScrollArea to WaveWidget as I said to allow user zoom without results
    Franco Amato

  18. #18
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    Quote Originally Posted by pezmaster31 View Post
    Not sure if this is the ideal way to achieve this sort of behavior... but this seems to work on my end (if I understand you correctly)

    Qt Code:
    1. // set size policy for your WaveDisplay widget to use as much vertical space on its parent as is available
    2. m_WaveDisplay->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    3.  
    4. il->setSpacing(0);
    5. il->addWidget(m_TimeDisplay);
    6. il->addWidget(m_WaveDisplay);
    7.  
    8. QWidget* w = new QWidget();
    9. // you had the widget's vertical size policy set to Fixed, so there's no way it could ever expand vertically, changed to Ignored (same as WaveDisplay)
    10. // w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    11. w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
    12. w->setMinimumSize(800, 271); // 271 = 250(m_WaveDisplay) + 20(m_TimeDisplay) + 1
    13. w->setLayout(il);
    14.  
    15. sa->setWidgetResizable(true);
    16. sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    17. sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    18. sa->setWidget(w);
    To copy to clipboard, switch view to plain text mode 
    Hi now the widget fit the scroill area but I still have some problems.
    1) when I resize the widget ( for example user zoom in ) the widget seems to pulse. It grow and shrank (in the orizontally direction that must be fixed) continuosly every time user press zoom button
    2) The scroll bar is not shown. The widget grow ( in a bad way ) but I can not navigate in it with the scroll bar.

    May be my code of zoom is not correct.
    This is the code
    Qt Code:
    1. // executed when user press zoonin button
    2. void WaveWidget::zoomIn()
    3. {
    4. float f = m_ZoomFactor + 0.12;
    5. qDebug() << "WaveWidget::zoomIn - f: " << f;
    6. m_WaveDisplay->setZoomFactor( f );
    7. }
    To copy to clipboard, switch view to plain text mode 

    And then the code to expand the WaveDisplay

    Qt Code:
    1. /************************************************************************/
    2. /* setZoomFactor */
    3. /************************************************************************/
    4. void WaveDisplay::setZoomFactor( float f )
    5. {
    6. int w, h;
    7.  
    8. w = width() * f;
    9. h = height();
    10.  
    11. setMinimumSize( w, h );
    12. QWidget* p = dynamic_cast<QWidget*>( parent() );
    13. if (p)
    14. resize( p->width(), p->height() );
    15. repaint();
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'm sure my code is wrong but I don't know where...
    The zoom is wrote in very few time and I'm not sure is a correct way to do it.
    The code of the zoom of TimeDisplay is not implemented yet..

    Regards
    Franco Amato

  19. #19
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: attach more widgets to a QScrollArea

    I changed the code of the zoom so:
    Qt Code:
    1. /************************************************************************/
    2. /* setZoomFactor */
    3. /************************************************************************/
    4. void WaveDisplay::setZoomFactor( float f )
    5. {
    6. int w, h;
    7.  
    8. w = width() * f;
    9. h = height();
    10.  
    11. setMinimumSize( w, h );
    12. QWidget* p = dynamic_cast<QWidget*>( parent() );
    13. if (p)
    14. {
    15. //resize( p->width(), p->height() );
    16. resize(w, h); // <--------------------------------------------------------CHANGED
    17. }
    18.  
    19. repaint();
    20. }
    To copy to clipboard, switch view to plain text mode 

    And now I don't get the "grow and shrank" effect when I zoom. But the status bar still doesn't appear.
    I can not navigate into the wave.
    Any idea?

    Best
    Last edited by franco.amato; 16th January 2010 at 01:40. Reason: errors in the post
    Franco Amato

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: attach more widgets to a QScrollArea

    Quote Originally Posted by franco.amato View Post
    The zoom is wrote in very few time and I'm not sure is a correct way to do it.
    Defintely not

    You are artificially manipulating the size of each child widget which is not a very good idea. Using graphics view or implementing everything as one widget derived from QAbstractScrollArea would be a much better approach.
    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.


Similar Threads

  1. Adding widgets to QScrollArea
    By ser_bur in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2013, 10:38
  2. QSharedMemory won't attach
    By MattPhillips in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2009, 15:45
  3. Replies: 2
    Last Post: 10th March 2008, 20:16
  4. QScrollArea With Custom Widgets
    By VireX in forum Qt Programming
    Replies: 30
    Last Post: 22nd April 2007, 16:48

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.