Results 1 to 20 of 24

Thread: Add QScrollArea to QDialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    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

  3. #3
    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.

  4. #4
    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 ); ?

  5. #5
    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.

  6. #6
    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?

  7. #7
    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)

  8. #8
    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!

  9. #9
    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?

  10. #10
    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.

  11. #11
    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.

  12. #12
    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

    Ok, now it is easy

    There are couple of ways to solve your problem.

    1. The easiest way would be to place a layout inside your MixerStrip class and then place all the widgets inside the layout instead of hardcoding all coordinates.
    2. Re-implement minimumSizeHint and maximumSizeHint. You could return the same value that you return in your sizeHint implementation.

  13. The following user says thank you to Rachol for this useful post:

    cpsmusic (26th June 2011)

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

    Default Re: Add QScrollArea to QDialog

    Quote Originally Posted by Rachol View Post
    Ok, now it is easy

    There are couple of ways to solve your problem.

    1. The easiest way would be to place a layout inside your MixerStrip class and then place all the widgets inside the layout instead of hardcoding all coordinates.
    2. Re-implement minimumSizeHint and maximumSizeHint. You could return the same value that you return in your sizeHint implementation.
    I tried the second suggestion (see below) however the widgets are still spread out.

    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. }
    69.  
    70. QSize MixerStrip::minimumSizeHint() const
    71. {
    72.  
    73. QSize size(70,300);
    74.  
    75. return size;
    76. }
    77.  
    78. QSize MixerStrip::maximumSize()
    79. {
    80. QSize size(70,300);
    81.  
    82. return size;
    83. }
    To copy to clipboard, switch view to plain text mode 

  15. #14
    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, I hope this should fix your problem for sure.

    When you implement size hints on your MixerStrip, then you don't need to set it to fixed size explicitly. Just remove the following statement at the end of MixerStrip Ctor, you should be done, worrying.
    Qt Code:
    1. //setFixedSize(size());
    To copy to clipboard, switch view to plain text mode 

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

    cpsmusic (26th June 2011)

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

    Default Re: Add QScrollArea to QDialog

    Thanks, that last change solved the problem! Thanks very much, the people on this forum are very helpful.

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
  •  
Qt is a trademark of The Qt Company.