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

Thread: Add QScrollArea to QDialog

  1. #1
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Add QScrollArea to QDialog

    Hi,

    Is it possible to create a Qdialog with a QScrollArea?

    I've tried the following but neither method works:

    Qt Code:
    1. QScrollArea scrollArea;
    2. scrollArea.viewport()->setBackgroundRole(QPalette::Dark);
    3. scrollArea.viewport()->setAutoFillBackground(true);
    4. scrollArea.setWindowTitle(QObject::tr("Mixer Strip"));
    5. scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    6. scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    7. scrollArea.setParent(this);
    8. scrollArea.show();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QScrollArea scroll;
    2. scroll.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    3. scroll.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    4. QWidget *viewport = new QWidget;
    5. scroll.setWidget(viewport);
    6. scroll.setWidgetResizable(true);
    7. QVBoxLayout *l = new QVBoxLayout(viewport);
    8. setLayout(l);
    9. scroll.show();
    To copy to clipboard, switch view to plain text mode 

    Cheers,

    Chris

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Add QScrollArea to QDialog

    The second example seems to be correct, but what is the parent of QScrollArea?

  3. #3
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    If I change the code to the following I still don't see the QScrollArea:

    Qt Code:
    1. QScrollArea scroll;
    2. scroll.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    3. scroll.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    4. QWidget *viewport = new QWidget;
    5. scroll.setWidget(viewport);
    6. scroll.setWidgetResizable(true);
    7. QVBoxLayout *l = new QVBoxLayout(viewport);
    8. setLayout(l);
    9. scroll.setParent(this);
    10. scroll.show();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    Create QScrollArea on heap
    Qt Code:
    1. QScrollArea * scroll = new QScrollArea(this);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Hi,

    I tried the following:

    Qt Code:
    1. setWindowTitle("Mixer Strip");
    2. setMinimumSize(600, 350);
    3.  
    4. QScrollArea *scroll = new QScrollArea(this);
    5. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    6. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    7. QWidget *viewport = new QWidget;
    8. scroll->setWidget(viewport);
    9. scroll->setWidgetResizable(true);
    10. QHBoxLayout *l = new QHBoxLayout(viewport);
    11. setLayout(l);
    To copy to clipboard, switch view to plain text mode 

    Using this code the scrollbars are visible however they're positioned in the top left corner of the dialog and don't change position when the dialog is resized. I can just makeout the widgets that are added.

    Cheers,

    Chris

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    If you are adding widgets to QHBoxLayout *l then

    Qt Code:
    1. QHBoxLayout *l = new QHBoxLayout(viewport);
    2. viewport->setLayout(l);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 23rd June 2011 at 16:52.

  7. #7
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    I tried the following:

    Qt Code:
    1. QScrollArea *scroll = new QScrollArea(this);
    2. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    3. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    4. QWidget *viewport = new QWidget;
    5. scroll->setWidget(viewport);
    6. scroll->setWidgetResizable(true);
    7. QHBoxLayout *l = new QHBoxLayout(viewport);
    8. viewport->setLayout(l);
    To copy to clipboard, switch view to plain text mode 

    The scrollbars are visible however they're still not attached to the dialog.

    I also tried removing the call to

    Qt Code:
    1. scroll->setWidget(viewport)
    To copy to clipboard, switch view to plain text mode 

    In this case, the scrollbars are visible (and the size of the area they contain is larger) but they're still not attached to the dialog.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    I think I got you problem, not sure how you are using QDialog, let me put both the ways, check which is applicable to you

    1. If sub-classing QDialog, then in the constructor of the sub-calss
    Qt Code:
    1. MyDialog::MyDialog(QWidget * parent) : QDialog(parent)
    2. {
    3. QScrollArea *scroll = new QScrollArea(this);
    4. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    5. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    6.  
    7. QWidget *viewport = new QWidget(this);
    8. scroll->setWidget(viewport);
    9. scroll->setWidgetResizable(true);
    10.  
    11. QHBoxLayout *l = new QHBoxLayout(viewport);
    12. viewport->setLayout(l);
    13.  
    14. // add needed widgets to layout "l"
    15. for(int i = 0; i < 10; i++)
    16. l->addWidget(new QPushButton());
    17.  
    18. // Add a layout for QDialog
    19. QHBoxLayout *dialog_layout = new QHBoxLayout(this);
    20. dialog_layout->addWidget(scroll); // add scroll to the QDialog's layout
    21. setLayout(dialog_layout);
    22.  
    23. // show dialog
    24. show();
    25. }
    To copy to clipboard, switch view to plain text mode 

    1. If using QDialog directly, then
    Qt Code:
    1. QDialog * dialog = new QDialog(this); //my be you already have this
    2.  
    3. QScrollArea *scroll = new QScrollArea(dialog);
    4. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    5. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    6.  
    7. QWidget *viewport = new QWidget(dialog);
    8. scroll->setWidget(viewport);
    9. scroll->setWidgetResizable(true);
    10.  
    11. QHBoxLayout *l = new QHBoxLayout(viewport);
    12. viewport->setLayout(l);
    13.  
    14. // add needed widgets to layout "l"
    15. for(int i = 0; i < 10; i++)
    16. l->addWidget(new QPushButton());
    17.  
    18. // Add a layout for QDialog
    19. QHBoxLayout *dialog_layout = new QHBoxLayout(dialog);
    20. dialog->setLayout(dialog_layout);
    21. dialog->layout()->addWidget(scroll); // add scroll to the QDialog's layout
    22. dialog->show();
    To copy to clipboard, switch view to plain text mode 


    Edit: your actual problem is you are adding QScrollArea to QDialog, where as you should be adding QScrollArea to QDialog's layout
    Last edited by Santosh Reddy; 24th June 2011 at 02:02. Reason: updated contents

  9. The following user says thank you to Santosh Reddy for this useful post:

    cpsmusic (24th June 2011)

  10. #9
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Thanks, that fixed the problem.

  11. #10
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Although I've got the scrollbars showing now, I've run into another problem - the widgets are being spread too widely in the horizontal direction. What I'd like is for the widgets to bunch up on the left hand side of the layout. I tried using a QGridLayout however this didn't fix the problem. The code I'm using is below. One thing, the widget is a custom widget that has a fixed size. I'm implemented the sizeHint method returning the size of the widget.

    Qt Code:
    1. MixerDialog::MixerDialog(QWidget *parent) :
    2. QDialog(parent) {
    3.  
    4. setWindowTitle("Mixer");
    5. setMinimumSize(600, 350);
    6.  
    7. QScrollArea *scrollArea = new QScrollArea(this);
    8. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    9. scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    10.  
    11. QWidget *viewport = new QWidget(this);
    12. scrollArea->setWidget(viewport);
    13. scrollArea->setWidgetResizable(true);
    14.  
    15. QHBoxLayout *hlayout = new QHBoxLayout(viewport);
    16. hlayout->setMargin(0);
    17. hlayout->setSpacing(0);
    18. viewport->setLayout(hlayout);
    19.  
    20. // add needed widgets to layout "hlayout"
    21. int numberOfChannels = 10;
    22. for(int i = 0; i < numberOfChannels; i++) {
    23. MixerStrip *strip = new MixerStrip;
    24. hlayout->addWidget(strip);
    25. }
    26. hlayout->addStretch();
    27.  
    28. // Add a layout for QDialog
    29. QHBoxLayout *dialog_layout = new QHBoxLayout(this);
    30. dialog_layout->setMargin(0);
    31. dialog_layout->setSpacing(0);
    32. dialog_layout->addWidget(scrollArea); // add scrollArea to the QDialog's layout
    33. setLayout(dialog_layout);
    34.  
    35. // show dialog
    36. show();
    37. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    You code should work, hlayout->addStretch(); does the magic...Post a screenshot indicating the of the probelm

  13. #12
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Here's a screenshot of how the widgets are being laid out:

    Grab.tiff

    The addStretch() doesn't seem to push them left.

  14. #13
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Add QScrollArea to QDialog

    Have you tried hlayout->setSizeConstraint ( QLayout::SetFixedSize ); ?

  15. #14
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Quote Originally Posted by Rachol View Post
    Have you tried hlayout->setSizeConstraint ( QLayout::SetFixedSize ); ?
    I just tried what you've suggested however it didn't make any difference.

  16. #15
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Add QScrollArea to QDialog

    Can you try your code with QPushButton instead of MixerStrip and then see if the widgets are being laid out normally?

  17. #16
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    Ok, Your problem seems to be with in mixer widget, set proper size constraint to it, and also don't use any spacers if designing from Qt Designer (i mean don't place spacers in such a position which will cause the mixer widget to fit the area available)

  18. #17
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    I'm not clear about how to set the size constraint in the widget. I tried adding:

    Qt Code:
    1. setSizeConstraint ( QLayout::SetFixedSize );
    To copy to clipboard, switch view to plain text mode 

    to the MixerStrip (which inherits from QWidget) but the code won't build.

    I'm not using Qt Designer so I don't think that's the problem.
    Last edited by cpsmusic; 25th June 2011 at 11:08. Reason: Text wrong!

  19. #18
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Add QScrollArea to QDialog

    setSizeConstraint -> this is QLayout API, so of course it won't work. Have you tried what I suggested in my previous post? How did it work? How is your MixerStrip widget implemented?

  20. #19
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Add QScrollArea to QDialog

    You can use
    Qt Code:
    1. hlayout.setSizeConstraint(QLayout::SetMinimumSize);
    To copy to clipboard, switch view to plain text mode 
    This will set the layout and the containing widget to smallest size possible.

  21. #20
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Add QScrollArea to QDialog

    Quote Originally Posted by Rachol View Post
    Can you try your code with QPushButton instead of MixerStrip and then see if the widgets are being laid out normally?
    If I use QPushButtons they're laid out correctly.

    Here's the code for the MixerStrip:

    Qt Code:
    1. #include "mixerstrip.h"
    2.  
    3. MixerStrip::MixerStrip(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. QSlider *volSlider = new QSlider(Qt::Vertical);
    7. volSlider->setMinimum(0);
    8. volSlider->setMaximum(100);
    9. QPalette pal = volSlider->palette();
    10. pal.setColor(volSlider->backgroundRole(), QColor(128, 128, 128));
    11. volSlider->setPalette(pal);
    12. volSlider->setAutoFillBackground(TRUE);
    13. volSlider->setGeometry(0,0,20,220);
    14. volSlider->setParent(this);
    15.  
    16. Meter *meter = new Meter;
    17. meter->setGeometry(20,0,50,220);
    18. meter->setParent(this);
    19.  
    20. QPushButton *muteButton = new QPushButton("M");
    21. QPushButton *soloButton = new QPushButton("S");
    22. muteButton->setCheckable(true);
    23. soloButton->setCheckable(true);
    24.  
    25. pal = muteButton->palette();
    26. pal.setColor(muteButton->backgroundRole(), QColor(128, 128, 128));
    27. muteButton->setPalette(pal);
    28. muteButton->setAutoFillBackground(TRUE);
    29. pal = soloButton->palette();
    30. pal.setColor(soloButton->backgroundRole(), QColor(128, 128, 128));
    31. soloButton->setPalette(pal);
    32. soloButton->setAutoFillBackground(TRUE);
    33.  
    34. muteButton->setGeometry(0,220,70,20);
    35. soloButton->setGeometry(0,240,70,20);
    36. muteButton->setParent(this);
    37. soloButton->setParent(this);
    38.  
    39. QSlider *panSlider = new QSlider(Qt::Horizontal);
    40. panSlider->setMinimum(-100);
    41. panSlider->setMaximum(100);
    42. panSlider->setValue(0);
    43. pal = panSlider->palette();
    44. pal.setColor(panSlider->backgroundRole(), QColor(128, 128, 128));
    45. panSlider->setPalette(pal);
    46. panSlider->setAutoFillBackground(TRUE);
    47. panSlider->setGeometry(0,260,70,20);
    48. panSlider->setParent(this);
    49.  
    50. QLineEdit *trackName = new QLineEdit();
    51. trackName->setAlignment(Qt::AlignCenter);
    52. trackName->setText("Track");
    53. trackName->setGeometry(0,280,70,20);
    54. trackName->setParent(this);
    55.  
    56. QObject::connect(volSlider, SIGNAL(valueChanged(int)), meter, SLOT(valueChanged(int)));
    57. volSlider->setValue(0);
    58.  
    59. setFixedSize(size());
    60. //setSizeConstraint ( QLayout::SetFixedSize );
    61. }
    62.  
    63. QSize MixerStrip::sizeHint() const
    64. {
    65. QSize size(70,300);
    66.  
    67. return size;
    68. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Santosh Reddy View Post
    You can use
    Qt Code:
    1. hlayout.setSizeConstraint(QLayout::SetMinimumSize);
    To copy to clipboard, switch view to plain text mode 
    This will set the layout and the containing widget to smallest size possible.
    I tried what you suggested but it still doesn't fix the problem.

Similar Threads

  1. Replies: 9
    Last Post: 25th March 2011, 21:22
  2. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  3. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02
  4. How to "lock" a new qdialog in another qdialog
    By donglebob in forum Qt Programming
    Replies: 7
    Last Post: 4th February 2009, 08:37
  5. Replies: 2
    Last Post: 10th March 2008, 20:16

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.