Results 1 to 2 of 2

Thread: Composite Widget Tutorial

  1. #1
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Composite Widget Tutorial

    In QWidget (documentation) it talks about building composite widgets and references a page that's supposed to have a tutorial on how to do it; though i couldn't find it.

    Could someone point me in the direction of a thread or tutorial on (or explain) how to:
    -Turn a qwidget into a container (I believe it is by default)
    -Have it "contain" another widget

    Again, I search the reference it gave, but it didn't seem to contain anything on containers/composite widgets. To clarify, I know how to do it in designer; I need to figure out how to do it.


    I looked at the ui_myclass.h files to get an idea; it seems that it's enough to just say this during initialization?:
    Qt Code:
    1. myContainerClass foo = new myContainterClass[B](ContainedClassName)[/B];
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Composite Widget Tutorial

    The simplest way is just to: create a QWidget, create a QLayout with that widget as parent and add other widgets to the layout:
    Qt Code:
    1. //create a layout with parent
    2. QVBoxLayout *layout = new QVBoxLayout(&w);
    3. //widgets that will be contained in QWidget
    4. QLabel* label1 = new QLabel("Label 1");
    5. QLabel* label2 = new QLabel("Label 2");
    6. layout->addWidget(label1); //add contained widgets to layout
    7. layout->addWidget(label2);
    8. w.show(); //show the widget
    To copy to clipboard, switch view to plain text mode 
    Or another way is to derive a class from QWidget and contain the widgets into that class, this creates a better "encapsulated widget" that can be reused easier.
    Qt Code:
    1. //MyWidget.h file
    2. #ifndef MYWIDGET_H
    3. #define MYWIDGET_H
    4. #include <QWidget>
    5.  
    6. //forward declarations
    7. class QLabel;
    8.  
    9. class MyWidget : public QWidget //inherit from QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. MyWidget(QWidget* parent = 0); //don't forget to pass the parent
    14.  
    15. private:
    16. //contained widgets:
    17. QVBoxLayout *mainLayout;
    18. QHBoxLayout *anotherLayout;
    19. QLabel *firstLabel;
    20. QLabel *secondLabel;
    21. QPushButton *firstButton;
    22. QPushButton *secondButton;
    23. signals:
    24. //MyWidget's signals....
    25. public slots:
    26. //MyWidget's slots example:
    27. // void firstButtonClicked();
    28. //...
    29. };
    30. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 
    And in the constructor you initialize the pointers and add them to the layouts:
    Qt Code:
    1. #include "mywidget.h"
    2. #include <QVBoxLayout>
    3. #include <QHBoxLayout>
    4. #include <QLabel>
    5. #include <QPushButton>
    6.  
    7. MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
    8. {
    9. mainLayout = new QVBoxLayout(this);
    10. anotherLayout = new QHBoxLayout();
    11.  
    12. firstLabel = new QLabel("first label");
    13. secondLabel = new QLabel("second label");
    14. firstButton = new QPushButton("Fist button");
    15. secondButton = new QPushButton("Second Button");
    16.  
    17. mainLayout->addWidget(firstLabel);
    18. mainLayout->addWidget(firstButton);
    19. mainLayout->addLayout(anotherLayout);
    20. anotherLayout->addWidget(secondLabel);
    21. anotherLayout->addWidget(secondButton);
    22. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. mapping composite data from model to widgets
    By tzeentch.gm in forum Newbie
    Replies: 4
    Last Post: 4th October 2011, 06:32
  2. Help with a tutorial
    By PeterUK in forum Newbie
    Replies: 2
    Last Post: 24th July 2009, 11:11
  3. Creating And Destoying composite Widgets
    By morraine in forum Newbie
    Replies: 3
    Last Post: 31st August 2008, 16:57
  4. QT tutorial 7
    By ranu in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2007, 06:05
  5. Qt Tutorial #1 Add-on
    By ShaChris23 in forum Newbie
    Replies: 10
    Last Post: 24th April 2007, 00:29

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
  •  
Qt is a trademark of The Qt Company.