Results 1 to 4 of 4

Thread: QFrame within QScrollArea

  1. #1
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFrame within QScrollArea

    I can't find a way to get the scroll bar to work with QFrame. My class is derived from QFrame and I display a couple of images within it. I want to add a scrollbar as soon as the images gets to many. I tried a lot of examples without success for my particular design

    Qt Code:
    1. Monitors::Monitors(QWidget *parent)
    2. : QFrame(parent)
    3. {
    4. for (int i = 0; i < 4; i++)
    5. {
    6. QLabel *MonitorIcon = new QLabel(this);
    7. QPixmap pic = QPixmap("C:/DevelopmentWork/TestFrame/test.png");
    8.  
    9. QPainter paint;
    10. paint.begin(&pic);
    11. paint.drawPixmap(225, 162, pic);
    12. paint.end();
    13.  
    14. MonitorIcon->setPixmap(pic);
    15.  
    16. if (i < 2)
    17. MonitorIcon->move(20 + (i * 100), 20);
    18. else
    19. MonitorIcon->move(20 + ((i -2) * 100), 20 + 100);
    20.  
    21. MonitorIcon->show();
    22. MonitorIcon->setAttribute(Qt::WA_DeleteOnClose);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    The following example doesn't work on my class either http://discussion.forum.nokia.com/fo...-Bar-in-QFrame
    Last edited by wysota; 9th May 2011 at 00:05. Reason: missing [code] tags

  2. #2
    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: QFrame within QScrollArea

    You forgot to use layouts.
    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.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QFrame within QScrollArea

    The example you link to works as advertised. A scroll bar appears when the content of the QScrollArea is larger than the space available to display that content.

    Tell us what you have done to try and get a scroll bar? Nothing in this code suggests you have tried anything. (It also suggests that you have not grasped the notion of layouts: if you want a 2x2 grid of labels then you should consider QGridLayout.)

  4. #4
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFrame within QScrollArea

    Thanks Chris, you're right.

    I got it to work by using the following code, similar to the example.
    However I'd to calculate the size of my widget depending on the total images I added.
    By doing a resize within my widget the scrollbars works fine.

    Qt Code:
    1. QHBoxLayout* MYCLASS::CreateMonLayout()
    2. {
    3. QHBoxLayout *sLayout = new QHBoxLayout();
    4.  
    5. MonWidget *screenW = new MonWidget();
    6. screenW->setParent(NULL);
    7.  
    8. // Assign Widget to scroll area to allow scrolling
    9. QScrollArea *scroll = new QScrollArea;
    10. scroll->setWidget(screenW);
    11. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    12. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    13. sLayout->addWidget(scroll);
    14.  
    15. return sLayout;
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'd tried 3 days to get it work automatically with layouts, as suggested by wysota but it ends up the layout will control the size of my widget and result in the images just being cut. It would be great to know if there's a better way though but I'd to move one. I wasted to much time on this one

    Thanks for your help.

Similar Threads

  1. QFrame MousePressedEvent
    By jwflammer in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2011, 13:05
  2. QFrame V and H Line
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 5th September 2008, 20:30
  3. Using css for all QFrame's except one.
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 10th July 2008, 09:05
  4. Replies: 2
    Last Post: 10th March 2008, 21:16
  5. Reg - QFrame
    By suresh in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2006, 05:34

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.