Results 1 to 3 of 3

Thread: Custom Scroll Widget Sizing Problem

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom Scroll Widget Sizing Problem

    the basic idea of following code is to create a collection of widgets and add the collection to a scroll area,each collection has at least 2 labels describing it's contents
    Qt Code:
    1. #ifndef collection_class
    2. #define collection_class
    3. #include <QWidget>
    4. #include <QLabel>
    5. #include <QVBoxLayout>
    6. #include <QGridLayout>
    7. class collection:public QWidget
    8. {
    9. private:
    10.  
    11.  
    12. static QVBoxLayout* mainLayout;
    13. QGridLayout innerLayout
    14. QLabel caption;
    15.  
    16. public:
    17. QSize sizeHint();
    18. collection(QWidget *parent,QString collectionName,QString collectionCaption);
    19. static QVBoxLayout* returnMainLayout();
    20. QLabel triClick;
    21.  
    22. };
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "collection.h"
    2.  
    3. collection::collection(QWidget *parent, QString collectionName, QString collectionCaption)
    4. :QWidget(parent)
    5. {
    6.  
    7. innerLayout.setProperty("name",collectionName);
    8. triClick.setText(collectionName);
    9. caption.setText(collectionCaption);
    10. innerLayout.addWidget(&triClick,0,0);
    11. innerLayout.addWidget(&caption,0,1);
    12. mainLayout->addLayout(&innerLayout);
    13.  
    14. }
    15. static QVBoxLayout* collection::returnMainLayout()
    16. {
    17. return mainLayout;
    18. }
    19. QSize collection::sizeHint()
    20. {
    21. return QSize(100,200);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef scroll_class
    2. #define scroll_class
    3. #include "collection.h"
    4. #include <QScrollArea>
    5. class scroll:public QScrollArea
    6. {
    7. public:
    8. scroll(QWidget *parent=0);
    9. collection* addCollection(QString collectionName, QString collectionLabel);
    10. bool addWidget(QString collectionName, QWidget* object2Add,QString objectIdentifier);
    11. QSize sizeHint();
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "scroll.h"
    2. #include <QDebug>
    3. scroll::scroll(QWidget *parent)
    4. :QScrollArea(parent)
    5. {
    6.  
    7. setMaximumSize(200,80);
    8. // **** point of interest *****
    9. collection* collectionTmpPtr=new collection(this, "", "");
    10. collectionTmpPtr=new collection(this, "shoot", "wow");
    11. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    12. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    13. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    14. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    15. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    16. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    17. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    18. collectionTmpPtr=new collection(this, "shoot3S", "wow");
    19.  
    20. QWidget * widget=new QWidget(parent);
    21. VBoxLayout *mainLayout = collection::returnMainLayout() ;
    22. widget->setLayout(mainLayout);
    23. setWidget(widget);
    24.  
    25. }
    26. collection* scroll::addCollection(QString collectionName,QString collectionLabel)
    27. {
    28. collection* collectionTmpPtr=new collection(this, collectionName, collectionLabel);
    29. return collectionTmpPtr;
    30. }
    31. bool scroll::addWidget(QString collectionName, QWidget* object2Add, QString objectIdentifier)
    32. {
    33.  
    34. QVBoxLayout *mainLayout = collection::returnMainLayout();
    35.  
    36. for (int i = 0; i < mainLayout->count(); ++i)
    37. {
    38.  
    39. QLayout* item=mainLayout->itemAt(i)->layout();
    40. if (item !=0) //if item is a layout
    41. if (item->property("name")==collectionName) //if that's the collection user is looking for to add items to
    42. {
    43. object2Add->setProperty ("name",objectIdentifier);
    44. QGridLayout* newItem=static_cast<QGridLayout*>(item);
    45. newItem->addWidget(object2Add,newItem->count()-1,0);
    46. return true;
    47. }
    48.  
    49. }
    50. return false;
    51.  
    52. }
    53. QSize scroll::sizeHint()
    54. {
    55. return QSize(100,80);
    56. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include "scroll.h"
    4. QVBoxLayout* collection::mainLayout=new QVBoxLayout;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8.  
    9. QApplication a(argc, argv);
    10. QWidget windows;
    11. scroll test(&windows);
    12. QVBoxLayout layout;
    13. test.addCollection("salam2","2");
    14. test.addCollection("salam3","2");
    15. test.addCollection("salam2","1");
    16. test.addCollection("salam4","3");
    17. test.addCollection("salam5","5");
    18. test.addCollection("salam2","2");
    19. test.addCollection("salam3","2");
    20. test.addCollection("salam2","1");
    21. test.addCollection("salam4","3");
    22. test.addCollection("salam5","5");
    23. test.addCollection("salam2","1");
    24. test.addCollection("salam4","3");
    25. test.addCollection("salam5","5");
    26.  
    27.  
    28. windows.setLayout(&layout);
    29. windows.show();
    30.  
    31. return a.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 
    now the problem is that when i add collections in scroll constructor everything is fine like
    following which i have used my custom scroll widget alone with 2 other widgets:

    but when i add collections using add collection method which performs the same job,i have sizing issues like following:

    the picture above shows my widget when some of the collections are added in scroll widget constructor and some of them are added in main.cpp using addCollection method,
    if I add all collections in main then i would have NO collections being seen at all(i think they get zero size so they are not shown) i have played with sizepolicies and minimumSize methods but no luck,I'm Stuck,please help

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom Scroll Widget Sizing Problem

    i) not related to your problem, but: a global returnMainLayout() is a bad idea: what if you want two instances of that widget?

    ii) you should not set the maximumSize of your widget.
    now, the available space is quite limited and the layout tries to fit all the widgets into that (too small) space.

  3. #3
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Scroll Widget Sizing Problem

    Quote Originally Posted by caduel View Post
    i) not related to your problem, but: a global returnMainLayout() is a bad idea: what if you want two instances of that widget?

    ii) you should not set the maximumSize of your widget.
    now, the available space is quite limited and the layout tries to fit all the widgets into that (too small) space.
    thanks for reply,since other objects like buttons or textbox and .. can be added to a collection, I want to have a scrolling widget so it won't take alot of space

    any ideas how to approach that?

Similar Threads

  1. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55
  2. Replies: 16
    Last Post: 7th March 2006, 15:57
  3. Replies: 4
    Last Post: 1st March 2006, 23:11
  4. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 22:35
  5. QRubberBand painting in the scroll area widget
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 17th January 2006, 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.