Results 1 to 20 of 31

Thread: QScrollArea With Custom Widgets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QScrollArea With Custom Widgets

    Ok let's say you have a program like structured like this:

    CObject Objects in the table, like 25
    CTable Table for QScrollArea
    main where you display QScrollArea

    In CTable you do this (assume all objects declared):

    Qt Code:
    1. CTable::CTable(QWidget *parent) : QWidget(parent) {
    2. iObjects = 25;
    3. setLayout(lay);
    4. for(int i = 0; i < iObjects; i++){
    5. buttons << new QPushButtons(QString("%1").arg(i));
    6. }
    7. }
    8. void CTable::resizeEvent(QResizeEvent *evt){
    9. // ASSUME XPOS AND YPOS ARE CORRECTLY DECLARED
    10. for(int i = 0; i < iObjects; i++){
    11. buttons[i]->move(xpos*150+10, ypos*101+10);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    And in main
    Qt Code:
    1. qs->setWidget(ctable);
    To copy to clipboard, switch view to plain text mode 


    Code has been shortened for clarity!!!

    The idea here is, I am building 25 custom widgets (I used buttons here for simplicity, with a QList), and they have to automatically resize and list themselves (the dimensions are 150 by 100), like as if you would see a test with problems in them. It will go like this:

    1 2 3 4 5
    6 7 8 9 10
    11 12.....

    Problem is when I compile this code, it will not show the objects, it will show them all in one spot (so all 25 are in same spot so you only see ONE).

    Need help please.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    Where are u adding the buttons to the grid layout ??

  3. #3
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea With Custom Widgets

    I'm not... but I am declaring the pushbuttons as parent "this".
    but I have tried addWidget on each, but didn't get me very far.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea With Custom Widgets

    I'd suggest reading about Layout Classes.
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea With Custom Widgets

    Are you creating the buttons directly in the scroll area?
    If so, it's not OK. You must create another widget that you will set as the scroll area contents with QScrollArea::setWidget and then create your buttons with this widget as parent. Also, the grid layout must be set for this widget.

    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea With Custom Widgets

    Quote Originally Posted by marcel View Post
    You must create another widget that you will set as the scroll area contents with QScrollArea::setWidget and then create your buttons with this widget as parent.
    He is doing so:
    Qt Code:
    1. qs->setWidget(ctable);
    To copy to clipboard, switch view to plain text mode 

    Also, the grid layout must be set for this widget.
    He is doing that too:
    Qt Code:
    1. setLayout(lay);
    To copy to clipboard, switch view to plain text mode 

    It's just the actual usage of the layout that's missing.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea With Custom Widgets

    I would suggest... you're right.

    But see, when I do this:
    lay->addWidget(cobject[i]);

    and then compile...
    All I see is one widget (I believe the others are underneath it).

    move I guess doesn't work with QGridLayout, but I tried removing QGridLayout, and it seems thats not the problem. So I'm wondering... is move a useless function that only works when it feels like it?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea With Custom Widgets

    Use QGridLayout::addWidget() which takes a row and column number. Oh, and you can forget about resizing and moving by hand when a layout is in use.
    J-P Nurmi

  9. #9
    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: QScrollArea With Custom Widgets

    Shouldn't the layout be applied on the viewport and not directly on the scroll area?

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea With Custom Widgets

    Quote Originally Posted by wysota View Post
    Shouldn't the layout be applied on the viewport and not directly on the scroll area?
    But then it wouldn't make any sense to use a scroll area? His layout is applied on the widget inside the scroll area as I see is correct.
    J-P Nurmi

  11. #11
    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: QScrollArea With Custom Widgets

    Oh, right. I didn't notice that.

Similar Threads

  1. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38
  2. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 08:24
  3. Global includes with designer custom widgets
    By mab in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 22:06
  4. size issues for custom QWidget in QScrollArea
    By anotheruser in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2006, 14:52
  5. container populated with custom widgets
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 16th April 2006, 21:02

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.