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

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.