Results 1 to 14 of 14

Thread: Adding a scrollbar to a QWidget

  1. #1
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Adding a scrollbar to a QWidget

    I am using Qt4.4.3. Following is my issue;

    1. I have a QWidget which I use to create a tag cloud view (a weighted list placed in a rectangle area where the words (text) are of varying size). See attachment.
    2. The QWidget which has the tag cloud is created at runtime
    3. The problem is when the amount of text is large, and its area exceeds that of the container QWidget, a ScrollBar DOESN't appear. You can see this in the attachment

    4. I tried enclosing the the QWidget into a QScrollArea as follows, but it doesn't work, the scrollbar doesn't appear.
    Qt Code:
    1. QWidget* tagCloudDisplay; // This is the widget which holds the tag cloud
    2.  
    3. QScrollArea *tagCloudScrollArea = new QScrollArea;
    4. tagCloudScrollArea->setGeometry(tagCloudDisplay->geometry());
    5. tagCloudScrollArea->setWidget(mainWidget->tagCloudDisplay); // MainWidget is the container widget i.e. the window itself
    6. tagCloudScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    7. tagCloudScrollArea->setWidgetResizable(true);
    To copy to clipboard, switch view to plain text mode 
    5. Is it possible to havea ScrollArea for a QWidget, or should I be using a different type of widget instead of QWidget Eg a QTextEdit to create the tag cloud?

    Any advise or suggestions will really be appreciated.
    Thanks.
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding a scrollbar to a QWidget

    You have seen the little example in the docs http://qt.nokia.com/doc/4.5/qscrollarea.html#details?
    Doesn't it work that way?

  3. #3
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding a scrollbar to a QWidget

    You are resizing the scroll area to the size of the widget. That makes the scrollarea bigger so that it doesn't need scrollbars. You can resize the widget itself if you want, but not the scrollarea.

    You have also set widgetResizable to true, which makes the scroll area resize the widget. From the Qt docs:

    If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.
    Qt Code:
    1. QWidget* tagCloudDisplay; // This is the widget which holds the tag cloud
    2. tagCloudDisplay->resize(QSize(1000, 1000)); // optional: make it big enough
    3.  
    4. QScrollArea *tagCloudScrollArea = new QScrollArea;
    5. tagCloudScrollArea->setWidget(tagCloudDisplay);
    To copy to clipboard, switch view to plain text mode 
    Last edited by victor.fernandez; 1st September 2009 at 15:55.

  4. #4
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding a scrollbar to a QWidget

    Boron: Yes, the first thing I tried was that example.

    Victor: Thanks for the details.

    The most puzzling part is when I add the ScrollArea, the window itself doesn't display

    Example I tried what victor suggested:
    Qt Code:
    1. QWidget* tagCloudDisplay; // This is the widget which holds the tag cloud
    2. tagCloudDisplay->resize(QSize(1000, 1000)); // optional: make it big enough
    3.  
    4. QScrollArea *tagCloudScrollArea = new QScrollArea;
    5. tagCloudScrollArea->setWidget(tagCloudDisplay);
    To copy to clipboard, switch view to plain text mode 

    But then there is no window displayed at all.
    Note: This tagCloudDisplay in turn contains a QTextEdit in which a QStringList populates the textEdit with the words.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding a scrollbar to a QWidget

    Did you implement sizeHint() for your widget? Did you apply a layout to your widget?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding a scrollbar to a QWidget

    Did you call mainWidget->show() somewhere?

  7. #7
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding a scrollbar to a QWidget

    Quote Originally Posted by wysota View Post
    Did you implement sizeHint() for your widget? Did you apply a layout to your widget?
    Wysota, I tried a couple of approaches:

    Approach 1
    -------------
    Result: No Window displayed
    Qt Code:
    1. QWidget* tagCloudDisplay; // This is the widget which holds the tag cloud
    2. QVBoxLayout *vbLayout = new QVBoxLayout;
    3. tagCloudDisplay->setLayout(vbLayout);
    4. tagCloudDisplay->setMaximumHeight(100);
    5. tagCloudDisplay->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    6.  
    7. QScrollArea *scroll = new QScrollArea;
    8. scroll->setWidget(tagCloudDisplay);
    To copy to clipboard, switch view to plain text mode 

    Approach 2
    -------------
    Result:The program compiles but crashes at runtime.

    Qt Code:
    1. // Function 1
    2. void update()
    3. {
    4. :
    5. // Layout
    6. QVBoxLayout *cloudVerticalBoxLayout = new QVBoxLayout;
    7. cloudVerticalBoxLayout->addWidget(MainWidget->tagCloudDisplay);
    8. mainWidgetr->setLayout(cloudVerticalBoxLayout);
    9.  
    10. // Setting the scrollArea
    11. QScrollArea *tagCloudScrollArea = new QScrollArea;
    12. tagCloudScrollArea->setWidget(tagCloudDisplay );
    13.  
    14.  
    15. mainWidget->updateTagCloud(); // Calls function below which adds the tag cloud words to the widget
    16. }
    17.  
    18. // Function 2
    19. void updateTagCloud()
    20. {
    21. QWidget* tagCloudDisplay; // This is the widget which holds the tag cloud
    22. :
    23. :
    24. QTextEdit *textAreaForTags = new QTextEdit;
    25. textAreaForTags->setWindowFlags(Qt::ToolTip);
    26. :
    27. // Code here to retreive required words
    28. :
    29.  
    30. // Start of loop
    31. tagCloudArray[i].setParent(tagCloudDisplay);
    32. :
    33. // End of loop
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 


    Questions:
    1. Do I need a layout in this case? As there is only one widget which contains the QTextEdit?
    2. What is the order in which the components are build?

    Any suggestions will be appreciated.
    Thanks once again for your time.
    Last edited by qtUser500; 1st September 2009 at 20:09.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding a scrollbar to a QWidget

    Hold on... the code above suggests you don't know how to use layouts. Is that true? Can you show us the definition of your tag cloud widget?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding a scrollbar to a QWidget

    Quote Originally Posted by wysota View Post
    Hold on... the code above suggests you don't know how to use layouts. Is that true?
    Well, yes I am learning, and have been looking at different forum threads and QT documentation to understand why it is not working in my application, and if I am getting the order wrong or missing out on calling cetain settings.
    So, yes, I am not 100% comfortable in the way the layouts work, but am eager to learn the right approach.

    Quote Originally Posted by wysota View Post
    Can you show us the definition of your tag cloud widget?
    I have put snippets of what my code looks like hence it might not be complete.
    The mainWidget inherits from another user-defined class (which has some customised functions ) and this in turn inherits from QWidget

    ie. mainWidget--> myCustomisedClass->QWidget

    Thank you once again for your patience.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding a scrollbar to a QWidget

    I don't care what it is derived from. I only care if it has a proper layout or not. Please provide the full unmodified contents of the constructor.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding a scrollbar to a QWidget

    There is no layout specified in the constructor.

    I tried implementing layouts in the ui, and have enclosed them (both ui's: before and after my changes)

    - To give you additional information of this Window. It has an option of either viewing words as a list (QListWidget) or as a tag cloud (QWidget)
    Before
    - In the UI file, there is only the QlistWidget added , there is no layout present. (attachment: tagCloud-before.ui)
    - But when the application runs for the listWidget the scrollbar appears when needed.
    - the tag cloud widget is added at runtime.

    After
    - I made modifications to the ui file (tagCloud-after.ui), by enclosing the QListWidget in a ScrollArea.
    - I put them in a grid-layout, as when I tried other layouts the QListWidget was not expanding as the window is resized.
    Question: Is this the correct layout to use? If not, if you have just one widget in a scrollArea, which is the preferred layout?
    - at runtime I tried to hookup the tagcloud to the scrollArea

    Qt Code:
    1. Ui::TagCloud ui_tagCloud;
    2. ui_tagCloud.scrollAreaWords->setWidget(tagCloudDisplay);
    To copy to clipboard, switch view to plain text mode 
    The application starts to run but hangs after awhile.

    Is this a good approach?
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding a scrollbar to a QWidget

    The one "after" is better. But still, I was asking about your tag cloud (tagCloudDisplay) and not the scroll area window.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding a scrollbar to a QWidget

    Thanks Wyosta.

    In reply to your questions:
    - tagCloudDisplay is a member variable.
    - Details are as follows

    Class 1 : MyApplication
    Qt Code:
    1. void MyApplication::createTagWindow()
    2. {
    3. TagWindowWidget* tagWindow = new TagWindowWidget; // This class inherits from a customised class which in turn inherits from QObject
    4. Ui::TagCloud ui_TagCloud; // This is the ui attached
    5. ui_tagCloud.setupUi(tagWindow );
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Class 2: WordsManager (inherits directly from QObject)
    Qt Code:
    1. void WordsManager::loadWordWindow( QWidget *tagCloud, QListWidget *wordList)
    2. {
    3.  
    4. WordsDisplay *wordsDisplayWindow = new WordsDisplay(tagCloud, wordList)
    5. }
    To copy to clipboard, switch view to plain text mode 

    The WordsDisplay class structure is as follows:

    Qt Code:
    1. class WordsDisplay : public QObject
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. protected:
    7. QWidget* tagCloudDisplay;
    8. QListWidget* wordListDisplay;
    9. :
    10. :
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    - I am trying to associate the 'scrollArea' in the ui to the tagCloudDisplay. Would this be the solution?
    - How can I do this with the structure I have? As I am not able to retrieve the ui.scrollArea after the tagCloudDisplay is instantiated

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding a scrollbar to a QWidget

    We're not getting anywhere...

    I'm asking you to present your code because I want to have a look at it, not because I want to steal something from you. If you want me to help you, you have to provide the code I ask you for, not the one you feel like sharing at the moment.

    I need to know how the tag cloud is composed. It probably doesn't have a layout applied and I'm pretty certain you didn't reimplement sizeHint() for your widget. Therefore the widget has no real size and will not make the scroll area have scrollbars ever.

    Furthermore please read this: Using a Designer .ui File in Your Application - please follow one of the recommended approaches.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QGraphicsView and adding QWidget with transparent areas
    By sting73 in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2009, 11:19
  2. Replies: 11
    Last Post: 25th February 2009, 17:35
  3. Replies: 0
    Last Post: 11th November 2008, 15:36
  4. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  5. GraphicsView/GraphicsScene: scrollbar policy Qt::ScrollBarAsNeeded
    By Pieter from Belgium in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2007, 13:15

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.