Results 1 to 7 of 7

Thread: Class that inherits QWidget to create container of QPushbuttons - doing it correctly?

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Class that inherits QWidget to create container of QPushbuttons - doing it correctly?

    I have created a class that I am calling from another class that deals with the user interface for my program.
    The called class sets up a scrollable area that contains 2 QPushbuttons at this point to test it, and it works fine, however I am uneasy that what I have done is not quite correct, and I was wondering if someone could give me feedback on if it is correct, and how to improve it.
    At this point I have made the 2 buttons very large (in height) only so that I can see that the effect of the scrolling action is working, which it is.
    My concern is that I get the feeling that within the class, I am creating a widget again, even though it inherits a QWidget already, and so this shouldn't be needed.
    Note that my code is based on code that I found online, and modified.

    Qt Code:
    1. #include "stoppagereasons.h"
    2.  
    3. #include <QPushButton>
    4. #include <QGridLayout>
    5. #include <QScrollBar>
    6. #include <QVBoxLayout>
    7. #include <QScrollArea>
    8.  
    9. StoppageReasons::StoppageReasons(QWidget *parent)
    10. : QWidget(parent)
    11. {
    12. QWidget *widget = new QWidget;
    13. QVBoxLayout *layout = new QVBoxLayout();
    14. QPushButton *btn = new QPushButton("test");
    15. btn->setFixedSize(100,500);
    16. QPushButton *btn2 = new QPushButton("test2");
    17. btn2->setFixedSize(100,500);
    18.  
    19. layout->addWidget(btn);
    20. layout->addWidget(btn2);
    21. widget->setLayout(layout);
    22.  
    23. QScrollArea *scroll = new QScrollArea;
    24. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    25. scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    26. scroll->setWidgetResizable(false);
    27. scroll->setWidget(widget);
    28.  
    29. QVBoxLayout *vLayout = new QVBoxLayout();
    30. vLayout->addWidget(scroll);
    31. setLayout(vLayout);
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    That looks ok.

    Your widget contains a QScrollArea and it needs a widget as its content.

    The only thing you could change is to make your widget a subclass of QScrollArea instead of having a single QScrollArea child

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    Thanks anda_skoa

    Would you mind giving me some code? I don't know how to do that.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    I am afraid I don't understand.

    You obviously know how to create a subclass, so instead of deriving from QWidget you just derive from QScrollArea.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    Sorry, I didn't understand you correctly, so this is what I have done now.
    Is this an improvement over the original code? Any other improvements I could do?
    The code is definitely shorter, so I like that.

    Qt Code:
    1. StoppageReasons::StoppageReasons(QScrollArea *parent)
    2. : QScrollArea(parent)
    3. {
    4. QPushButton *btn = new QPushButton("test");
    5. btn->setFixedSize(400,350);
    6. QPushButton *btn2 = new QPushButton("test2");
    7. btn2->setFixedSize(400,350);
    8. QPushButton *btn3 = new QPushButton("test3");
    9. btn3->setFixedSize(400,350);
    10.  
    11. QVBoxLayout *layout = new QVBoxLayout();
    12. layout->addWidget(btn);
    13. layout->addWidget(btn2);
    14. layout->addWidget(btn3);
    15.  
    16. QWidget *widget = new QWidget;
    17. widget->setLayout(layout);
    18.  
    19. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    20. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    21. setWidgetResizable(false);
    22. setWidget(widget);
    23. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    Yes, that's what I meant.

    Looks good to me, the rest is a matter of personal preference.

    E.g. I would first create the "widget" object, then its layout and then add the children as I go along.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class that inherits QWidget to create container of QPushbuttons - doing it correc

    Thanks anda_skoa, I appreciate your input.

Similar Threads

  1. Using container class with my own classes.
    By harvey_slash in forum Newbie
    Replies: 15
    Last Post: 2nd October 2013, 17:03
  2. Best widget to use as container for row of QPushButtons?
    By Coolname007 in forum Qt Programming
    Replies: 3
    Last Post: 25th November 2012, 20:24
  3. Using QWidget as container
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 4th September 2012, 12:21
  4. Replies: 5
    Last Post: 14th June 2012, 08:35
  5. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04

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.