Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: QScrollArea With Custom Widgets

  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.

  12. #12
    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

    See it's not working. Inside my main loop, I tried addWidget(cobjects[i], i, i); just to see if i can get more than one, but nooooo, still one damn widget in one damn top left corner.

    Also, lemme list something, the QScrollArea is there for a reason. Because there will always be vertical scroll, since there may be a high number of objects. Let's say the person maximizes the program the program will go from:

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    17 18 19 20
    21 22 23 24

    to

    1 2 3 4 5 6 7 8
    9 10 11 12 13 14 15 16
    17 18 19 20 21 22 23 24

    Understand??
    So in other words, I dont even know if I should use grid layout, because I need the positions of the objects to change according to the size of the widget. And I don't see no "ChangeItemPos()"... And eliminating the gridlayout does the same thing, even with no gridlayout move doesnt' work... WHY?
    Last edited by VireX; 20th April 2007 at 17:24.

  13. #13
    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

    Could you prepare a small compilable example and attach it here?

  14. #14
    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

    ok hold on... but btw, i updated my last post. Ima have to do it after lunch.

  15. #15
    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

    You should use the flow layout (or whatever it's called) instead of grid layout. It's bundled with Qt as one of its examples.

  16. #16
    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 tried using a flowlayout,
    with Cobjects, it doesn't work, still same problem.
    With QPushButtons, it lists them like so:
    1 2 3 4 5 6 7 8 9 10 11 12
    13 14 15 16 17 18 19 20 21 21 23 24

    But, I don't want to scroll horizontally, so flowlayout is useless to me, because it doesn't adjust itself to fit the size of the QScrollArea and window. There should never be a need to use horizontal scroll, it should automatically reorder itself to fit vertical scroll only.

  17. #17
    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

    What is a CObject? Obviously you can only put QObjects into layouts. And the flow layout does work for you - it layouts widgets, it doesn't control size of the parent widget - you have to control that yourself. Did you make the widget of the viewport resizable? Maybe it'll adjust itself to the scrollarea then.

  18. #18
    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

    CObject inherits QObject. CObject has like 2 buttons.
    I don't understand what you mean but I'm about to upload the test rar i prepared for you to better understand.

    I can't believe VBulletin doesn't accept rar, who doesn't use rar... :S.

    Also an interesting find:
    I tried this:
    flow->addWidget(new QPushButton("bla"));

    and it crashes

    but when I do
    flow->addWidget(new CObject(this));

    it works fine. Meaning QPushButtons don't work with flowlayouts... odd. Hmm I noticed that it works only if i resize the widget, i guess some sort of size causes flowlayouts to crash.

    But mainly the bigger problem is, when you have QPushButtons it seems to have the illusion that its working except ResizeEvent doesn't work AT ALL. However, why the hell does my widget only become 1 widget, why can't i ever see more than one? Why doesn't the 24 widgets I make become visible?
    Attached Files Attached Files
    Last edited by VireX; 20th April 2007 at 19:32.

  19. #19
    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

    I guess the thing attached is what you wanted.
    Attached Files Attached Files

  20. #20
    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

    Yes that's exactly what I wanted... now i'm gonna try it with my object class.

    Nope, it's not working with FlowLayout,.. My class that inherits QWidget, with parent wgt.

    So basically, flowlayout only works with QPushButtons, and other widgets, not with widgets with multiple buttons and QLabels.
    Last edited by VireX; 20th April 2007 at 23:10.

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.