Results 1 to 1 of 1

Thread: Scrollbars for QStackedLayout

  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Scrollbars for QStackedLayout

    Hello!

    I have worked out a solution that works for me, but I would like to know, if I missed a more elegant solution.

    I needed a widget, that shows a list of several possible user interfaces, that fit into a defined space. Therefore I put a StackedLayout into a Scrollarea.

    First: A StackedLayout that has its minimumSize set depending only on the currentwidget and not all its child-widgets:

    Qt Code:
    1. class CStackedLayout : public QStackedLayout
    2. { Q_OBJECT
    3. public:
    4. explicit CStackedLayout(QWidget *parent = 0) : QStackedLayout(parent) {}
    5. QSize sizeHint() const
    6. {
    7. QWidget* cw = currentWidget();
    8. if (cw) return cw->sizeHint(); else return QSize(0,0);
    9. }
    10. QSize minimumSize() const
    11. {
    12. QWidget* cw = currentWidget();
    13. if (cw) return cw->minimumSize(); else return QSize(0,0);
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    Based on that StackLayout I build a StackedWidget, that updates its minimumSize, if appropriate:

    Qt Code:
    1. class CStackedWidget : public QFrame
    2. { Q_OBJECT
    3. Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentChanged)
    4. Q_PROPERTY(int count READ count)
    5. public:
    6. explicit CStackedWidget(QWidget *parent=0) : QFrame(parent)
    7. {
    8. layout = new CStackedLayout(this);
    9. connect(layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int)));
    10. connect(layout, SIGNAL(currentChanged(int)), this, SLOT(updateConstraints()));
    11. connect(layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
    12. }
    13.  
    14. int addWidget(QWidget *w) {return layout->addWidget(w);}
    15. int insertWidget(int index, QWidget *w) {return layout->insertWidget(index, w);}
    16. void removeWidget(QWidget *w) {layout->removeWidget(w);}
    17.  
    18. QWidget *currentWidget() const {return layout->currentWidget();}
    19. int currentIndex() const {return layout->currentIndex();}
    20.  
    21. int indexOf(QWidget *w) const {return layout->indexOf(w);}
    22. QWidget *widget(int index) const {return layout->widget(index);}
    23. int count() const {return layout->count();}
    24.  
    25. public slots:
    26. void setCurrentIndex(int index) {layout->setCurrentIndex(index);}
    27. void setCurrentWidget(QWidget *w) {layout->setCurrentWidget(w);}
    28.  
    29. void updateConstraints() {setMinimumSize(layout->minimumSize().width(),layout->minimumSize().height());resize(size());}
    30. signals:
    31. void currentChanged(int);
    32. void widgetRemoved(int index);
    33.  
    34. private:
    35. CStackedLayout* layout;
    36. };
    To copy to clipboard, switch view to plain text mode 
    And a multiwidget selector class, which shows a ListWidget from which you can choose the current widget of the stack.

    Qt Code:
    1. class CMultiWidget : public QWidget
    2. { Q_OBJECT
    3. public:
    4. CMultiWidget()
    5. {
    6. list = new QListWidget();
    7. list->setMinimumWidth(30);
    8. list->setMaximumWidth(100);
    9.  
    10. wdgstack = new CStackedWidget();
    11. wdgstack->setContentsMargins(0,0,0,0);
    12.  
    13. sa = new QScrollArea();
    14. sa->setWidgetResizable(true);
    15. sa->setWidget(wdgstack);
    16. sa->setContentsMargins(0,0,0,0);
    17. sa->setFrameStyle(QFrame::NoFrame);
    18.  
    19. QHBoxLayout *layout = new QHBoxLayout();
    20.  
    21. layout->addWidget(list,0);
    22. layout->addWidget(sa,1);
    23.  
    24. setLayout(layout);
    25.  
    26. connect(list,SIGNAL(currentRowChanged(int)),wdgstack,SLOT(setCurrentIndex(int)));
    27. }
    28. public slots:
    29. void AddWidget(QString caption,QWidget* wdg)
    30. {
    31. wdgstack->addWidget(wdg);
    32. list->addItem(caption);
    33. }
    34. private:
    35. QListWidget* list;
    36. CStackedWidget* wdgstack;
    37. };
    To copy to clipboard, switch view to plain text mode 
    A small example:

    Qt Code:
    1. class CContentWidget : public QWidget
    2. { Q_OBJECT
    3. public:
    4. CContentWidget(int listcount,int w,int h)
    5. {
    6. setMinimumSize(w,h);
    7. QHBoxLayout* hl = new QHBoxLayout();
    8. for (int i = 0;i < listcount;++i)
    9. {
    10. hl->addWidget(new QListWidget());
    11. }
    12. setLayout(hl);
    13. }
    14. private:
    15. };
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication a(argc, argv);
    20. CMultiWidget w;
    21. w.resize(600,300);
    22. w.AddWidget("A",new CContentWidget(2,250,150));
    23. w.AddWidget("B",new CContentWidget(3,250,500));
    24. w.show();
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    Thx in advance for any recommendations!

    Johannes
    Last edited by JohannesMunk; 6th December 2009 at 21:06. Reason: QScrollArea.setWidgetResizable

Similar Threads

  1. scrollbars and qgraphicsview
    By captiva in forum Qt Programming
    Replies: 13
    Last Post: 20th February 2009, 13:00
  2. scrollbars in MaximumSize QGraphicsView
    By captiva in forum Newbie
    Replies: 0
    Last Post: 17th February 2009, 10:24
  3. Problem with Scrollbars & QWebView
    By lashus in forum Newbie
    Replies: 0
    Last Post: 26th October 2008, 19:30
  4. QItemDelegate, QTreeView and scrollbars
    By SiLiZiUMM in forum Qt Programming
    Replies: 11
    Last Post: 6th May 2008, 18:23
  5. QEvent for QGraphicsView scrollbars?
    By forrestfsu in forum Qt Programming
    Replies: 9
    Last Post: 2nd December 2006, 08:42

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.