Results 1 to 13 of 13

Thread: QScrollArea margins

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QScrollArea margins

    Hi,

    I have a scroll area in which i need to palce a widget with some margin so that it looks as if it is placed at the center of the area.

    Doing this was very simple using Qt3 but I am not able to find a way to do this using Qt4.

    Can someone please help?

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    did you try QScrollArea::ensureVisible ( int x, int y, int xmargin = 50, int ymargin = 50 )?

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    Quote Originally Posted by high_flyer
    did you try QScrollArea::ensureVisible ( int x, int y, int xmargin = 50, int ymargin = 50 )?
    I thought that was to bring the point (x,y) int the scroll area appear in the viewport with the given margins.

    Am I wrong?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    you are right - but isn't it exactly what you want?

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    No, I dont want that.

    In Qt3, if I use :

    Qt Code:
    1. scrollview->addChild(widget,10,10);
    To copy to clipboard, switch view to plain text mode 

    The widget will appear from the point (10,10), which means I will have some top and left margin.

    Using resizeContents() I can get the right and the bottom margins also.

    This is not so simple in Qt4.

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

    Default Re: QScrollArea margins

    You can add a layout to the area and add the widget to the layout. You can then set margins used by that layout and it should do what you want.

  7. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    From the Book: Practical Qt
    try adding the line:
    Qt Code:
    1. scrollView->setResizePolicy( QScrollView::AutoOneFit );
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  8. #8
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    I tried

    Qt Code:
    1. QVBoxLayout *mainLayout = new QVBoxLayout(scrollarea);
    2. mainLayout->addWidget(widget);
    3. scrollarea->setLayout(layout)
    4. scrollarea->setWidget(widget)
    To copy to clipboard, switch view to plain text mode 

    but is not working. I am sure this is not what you meant. Can you please explain ?

    Thanks.

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

    Default Re: QScrollArea margins

    It should probably be
    Qt Code:
    1. QVBoxLayout *mainLayout = new QVBoxLayout(scrollarea->viewport());
    2. mainLayout->addWidget(widget);
    3. scrollarea->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 
    If this doesn't help try:
    Qt Code:
    1. QWidget *wgt = new QWidget();
    2. QVBoxLayout *mainLayout = new QVBoxLayout();
    3. mainLayout->addWidget(widget);
    4. wgt->setLayout(mainLayout);
    5. scrollarea->setWidget(wgt);
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th September 2006 at 17:48. Reason: Corrected a variable name in code

  10. #10
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    No. Its not working.

    I think I have to use some round about way to do this.

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

    Default Re: QScrollArea margins

    But what do you mean "it is not working"? Could you elaborate? Did you set the margin for that layout?

  12. #12
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea margins

    Here is my code

    Qt Code:
    1. previewArea = new PreviewArea(this);
    2. previewArea->setWidgetResizable(true);
    3.  
    4. previewWidget = new PreviewWidget(previewArea);
    5.  
    6. QVBoxLayout *pLay = new QVBoxLayout(previewArea->viewport());
    7. pLay->setMargin(10);
    8. pLay->addWidget(previewWidget);
    9.  
    10. previewArea->setLayout(pLay);
    11. previewArea->setWidget(previewWidget);
    To copy to clipboard, switch view to plain text mode 

    With the above code, when I close the window that contains the area, my application crashes. I think the crash has some thing to do with QVBoxLayout *pLay = new QVBoxLayout(previewArea->viewport());

    The next option

    Qt Code:
    1. previewArea = new PreviewArea(this);
    2. previewArea->setWidgetResizable(true);
    3. previewWidget = new PreviewWidget(onlyCurr,cd,previewArea);
    4.  
    5. QVBoxLayout *pLay = new QVBoxLayout();
    6. pLay->setMargin(10);
    7. pLay->addWidget(previewWidget);
    8. previewArea->setLayout(pLay);
    9. previewArea->setWidget(previewWidget);
    To copy to clipboard, switch view to plain text mode 

    The above code is also not working. But this is not crashing the application.

    Thanks for your help.

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

    Default Re: QScrollArea margins

    Here is my code and it works:

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QScrollArea>
    4. #include <QVBoxLayout>
    5.  
    6.  
    7. int main(int argc, char **argv){
    8. QApplication app(argc, argv);
    9. QWidget *widget = new QWidget;
    10. QLabel *label = new QLabel("Some text");
    11. label->setFrameShape(QFrame::Box);
    12. lay->addWidget(label);
    13. lay->setMargin(20);
    14. widget->setLayout(lay);
    15. sa.setWidget(widget);
    16. sa.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. is there any signal for QScrollArea?
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 07:12
  2. Replies: 2
    Last Post: 17th July 2006, 20:09
  3. Using QScrollArea
    By Jimmy2775 in forum Qt Programming
    Replies: 4
    Last Post: 2nd March 2006, 21:26
  4. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59
  5. 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

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.